Skip to content

Commit 1213b40

Browse files
pirelenitomarlonicus
authored andcommitted
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 065e744 commit 1213b40

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
@@ -228,7 +228,7 @@ it('does not trigger a re-render when changing a facet from undefined to undefin
228228
})
229229

230230
it('supports custom equality checks', () => {
231-
const value = {}
231+
const value = { prop: 'initial' }
232232
const demoFacet = createFacet({ initialValue: value })
233233

234234
// Dummy equality check that always returns its not equal
@@ -267,4 +267,18 @@ it('supports custom equality checks', () => {
267267
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
268268
expect(check).toHaveBeenCalledWith(value) // passing the value (which should be the same)
269269
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render
270+
271+
jest.clearAllMocks()
272+
273+
const newValue = { prop: 'new' }
274+
275+
// If we update with a new object,
276+
act(() => {
277+
demoFacet.set(newValue)
278+
})
279+
280+
expect(equalityCheck).toHaveBeenCalledTimes(0) // equality check was already initialized
281+
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
282+
expect(check).toHaveBeenCalledWith(newValue) // passing the new value
283+
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render
270284
})

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

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

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

0 commit comments

Comments
 (0)