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

Commit

Permalink
fix(connectToggleRefinement): cast currentRefinement to boolean (#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Croissant authored Jul 23, 2019
1 parent 07d6582 commit aaf8043
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('connectToggleRefinement', () => {
expect(actual.currentRefinement).toBe(false);
});

it('expect `currentRefinement` to be `false` when searchState is a string considered falsy', () => {
const props = { attribute: 'shipping', value: true };
const searchState = { toggle: { shipping: 'false' } };
const searchResults = {};

const actual = getProvidedProps(props, searchState, searchResults);

expect(actual.currentRefinement).toBe(false);
});

it('expect `currentRefinement` to be `defaultRefinement`', () => {
const props = {
defaultRefinement: true,
Expand Down Expand Up @@ -406,6 +416,18 @@ describe('connectToggleRefinement', () => {
expect(actual.currentRefinement).toBe(false);
});

it('expect `currentRefinement` to be `false` when searchState is a string considered falsy', () => {
const props = { attribute: 'shipping', value: true };
const searchState = createMultiIndexSearchState({
toggle: { shipping: 'false' },
});
const searchResults = {};

const actual = getProvidedProps(props, searchState, searchResults);

expect(actual.currentRefinement).toBe(false);
});

it('expect `currentRefinement` to be `defaultRefinement`', () => {
const props = {
defaultRefinement: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function getId(props) {

const namespace = 'toggle';

const falsyStrings = ['0', 'false', 'null', 'undefined'];

function getCurrentRefinement(props, searchState, context) {
const currentRefinement = getCurrentRefinementValue(
props,
Expand All @@ -24,10 +26,11 @@ function getCurrentRefinement(props, searchState, context) {
false
);

if (currentRefinement) {
return currentRefinement;
if (falsyStrings.indexOf(currentRefinement) !== -1) {
return false;
}
return false;

return Boolean(currentRefinement);
}

function refine(props, searchState, nextRefinement, context) {
Expand Down

0 comments on commit aaf8043

Please sign in to comment.