Skip to content

Commit ffadf64

Browse files
francoischalifourHaroenv
authored andcommitted
fix(ratingMenu): update lifecycle state (#3987)
1 parent 208f271 commit ffadf64

File tree

3 files changed

+146
-20
lines changed

3 files changed

+146
-20
lines changed

src/connectors/rating-menu/__tests__/connectRatingMenu-test.js

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
5858
attribute,
5959
});
6060

61-
const config = widget.getConfiguration({});
62-
expect(config).toEqual({
63-
disjunctiveFacets: [attribute],
64-
});
61+
const config = widget.getConfiguration(new SearchParameters({}));
62+
expect(config).toEqual(
63+
new SearchParameters({
64+
disjunctiveFacets: [attribute],
65+
disjunctiveFacetsRefinements: {
66+
grade: [],
67+
},
68+
})
69+
);
6570

6671
const helper = jsHelper({}, '', config);
6772
helper.search = jest.fn();
@@ -167,7 +172,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
167172
attribute,
168173
});
169174

170-
const config = widget.getConfiguration({});
175+
const config = widget.getConfiguration(new SearchParameters({}));
171176

172177
const helper = jsHelper({}, '', config);
173178
helper.search = jest.fn();
@@ -261,6 +266,110 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
261266
}
262267
});
263268

269+
describe('getConfiguration', () => {
270+
test('returns initial search parameters', () => {
271+
const rendering = jest.fn();
272+
const makeWidget = connectRatingMenu(rendering);
273+
274+
const attribute = 'grade';
275+
const widget = makeWidget({
276+
attribute,
277+
});
278+
279+
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
280+
new SearchParameters({
281+
disjunctiveFacets: [attribute],
282+
disjunctiveFacetsRefinements: {
283+
grade: [],
284+
},
285+
})
286+
);
287+
});
288+
289+
test('supports previous disjunctive facets refinements', () => {
290+
const rendering = jest.fn();
291+
const makeWidget = connectRatingMenu(rendering);
292+
293+
const attribute = 'grade';
294+
const widget = makeWidget({
295+
attribute,
296+
});
297+
298+
expect(
299+
widget.getConfiguration(
300+
new SearchParameters({
301+
disjunctiveFacets: [attribute],
302+
disjunctiveFacetsRefinements: {
303+
grade: [4],
304+
},
305+
})
306+
)
307+
).toEqual(
308+
new SearchParameters({
309+
disjunctiveFacets: [attribute],
310+
disjunctiveFacetsRefinements: {
311+
grade: [4],
312+
},
313+
})
314+
);
315+
});
316+
});
317+
318+
describe('dispose', () => {
319+
test('calls the unmount function', () => {
320+
const render = jest.fn();
321+
const unmount = jest.fn();
322+
const makeWidget = connectRatingMenu(render, unmount);
323+
const helper = jsHelper({}, '', {});
324+
helper.search = jest.fn();
325+
326+
const attribute = 'grade';
327+
const widget = makeWidget({
328+
attribute,
329+
});
330+
331+
widget.dispose({ state: helper.state });
332+
333+
expect(unmount).toHaveBeenCalledTimes(1);
334+
});
335+
336+
test('resets the state', () => {
337+
const render = jest.fn();
338+
const makeWidget = connectRatingMenu(render);
339+
const indexName = 'indexName';
340+
const attribute = 'grade';
341+
const helper = jsHelper({}, indexName, {
342+
disjunctiveFacets: [attribute],
343+
disjunctiveFacetsRefinements: {
344+
[attribute]: [4, 5],
345+
},
346+
});
347+
helper.search = jest.fn();
348+
349+
const widget = makeWidget({
350+
attribute,
351+
});
352+
353+
expect(helper.state).toEqual(
354+
new SearchParameters({
355+
index: indexName,
356+
disjunctiveFacets: [attribute],
357+
disjunctiveFacetsRefinements: {
358+
grade: [4, 5],
359+
},
360+
})
361+
);
362+
363+
const nextState = widget.dispose({ state: helper.state });
364+
365+
expect(nextState).toEqual(
366+
new SearchParameters({
367+
index: indexName,
368+
})
369+
);
370+
});
371+
});
372+
264373
describe('routing', () => {
265374
const getInitializedWidget = (config = {}) => {
266375
const rendering = jest.fn();
@@ -272,7 +381,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
272381
...config,
273382
});
274383

275-
const initialConfig = widget.getConfiguration({});
384+
const initialConfig = widget.getConfiguration(new SearchParameters({}));
276385
const helper = jsHelper({}, '', initialConfig);
277386
helper.search = jest.fn();
278387

src/connectors/rating-menu/connectRatingMenu.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ export default function connectRatingMenu(renderFn, unmountFn = noop) {
106106
return {
107107
$$type: 'ais.ratingMenu',
108108

109-
getConfiguration() {
110-
return { disjunctiveFacets: [attribute] };
109+
getConfiguration(state) {
110+
return state.setQueryParameters({
111+
disjunctiveFacets: [attribute],
112+
disjunctiveFacetsRefinements: {
113+
[attribute]: state.disjunctiveFacetsRefinements[attribute] || [],
114+
},
115+
});
111116
},
112117

113118
init({ helper, createURL, instantSearchInstance }) {
@@ -134,7 +139,7 @@ export default function connectRatingMenu(renderFn, unmountFn = noop) {
134139
for (let v = max; v >= 0; --v) {
135140
allValues[v] = 0;
136141
}
137-
results.getFacetValues(attribute).forEach(facet => {
142+
(results.getFacetValues(attribute) || []).forEach(facet => {
138143
const val = Math.round(facet.name);
139144
if (!val || val > max) {
140145
return;
@@ -180,11 +185,7 @@ export default function connectRatingMenu(renderFn, unmountFn = noop) {
180185
dispose({ state }) {
181186
unmountFn();
182187

183-
const nextState = state
184-
.removeDisjunctiveFacetRefinement(attribute)
185-
.removeDisjunctiveFacet(attribute);
186-
187-
return nextState;
188+
return state.removeDisjunctiveFacet(attribute);
188189
},
189190

190191
getWidgetState(uiState, { searchParameters }) {

src/widgets/rating-menu/__tests__/rating-menu-test.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { render } from 'preact-compat';
2-
import jsHelper, { SearchResults } from 'algoliasearch-helper';
2+
import jsHelper, {
3+
SearchResults,
4+
SearchParameters,
5+
} from 'algoliasearch-helper';
36
import ratingMenu from '../rating-menu';
47

58
jest.mock('preact-compat', () => {
@@ -40,7 +43,11 @@ describe('ratingMenu()', () => {
4043
attribute,
4144
cssClasses: { body: ['body', 'cx'] },
4245
});
43-
helper = jsHelper({}, '', widget.getConfiguration({}));
46+
helper = jsHelper(
47+
{},
48+
'',
49+
widget.getConfiguration(new SearchParameters({}))
50+
);
4451
jest.spyOn(helper, 'clearRefinements');
4552
jest.spyOn(helper, 'addDisjunctiveFacetRefinement');
4653
jest.spyOn(helper, 'getRefinements');
@@ -61,9 +68,14 @@ describe('ratingMenu()', () => {
6168
});
6269

6370
it('configures the underlying disjunctive facet', () => {
64-
expect(widget.getConfiguration()).toEqual({
65-
disjunctiveFacets: ['anAttrName'],
66-
});
71+
expect(widget.getConfiguration(new SearchParameters())).toEqual(
72+
new SearchParameters({
73+
disjunctiveFacets: ['anAttrName'],
74+
disjunctiveFacetsRefinements: {
75+
anAttrName: [],
76+
},
77+
})
78+
);
6779
});
6880

6981
it('calls twice render(<RefinementList props />, container)', () => {
@@ -140,7 +152,11 @@ describe('ratingMenu()', () => {
140152
attribute,
141153
cssClasses: { body: ['body', 'cx'] },
142154
});
143-
const _helper = jsHelper({}, '', _widget.getConfiguration({}));
155+
const _helper = jsHelper(
156+
{},
157+
'',
158+
_widget.getConfiguration(new SearchParameters({}))
159+
);
144160
_helper.search = jest.fn();
145161

146162
_widget.init({

0 commit comments

Comments
 (0)