Skip to content

Commit a10baad

Browse files
authored
Fix useFacetUnwrap support for custom equality check (#140)
* Fix test catching the bug * Properly checks if the new value is the same as the previous one The prior implementation would always compare the previous with itself * Refactor so that in never calls setState, unless needed * Revert "Refactor so that in never calls setState, unless needed" This reverts commit dd53972.
1 parent 46bd639 commit a10baad

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/@react-facet/core/src/hooks/useFacetUnwrap.spec.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ it('does not trigger a re-render when changing a facet from undefined to undefin
219219
})
220220

221221
it('supports custom equality checks', () => {
222-
const value = {}
222+
const value = { prop: 'initial' }
223223
const demoFacet = createFacet({ initialValue: value })
224224

225225
// Dummy equality check that always returns its not equal
@@ -258,4 +258,18 @@ it('supports custom equality checks', () => {
258258
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
259259
expect(check).toHaveBeenCalledWith(value) // passing the value (which should be the same)
260260
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render
261+
262+
jest.clearAllMocks()
263+
264+
const newValue = { prop: 'new' }
265+
266+
// If we update with a new object,
267+
act(() => {
268+
demoFacet.set(newValue)
269+
})
270+
271+
expect(equalityCheck).toHaveBeenCalledTimes(0) // equality check was already initialized
272+
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
273+
expect(check).toHaveBeenCalledWith(newValue) // passing the new value
274+
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render
261275
})

packages/@react-facet/core/src/hooks/useFacetUnwrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function useFacetUnwrap<T extends Value>(
6060
return { value }
6161
}
6262

63-
if (previousValue !== NO_VALUE && isEqual(previousValue)) {
63+
if (previousValue !== NO_VALUE && isEqual(value)) {
6464
return previousState
6565
}
6666

0 commit comments

Comments
 (0)