Skip to content

Commit

Permalink
Fix array shallow equal return
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogasparin committed Dec 9, 2020
1 parent f643405 commit a502fe8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/utils/__tests__/create-selector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ describe('createMemoizedSelector', () => {
expect(selector).toHaveBeenCalledTimes(2);
expect(result2).toBe(result1);
});

it('should return same result if selector output is a shallow equal array', () => {
const selector = jest.fn((v) => v.foo);
const stateSelector = createMemoizedSelector(selector);

const item = { id: 1 };
const result1 = stateSelector({ foo: [item] });
const result2 = stateSelector({ foo: [item], w: 1 });
expect(selector).toHaveBeenCalledTimes(2);
expect(result2).toBe(result1);
});

it('should return different result if selector output is not a shallow equal array', () => {
const selector = jest.fn((v) => v.foo);
const stateSelector = createMemoizedSelector(selector);

const result1 = stateSelector({ foo: [{ id: 1 }] });
const result2 = stateSelector({ foo: [{ id: 1 }] });
expect(selector).toHaveBeenCalledTimes(2);
expect(result2).not.toBe(result1);
});
});

describe('with created selector', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/shallow-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default function shallowEqual(objA, objB) {
return false;
}
}

return true;
} else {
// do object comparison
let keysA;
Expand Down

0 comments on commit a502fe8

Please sign in to comment.