|
1 | 1 | import { getByText, fireEvent } from '@testing-library/dom';
|
2 | 2 | import instantsearch from '../../index.es';
|
3 |
| -import { configure } from '../../widgets'; |
| 3 | +import { configure, searchBox } from '../../widgets'; |
4 | 4 | import { connectConfigure } from '../../connectors';
|
5 | 5 | import { createSearchClient } from '../../../test/mock/createSearchClient';
|
| 6 | +import { MiddlewareDefinition } from '../../types'; |
| 7 | +import { runAllMicroTasks } from '../../../test/utils/runAllMicroTasks'; |
6 | 8 |
|
7 | 9 | describe('configure', () => {
|
8 | 10 | it('provides up-to-date uiState to onStateChange', () => {
|
@@ -44,3 +46,72 @@ describe('configure', () => {
|
44 | 46 | });
|
45 | 47 | });
|
46 | 48 | });
|
| 49 | + |
| 50 | +describe('middleware', () => { |
| 51 | + it("runs middlewares' onStateChange when uiState changes", async () => { |
| 52 | + const container = document.createElement('div'); |
| 53 | + const search = instantsearch({ |
| 54 | + indexName: 'instant_search', |
| 55 | + searchClient: createSearchClient(), |
| 56 | + }); |
| 57 | + |
| 58 | + const middlewareDefinition: MiddlewareDefinition = { |
| 59 | + onStateChange: jest.fn(), |
| 60 | + subscribe: jest.fn(), |
| 61 | + unsubscribe: jest.fn(), |
| 62 | + }; |
| 63 | + |
| 64 | + search.use(() => middlewareDefinition); |
| 65 | + |
| 66 | + search.addWidgets([ |
| 67 | + searchBox({ |
| 68 | + container, |
| 69 | + placeholder: 'search', |
| 70 | + }), |
| 71 | + ]); |
| 72 | + |
| 73 | + search.start(); |
| 74 | + |
| 75 | + fireEvent.input(container.querySelector('input')!, { |
| 76 | + target: { value: 'q' }, |
| 77 | + }); |
| 78 | + |
| 79 | + await runAllMicroTasks(); |
| 80 | + expect(middlewareDefinition.onStateChange).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it("runs middlewares' onStateChange when uiState changes with user-provided onStateChange param", async () => { |
| 84 | + const container = document.createElement('div'); |
| 85 | + const search = instantsearch({ |
| 86 | + indexName: 'instant_search', |
| 87 | + searchClient: createSearchClient(), |
| 88 | + onStateChange({ uiState, setUiState }) { |
| 89 | + setUiState(uiState); |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const middlewareDefinition: MiddlewareDefinition = { |
| 94 | + onStateChange: jest.fn(), |
| 95 | + subscribe: jest.fn(), |
| 96 | + unsubscribe: jest.fn(), |
| 97 | + }; |
| 98 | + |
| 99 | + search.use(() => middlewareDefinition); |
| 100 | + |
| 101 | + search.addWidgets([ |
| 102 | + searchBox({ |
| 103 | + container, |
| 104 | + placeholder: 'search', |
| 105 | + }), |
| 106 | + ]); |
| 107 | + |
| 108 | + search.start(); |
| 109 | + |
| 110 | + fireEvent.input(container.querySelector('input')!, { |
| 111 | + target: { value: 'q' }, |
| 112 | + }); |
| 113 | + |
| 114 | + await runAllMicroTasks(); |
| 115 | + expect(middlewareDefinition.onStateChange).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | +}); |
0 commit comments