Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useFacetUnwrap support for custom equality check #140

Merged
merged 4 commits into from
Jul 25, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/@react-facet/core/src/hooks/useFacetUnwrap.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ it('does not trigger a re-render when changing a facet from undefined to undefin
})

it('supports custom equality checks', () => {
const value = {}
const value = { prop: 'initial' }
const demoFacet = createFacet({ initialValue: value })

// Dummy equality check that always returns its not equal
Expand Down Expand Up @@ -258,4 +258,18 @@ it('supports custom equality checks', () => {
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
expect(check).toHaveBeenCalledWith(value) // passing the value (which should be the same)
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render

jest.clearAllMocks()

const newValue = { prop: 'new' }

// If we update with a new object,
act(() => {
demoFacet.set(newValue)
})

expect(equalityCheck).toHaveBeenCalledTimes(0) // equality check was already initialized
expect(check).toHaveBeenCalledTimes(1) // but the check should be executed
expect(check).toHaveBeenCalledWith(newValue) // passing the new value
expect(renderedMock).toHaveBeenCalledTimes(1) // and since the equality check always returns "false", we have a render
})
2 changes: 1 addition & 1 deletion packages/@react-facet/core/src/hooks/useFacetUnwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useFacetUnwrap<T extends Value>(
return { value }
}

if (previousValue !== NO_VALUE && isEqual(previousValue)) {
if (previousValue !== NO_VALUE && isEqual(value)) {
return previousState
}

Expand Down
Loading