|
| 1 | +import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights'; |
| 2 | + |
1 | 3 | import { createAutocomplete } from '../createAutocomplete'; |
2 | 4 |
|
3 | 5 | describe('createAutocomplete', () => { |
@@ -43,4 +45,126 @@ describe('createAutocomplete', () => { |
43 | 45 | setStatus: autocomplete.setStatus, |
44 | 46 | }); |
45 | 47 | }); |
| 48 | + |
| 49 | + describe('Insights plugin', () => { |
| 50 | + test('does not add Insights plugin by default', () => { |
| 51 | + const onStateChange = jest.fn(); |
| 52 | + |
| 53 | + createAutocomplete({ onStateChange }); |
| 54 | + |
| 55 | + expect(onStateChange).toHaveBeenCalledTimes(0); |
| 56 | + }); |
| 57 | + |
| 58 | + test('`insights: true` adds only one Insights plugin', () => { |
| 59 | + const onStateChange = jest.fn(); |
| 60 | + |
| 61 | + createAutocomplete({ |
| 62 | + onStateChange, |
| 63 | + insights: true, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(onStateChange).toHaveBeenCalledTimes(1); |
| 67 | + expect(onStateChange).toHaveBeenCalledWith( |
| 68 | + expect.objectContaining({ |
| 69 | + state: expect.objectContaining({ |
| 70 | + context: expect.objectContaining({ |
| 71 | + algoliaInsightsPlugin: expect.objectContaining({ |
| 72 | + insights: expect.objectContaining({ |
| 73 | + init: expect.any(Function), |
| 74 | + setUserToken: expect.any(Function), |
| 75 | + clickedObjectIDsAfterSearch: expect.any(Function), |
| 76 | + clickedObjectIDs: expect.any(Function), |
| 77 | + clickedFilters: expect.any(Function), |
| 78 | + convertedObjectIDsAfterSearch: expect.any(Function), |
| 79 | + convertedObjectIDs: expect.any(Function), |
| 80 | + convertedFilters: expect.any(Function), |
| 81 | + viewedObjectIDs: expect.any(Function), |
| 82 | + viewedFilters: expect.any(Function), |
| 83 | + }), |
| 84 | + }), |
| 85 | + }), |
| 86 | + }), |
| 87 | + }) |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + test('`insights` with options still creates one plugin only', () => { |
| 92 | + const onStateChange = jest.fn(); |
| 93 | + const insightsClient = jest.fn(); |
| 94 | + |
| 95 | + createAutocomplete({ |
| 96 | + onStateChange, |
| 97 | + insights: { insightsClient }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(onStateChange).toHaveBeenCalledTimes(1); |
| 101 | + expect(onStateChange).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ |
| 103 | + state: expect.objectContaining({ |
| 104 | + context: expect.objectContaining({ |
| 105 | + algoliaInsightsPlugin: expect.objectContaining({ |
| 106 | + insights: expect.objectContaining({ |
| 107 | + init: expect.any(Function), |
| 108 | + setUserToken: expect.any(Function), |
| 109 | + clickedObjectIDsAfterSearch: expect.any(Function), |
| 110 | + clickedObjectIDs: expect.any(Function), |
| 111 | + clickedFilters: expect.any(Function), |
| 112 | + convertedObjectIDsAfterSearch: expect.any(Function), |
| 113 | + convertedObjectIDs: expect.any(Function), |
| 114 | + convertedFilters: expect.any(Function), |
| 115 | + viewedObjectIDs: expect.any(Function), |
| 116 | + viewedFilters: expect.any(Function), |
| 117 | + }), |
| 118 | + }), |
| 119 | + }), |
| 120 | + }), |
| 121 | + }) |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + test('`insights` with options passes options to plugin', () => { |
| 126 | + const insightsClient = jest.fn(); |
| 127 | + |
| 128 | + createAutocomplete({ |
| 129 | + insights: { insightsClient }, |
| 130 | + }); |
| 131 | + |
| 132 | + expect(insightsClient).toHaveBeenCalledTimes(1); |
| 133 | + expect(insightsClient).toHaveBeenCalledWith( |
| 134 | + 'addAlgoliaAgent', |
| 135 | + 'insights-plugin' |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + test('`insights: false` disables default Insights plugin', () => { |
| 140 | + const onStateChange = jest.fn(); |
| 141 | + |
| 142 | + createAutocomplete({ |
| 143 | + insights: false, |
| 144 | + onStateChange, |
| 145 | + }); |
| 146 | + |
| 147 | + expect(onStateChange).toHaveBeenCalledTimes(0); |
| 148 | + }); |
| 149 | + |
| 150 | + test("users' Insights plugin overrides the default one when `insights: true`", () => { |
| 151 | + const defaultInsightsClient = jest.fn(); |
| 152 | + const userInsightsClient = jest.fn(); |
| 153 | + const insightsPlugin = createAlgoliaInsightsPlugin({ |
| 154 | + insightsClient: userInsightsClient, |
| 155 | + }); |
| 156 | + |
| 157 | + createAutocomplete({ |
| 158 | + insights: { insightsClient: defaultInsightsClient }, |
| 159 | + plugins: [insightsPlugin], |
| 160 | + }); |
| 161 | + |
| 162 | + expect(defaultInsightsClient).toHaveBeenCalledTimes(0); |
| 163 | + expect(userInsightsClient).toHaveBeenCalledTimes(1); |
| 164 | + expect(userInsightsClient).toHaveBeenCalledWith( |
| 165 | + 'addAlgoliaAgent', |
| 166 | + 'insights-plugin' |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
46 | 170 | }); |
0 commit comments