Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 1d38d5e

Browse files
committed
feat(IS): allow change of stalled search delay and search function
1 parent 6bac0bd commit 1d38d5e

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/components/InstantSearch.vue

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,9 @@ export default {
9696
'Please open a new issue: https://github.com/algolia/vue-instantsearch/issues/new?template=feature.md'
9797
);
9898
},
99-
searchFunction() {
100-
throw new Error(
101-
'searchFunction configuration can not be changed dynamically at this point.' +
102-
'\n\n' +
103-
'Please open a new issue: https://github.com/algolia/vue-instantsearch/issues/new?template=feature.md'
104-
);
99+
searchFunction(searchFunction) {
100+
// private InstantSearch.js API:
101+
this.instantSearchInstance._searchFunction = searchFunction;
105102
},
106103
},
107104
created() {

src/components/__tests__/InstantSearch.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,32 @@ import InstantSearch from '../InstantSearch.vue';
66
jest.mock('instantsearch.js/es', () => {
77
const isPlainObject = require('lodash/isPlainObject');
88
const start = jest.fn();
9+
10+
class RoutingManager {
11+
constructor(routing) {
12+
this._routing = routing;
13+
}
14+
}
915
const fakeInstantSearch = jest.fn(
1016
({
1117
indexName,
1218
searchClient,
13-
routing, // eslint-disable-line no-unused-vars
14-
stalledSearchDelay, // eslint-disable-line no-unused-vars
15-
searchFunction, // eslint-disable-line no-unused-vars
19+
routing,
20+
stalledSearchDelay,
21+
searchFunction,
1622
}) => {
17-
if (!searchClient && isPlainObject(searchClient)) {
18-
throw new Error('need searchClient to be a fn');
23+
if (!searchClient && !isPlainObject(searchClient)) {
24+
throw new Error('need searchClient to be a plain object');
1925
}
2026
if (!indexName) {
2127
throw new Error('need indexName to be a string');
2228
}
2329
return {
24-
start,
30+
_stalledSearchDelay: stalledSearchDelay,
31+
_searchFunction: searchFunction,
32+
routing: new RoutingManager(routing),
2533
helper: fakeInstantSearch.__helper,
34+
start,
2635
};
2736
}
2837
);
@@ -191,26 +200,44 @@ it('Allows a change in `search-client`', () => {
191200
expect(instantsearch.__helper.search).toHaveBeenCalledTimes(1);
192201
});
193202

194-
it('Does not allow a change in `search-function`', () => {
195-
global.console.error = jest.fn();
203+
it('Allows a change in `search-function`', () => {
204+
const oldValue = () => {};
205+
const newValue = () => {};
206+
196207
const wrapper = mount(InstantSearch, {
197208
propsData: {
198209
searchClient: {},
199210
indexName: 'bla',
200-
searchFunction: () => {},
211+
searchFunction: oldValue,
201212
},
202213
});
203214

215+
expect(wrapper.vm.instantSearchInstance._searchFunction).toEqual(oldValue);
216+
204217
wrapper.setProps({
205-
searchFunction: () => {},
218+
searchFunction: newValue,
206219
});
207220

208-
// Vue catches this error and throws it to the console
209-
expect(global.console.error.mock.calls[0][0]).toMatchInlineSnapshot(`
210-
[Error: searchFunction configuration can not be changed dynamically at this point.
221+
expect(wrapper.vm.instantSearchInstance._searchFunction).toEqual(newValue);
222+
});
211223

212-
Please open a new issue: https://github.com/algolia/vue-instantsearch/issues/new?template=feature.md]
213-
`);
224+
it('Allows a change in `stalled-search-delay`', () => {
225+
const wrapper = mount(InstantSearch, {
226+
propsData: {
227+
searchClient: {},
228+
indexName: 'bla',
229+
searchFunction: () => {},
230+
stalledSearchDelay: 200,
231+
},
232+
});
233+
234+
expect(wrapper.vm.instantSearchInstance._stalledSearchDelay).toEqual(200);
235+
236+
wrapper.setProps({
237+
stalledSearchDelay: 50,
238+
});
239+
240+
expect(wrapper.vm.instantSearchInstance._stalledSearchDelay).toEqual(50);
214241
});
215242

216243
it('Does not allow a change in `routing`', () => {

0 commit comments

Comments
 (0)