Skip to content

Commit 57c32c0

Browse files
Haroenvtkruggfrancoischalifour
authoredJun 29, 2021
fix(onStateChange): propagate change to middleware (#4796)
* chore: create a regression test showcasing onStateChange regression * fix(onStateChange): still call internal state change * simplify test * better test titles Co-authored-by: Youcef Mammar <youcef.mammar@algolia.com> Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
1 parent 2481834 commit 57c32c0

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
 

‎src/lib/InstantSearch.ts

+1
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ See ${createDocumentationLink({
597597
setIndexHelperState(this.mainIndex);
598598

599599
this.scheduleSearch();
600+
this.onInternalStateChange();
600601
}
601602

602603
public getUiState(): UiState {

‎src/lib/__tests__/InstantSearch-integration-test.ts

+72-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { getByText, fireEvent } from '@testing-library/dom';
22
import instantsearch from '../../index.es';
3-
import { configure } from '../../widgets';
3+
import { configure, searchBox } from '../../widgets';
44
import { connectConfigure } from '../../connectors';
55
import { createSearchClient } from '../../../test/mock/createSearchClient';
6+
import { MiddlewareDefinition } from '../../types';
7+
import { runAllMicroTasks } from '../../../test/utils/runAllMicroTasks';
68

79
describe('configure', () => {
810
it('provides up-to-date uiState to onStateChange', () => {
@@ -44,3 +46,72 @@ describe('configure', () => {
4446
});
4547
});
4648
});
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

Comments
 (0)
Please sign in to comment.