Skip to content

Commit

Permalink
fix(toggleRefinement): update lifecycle state (#3993)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored and Haroenv committed Oct 23, 2019
1 parent ffadf64 commit f1beff6
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
});

const config = widget.getConfiguration();
expect(config).toEqual({
disjunctiveFacets: [attribute],
});
const config = widget.getConfiguration(new SearchParameters({}));
expect(config).toEqual(
new SearchParameters({
disjunctiveFacets: [attribute],
disjunctiveFacetsRefinements: {
[attribute]: [],
},
})
);

const helper = jsHelper({}, '', config);
helper.search = jest.fn();
Expand Down Expand Up @@ -167,7 +172,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
});

const helper = jsHelper({}, '', widget.getConfiguration());
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
);
helper.search = jest.fn();

widget.init({
Expand All @@ -178,9 +187,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi

{
// first rendering
expect(helper.state.disjunctiveFacetsRefinements[attribute]).toEqual(
undefined
);
expect(helper.state.disjunctiveFacetsRefinements[attribute]).toEqual([]);
const renderOptions =
rendering.mock.calls[rendering.mock.calls.length - 1][0];
const { refine, value } = renderOptions;
Expand Down Expand Up @@ -309,7 +316,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
off: 'false',
});

const helper = jsHelper({}, '', widget.getConfiguration());
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
);
helper.search = jest.fn();

widget.init({
Expand Down Expand Up @@ -463,7 +474,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
off: false,
});

const helper = jsHelper({}, '', widget.getConfiguration());
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
);
widget.init({ helper, state: helper.state });

expect(helper.state.disjunctiveFacetsRefinements).toEqual(
Expand All @@ -480,7 +495,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
off: true,
});

const helper = jsHelper({}, '', widget.getConfiguration());
const helper = jsHelper(
{},
'',
widget.getConfiguration(new SearchParameters({}))
);
widget.init({ helper, state: helper.state });

expect(helper.state.disjunctiveFacetsRefinements).toEqual(
Expand All @@ -500,12 +519,18 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
on: false,
});

const helper = jsHelper({ search() {} }, '', widget.getConfiguration());
const helper = jsHelper(
{ search() {} },
'',
widget.getConfiguration(new SearchParameters({}))
);
helper.search = jest.fn();

widget.init({ helper, state: helper.state });

expect(helper.state.disjunctiveFacetsRefinements).toEqual({});
expect(helper.state.disjunctiveFacetsRefinements).toEqual({
whatever: [],
});

widget.render({
results: new SearchResults(helper.state, [
Expand Down Expand Up @@ -533,6 +558,82 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
);
});

describe('getConfiguration', () => {
test('returns initial search parameters', () => {
const rendering = jest.fn();
const makeWidget = connectToggleRefinement(rendering);

const attribute = 'freeShipping';
const widget = makeWidget({
attribute,
});

expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
new SearchParameters({
disjunctiveFacets: [attribute],
disjunctiveFacetsRefinements: {
freeShipping: [],
},
})
);
});
});

describe('dispose', () => {
test('calls the unmount function', () => {
const render = jest.fn();
const unmount = jest.fn();
const makeWidget = connectToggleRefinement(render, unmount);
const helper = jsHelper({}, '', {});
helper.search = jest.fn();

const attribute = 'freeShipping';
const widget = makeWidget({
attribute,
});

widget.dispose({ state: helper.state });

expect(unmount).toHaveBeenCalledTimes(1);
});

test('resets the state', () => {
const render = jest.fn();
const makeWidget = connectToggleRefinement(render);
const indexName = 'indexName';
const helper = jsHelper({}, indexName, {
disjunctiveFacets: ['freeShipping'],
disjunctiveFacetsRefinements: {
freeShipping: ['true'],
},
});
helper.search = jest.fn();

const attribute = 'freeShipping';
const widget = makeWidget({
attribute,
});

expect(helper.state).toEqual(
new SearchParameters({
index: indexName,
disjunctiveFacets: ['freeShipping'],
disjunctiveFacetsRefinements: {
freeShipping: ['true'],
},
})
);

const nextState = widget.dispose({ state: helper.state });

expect(nextState).toEqual(
new SearchParameters({
index: indexName,
})
);
});
});

describe('routing', () => {
const getInitializedWidget = (config = {}) => {
const rendering = jest.fn();
Expand All @@ -544,7 +645,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
...config,
});

const initialConfig = widget.getConfiguration({});
const initialConfig = widget.getConfiguration(new SearchParameters({}));
const helper = jsHelper({}, '', initialConfig);
helper.search = jest.fn();

Expand Down
17 changes: 8 additions & 9 deletions src/connectors/toggleRefinement/connectToggleRefinement.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ export default function connectToggleRefinement(renderFn, unmountFn = noop) {
return {
$$type: 'ais.toggleRefinement',

getConfiguration() {
return {
getConfiguration(state) {
return state.setQueryParameters({
disjunctiveFacets: [attribute],
};
disjunctiveFacetsRefinements: {
[attribute]: [],
},
});
},

_toggleRefinement(helper, { isRefined } = {}) {
Expand Down Expand Up @@ -193,7 +196,7 @@ export default function connectToggleRefinement(renderFn, unmountFn = noop) {
render({ helper, results, state, instantSearchInstance }) {
const isRefined = helper.state.isDisjunctiveFacetRefined(attribute, on);
const offValue = off === undefined ? false : off;
const allFacetValues = results.getFacetValues(attribute);
const allFacetValues = results.getFacetValues(attribute) || [];

const onData = find(
allFacetValues,
Expand Down Expand Up @@ -248,11 +251,7 @@ export default function connectToggleRefinement(renderFn, unmountFn = noop) {
dispose({ state }) {
unmountFn();

const nextState = state
.removeDisjunctiveFacetRefinement(attribute)
.removeDisjunctiveFacet(attribute);

return nextState;
return state.removeDisjunctiveFacet(attribute);
},

getWidgetState(uiState, { searchParameters }) {
Expand Down
41 changes: 23 additions & 18 deletions src/widgets/toggleRefinement/__tests__/toggleRefinement-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from 'preact-compat';
import jsHelper from 'algoliasearch-helper';
import jsHelper, { SearchParameters } from 'algoliasearch-helper';
import toggleRefinement from '../toggleRefinement';
import RefinementList from '../../../components/RefinementList/RefinementList';

Expand Down Expand Up @@ -43,9 +43,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
});

it('configures disjunctiveFacets', () => {
expect(widget.getConfiguration()).toEqual({
disjunctiveFacets: ['world!'],
});
expect(widget.getConfiguration(new SearchParameters({}))).toEqual(
new SearchParameters({
disjunctiveFacets: ['world!'],
disjunctiveFacetsRefinements: {
'world!': [],
},
})
);
});

describe('render', () => {
Expand Down Expand Up @@ -88,7 +93,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
/* on: true, off: undefined */
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ helper, state, createURL, instantSearchInstance });
widget.render({ results, helper, state });
widget.render({ results, helper, state });
Expand Down Expand Up @@ -120,7 +125,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
/* on: true, off: undefined */
RefinementList,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ state, helper, createURL, instantSearchInstance });
widget.render({ results, helper, state });
expect(render.mock.calls[0][0]).toMatchSnapshot();
Expand All @@ -143,7 +148,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
/* on: true, off: undefined */
RefinementList,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ state, helper, createURL, instantSearchInstance });
widget.render({ results, helper, state });
widget.render({ results, helper, state });
Expand Down Expand Up @@ -171,7 +176,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
on: 5,
});

const config = widget.getConfiguration();
const config = widget.getConfiguration(new SearchParameters({}));
const altHelper = jsHelper({}, '', config);
altHelper.search = () => {};

Expand Down Expand Up @@ -210,7 +215,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
/* on: true, off: undefined */
RefinementList,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ state, helper, createURL, instantSearchInstance });
widget.render({ results, helper, state });
widget.render({ results, helper, state });
Expand Down Expand Up @@ -241,7 +246,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
/* on: true, off: undefined */
RefinementList,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ state, helper, createURL, instantSearchInstance });
widget.render({ results, helper, state });
widget.render({ results, helper, state });
Expand All @@ -267,7 +272,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
/* on: true, off: undefined */
RefinementList,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
widget.init({ state, helper, createURL, instantSearchInstance });
widget.render({ results, helper, state });
const { refine } = render.mock.calls[0][0].props;
Expand Down Expand Up @@ -308,7 +313,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
/* on: true, off: undefined */
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
const state = {
isDisjunctiveFacetRefined: jest.fn().mockReturnValue(false),
};
Expand All @@ -334,7 +339,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
/* on: true, off: undefined */
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
const state = {
isDisjunctiveFacetRefined: jest.fn().mockReturnValue(true),
};
Expand Down Expand Up @@ -362,7 +367,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
...userValues,
});

const config = widget.getConfiguration();
const config = widget.getConfiguration(new SearchParameters({}));
const altHelper = jsHelper({}, '', config);
altHelper.search = () => {};

Expand Down Expand Up @@ -395,7 +400,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
...userValues,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
const state = {
isDisjunctiveFacetRefined: jest.fn().mockReturnValue(true),
};
Expand Down Expand Up @@ -428,7 +433,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
...userValues,
});
const config = widget.getConfiguration();
const config = widget.getConfiguration(new SearchParameters({}));
const helper = jsHelper({}, '', config);

// When
Expand All @@ -453,7 +458,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
...userValues,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
const state = {
isDisjunctiveFacetRefined: jest.fn().mockReturnValue(true),
};
Expand All @@ -476,7 +481,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
attribute,
...userValues,
});
widget.getConfiguration();
widget.getConfiguration(new SearchParameters({}));
const state = {
isDisjunctiveFacetRefined: () => false,
};
Expand Down

0 comments on commit f1beff6

Please sign in to comment.