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

fix(connectToggleRefinement): cast currentRefinement to boolean #2701

Merged
merged 3 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have wrote the condition on the 'true' that would include all falsy values.

if (typeof currentRefinement === 'string') {
  return currentRefinement === 'true';
}

return false;
}
return false;

return Boolean(currentRefinement);
}

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