|
| 1 | +import { fetchAutocompleteSuggestions } from './autocomplete' |
| 2 | +import { createContext } from '../mocks/contextFactory' |
| 3 | + |
| 4 | +describe('fetchAutocompleteSuggestions', () => { |
| 5 | + beforeEach(() => { |
| 6 | + jest.clearAllMocks() |
| 7 | + }) |
| 8 | + |
| 9 | + it('should call both clients and not log errors', async () => { |
| 10 | + const ctx = createContext({ |
| 11 | + intelligentSearchApiSettings: { |
| 12 | + fetchAutocompleteSuggestions: { |
| 13 | + searches: [{ term: 'test', count: 1 }], |
| 14 | + }, |
| 15 | + }, |
| 16 | + intschSettings: { |
| 17 | + fetchAutocompleteSuggestions: { |
| 18 | + searches: [{ term: 'test', count: 1 }], |
| 19 | + }, |
| 20 | + }, |
| 21 | + }) |
| 22 | + |
| 23 | + await fetchAutocompleteSuggestions(ctx, 'test') |
| 24 | + |
| 25 | + expect( |
| 26 | + ctx.clients.intelligentSearchApi.fetchAutocompleteSuggestions |
| 27 | + ).toHaveBeenCalledWith({ |
| 28 | + query: 'test', |
| 29 | + }) |
| 30 | + expect( |
| 31 | + ctx.clients.intsch.fetchAutocompleteSuggestions |
| 32 | + ).toHaveBeenCalledWith({ |
| 33 | + query: 'test', |
| 34 | + }) |
| 35 | + |
| 36 | + // no errors should be logged because the results are equal |
| 37 | + expect(ctx.vtex.logger.error).not.toHaveBeenCalled() |
| 38 | + }) |
| 39 | + |
| 40 | + it('should log if the results are different', async () => { |
| 41 | + const result = { |
| 42 | + searches: [{ term: 'test', count: 1 }], |
| 43 | + } |
| 44 | + |
| 45 | + const ctx = createContext({ |
| 46 | + intelligentSearchApiSettings: { |
| 47 | + fetchAutocompleteSuggestions: result, |
| 48 | + }, |
| 49 | + intschSettings: { |
| 50 | + fetchAutocompleteSuggestions: { |
| 51 | + searches: [ |
| 52 | + { |
| 53 | + term: 'camisa feminina', |
| 54 | + count: 1, |
| 55 | + attributes: [ |
| 56 | + { |
| 57 | + key: 'departamento', |
| 58 | + labelKey: 'Departamento', |
| 59 | + labelValue: 'Apparel & Accessories', |
| 60 | + value: 'apparel---accessories', |
| 61 | + }, |
| 62 | + { |
| 63 | + key: 'categoria', |
| 64 | + labelKey: 'Categoria', |
| 65 | + labelValue: 'Roupa', |
| 66 | + value: 'roupa', |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + { |
| 71 | + term: 'camisa masculina', |
| 72 | + count: 1, |
| 73 | + }, |
| 74 | + { |
| 75 | + term: 'camiseta', |
| 76 | + count: 2, |
| 77 | + }, |
| 78 | + { |
| 79 | + term: 'camisa', |
| 80 | + count: 1, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + const response = await fetchAutocompleteSuggestions(ctx, 'test') |
| 88 | + |
| 89 | + expect( |
| 90 | + ctx.clients.intelligentSearchApi.fetchAutocompleteSuggestions |
| 91 | + ).toHaveBeenCalledWith({ |
| 92 | + query: 'test', |
| 93 | + }) |
| 94 | + expect( |
| 95 | + ctx.clients.intsch.fetchAutocompleteSuggestions |
| 96 | + ).toHaveBeenCalledWith({ |
| 97 | + query: 'test', |
| 98 | + }) |
| 99 | + expect(ctx.vtex.logger.error).toHaveBeenCalledWith({ |
| 100 | + message: 'Autocomplete Suggestions: "test": Results differ', |
| 101 | + params: JSON.stringify({ query: 'test' }), |
| 102 | + }) |
| 103 | + expect(response).toEqual(result) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should still function if the new client fails', async () => { |
| 107 | + const result = { |
| 108 | + searches: [{ term: 'test', count: 1 }], |
| 109 | + } |
| 110 | + |
| 111 | + const ctx = createContext({ |
| 112 | + intelligentSearchApiSettings: { |
| 113 | + fetchAutocompleteSuggestions: result, |
| 114 | + }, |
| 115 | + intschSettings: { |
| 116 | + fetchAutocompleteSuggestions: new Error('Failed to fetch suggestions'), |
| 117 | + }, |
| 118 | + }) |
| 119 | + |
| 120 | + const response = await fetchAutocompleteSuggestions(ctx, 'test') |
| 121 | + |
| 122 | + expect( |
| 123 | + ctx.clients.intelligentSearchApi.fetchAutocompleteSuggestions |
| 124 | + ).toHaveBeenCalledWith({ |
| 125 | + query: 'test', |
| 126 | + }) |
| 127 | + expect( |
| 128 | + ctx.clients.intsch.fetchAutocompleteSuggestions |
| 129 | + ).toHaveBeenCalledWith({ |
| 130 | + query: 'test', |
| 131 | + }) |
| 132 | + |
| 133 | + expect(ctx.vtex.logger.error).not.toHaveBeenCalled() |
| 134 | + // the result should be the result from the old client |
| 135 | + expect(response).toEqual(result) |
| 136 | + }) |
| 137 | +}) |
0 commit comments