Skip to content

Commit 7f62e6d

Browse files
authored
fix(routing): should apply stateMapping when doing initial write (#2892)
* test(routing): should apply stateMapping when doing initial write * fix(routing): should apply stateMapping when doing initial write
1 parent 58b6f7f commit 7f62e6d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/lib/RoutingManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export default class RoutingManager {
9696
// We do this in order to make a URL update when there is search function
9797
// that prevent the search of the initial rendering
9898
// See: https://github.com/algolia/instantsearch.js/issues/2523#issuecomment-339356157
99-
this.router.write(firstRenderState);
99+
const route = this.stateMapping.stateToRoute(firstRenderState);
100+
this.router.write(route);
100101
}
101102
}
102103

src/lib/__tests__/RoutingManager-test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,61 @@ describe('RoutingManager', () => {
258258
});
259259
});
260260
});
261+
262+
test('should apply state mapping on differences after searchfunction', done => {
263+
const router = {
264+
write: jest.fn(),
265+
read: jest.fn(() => ({})),
266+
onUpdate: jest.fn(),
267+
};
268+
const stateMapping = {
269+
stateToRoute(uiState) {
270+
return {
271+
query: uiState.query && uiState.query.toUpperCase(),
272+
};
273+
},
274+
routeToState(routeState) {
275+
return routeState;
276+
},
277+
};
278+
const search = instantsearch({
279+
appId: 'latency',
280+
apiKey: '6be0576ff61c053d5f9a3225e2a90f76',
281+
indexName: 'ikea',
282+
routing: {
283+
router,
284+
stateMapping,
285+
},
286+
searchFunction: helper => {
287+
helper.setQuery('test').search();
288+
},
289+
});
290+
291+
const widget = {
292+
render: jest.fn(),
293+
getWidgetSearchParameters: jest.fn(),
294+
getWidgetState(uiState, { searchParameters }) {
295+
return {
296+
...uiState,
297+
query: searchParameters.query,
298+
};
299+
},
300+
};
301+
search.addWidget(widget);
302+
303+
search.start();
304+
305+
search.once('render', () => {
306+
// initialization is done at this point
307+
308+
expect(search.helper.state.query).toEqual('test');
309+
310+
expect(router.write).toHaveBeenLastCalledWith({
311+
query: 'TEST',
312+
});
313+
314+
done();
315+
});
316+
});
261317
});
262318
});

0 commit comments

Comments
 (0)