|
| 1 | +import { enhanceConfiguration } from '../InstantSearch'; |
| 2 | + |
| 3 | +const createWidget = (configuration = {}) => ({ |
| 4 | + getConfiguration: () => configuration, |
| 5 | +}); |
| 6 | + |
| 7 | +describe('enhanceConfiguration', () => { |
| 8 | + it('should return the same object if widget does not provide a configuration', () => { |
| 9 | + const configuration = { analytics: true, page: 2 }; |
| 10 | + const widget = {}; |
| 11 | + |
| 12 | + const output = enhanceConfiguration({})(configuration, widget); |
| 13 | + expect(output).toBe(configuration); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should return a new object if widget does provide a configuration', () => { |
| 17 | + const configuration = { analytics: true, page: 2 }; |
| 18 | + const widget = createWidget(configuration); |
| 19 | + |
| 20 | + const output = enhanceConfiguration({})(configuration, widget); |
| 21 | + expect(output).not.toBe(configuration); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should add widget configuration to an empty state', () => { |
| 25 | + const configuration = { analytics: true, page: 2 }; |
| 26 | + const widget = createWidget(configuration); |
| 27 | + |
| 28 | + const output = enhanceConfiguration({})(configuration, widget); |
| 29 | + expect(output).toEqual(configuration); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should call `getConfiguration` from widget correctly', () => { |
| 33 | + const widget = { getConfiguration: jest.fn() }; |
| 34 | + |
| 35 | + const configuration = {}; |
| 36 | + const searchParametersFromUrl = {}; |
| 37 | + enhanceConfiguration(searchParametersFromUrl)(configuration, widget); |
| 38 | + |
| 39 | + expect(widget.getConfiguration).toHaveBeenCalled(); |
| 40 | + expect(widget.getConfiguration).toHaveBeenCalledWith( |
| 41 | + configuration, |
| 42 | + searchParametersFromUrl |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should replace boolean values', () => { |
| 47 | + const actualConfiguration = { analytics: false }; |
| 48 | + const widget = createWidget({ analytics: true }); |
| 49 | + |
| 50 | + const output = enhanceConfiguration({})(actualConfiguration, widget); |
| 51 | + expect(output.analytics).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should union array', () => { |
| 55 | + { |
| 56 | + const actualConfiguration = { refinements: ['foo'] }; |
| 57 | + const widget = createWidget({ refinements: ['foo', 'bar'] }); |
| 58 | + |
| 59 | + const output = enhanceConfiguration({})(actualConfiguration, widget); |
| 60 | + expect(output.refinements).toEqual(['foo', 'bar']); |
| 61 | + } |
| 62 | + |
| 63 | + { |
| 64 | + const actualConfiguration = { refinements: ['foo'] }; |
| 65 | + const widget = createWidget({ refinements: ['bar'] }); |
| 66 | + |
| 67 | + const output = enhanceConfiguration({})(actualConfiguration, widget); |
| 68 | + expect(output.refinements).toEqual(['foo', 'bar']); |
| 69 | + } |
| 70 | + |
| 71 | + { |
| 72 | + const actualConfiguration = { refinements: ['foo', 'bar'] }; |
| 73 | + const widget = createWidget({ refinements: [] }); |
| 74 | + |
| 75 | + const output = enhanceConfiguration({})(actualConfiguration, widget); |
| 76 | + expect(output.refinements).toEqual(['foo', 'bar']); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it('should replace nested values', () => { |
| 81 | + const actualConfiguration = { refinements: { lvl1: ['foo'], lvl2: false } }; |
| 82 | + const widget = createWidget({ refinements: { lvl1: ['bar'], lvl2: true } }); |
| 83 | + |
| 84 | + const output = enhanceConfiguration({})(actualConfiguration, widget); |
| 85 | + expect(output).toEqual({ |
| 86 | + refinements: { lvl1: ['foo', 'bar'], lvl2: true }, |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments