Skip to content

Commit 8b40175

Browse files
authored
Fix support for updating equality checks of useFacetWrapMemo (#163)
1 parent 1c10485 commit 8b40175

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cSpell.words": [
33
"cohinline",
4+
"facetified",
45
"Gameface",
56
"gamepad"
67
],

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useFacetWrapMemo } from './useFacetWrapMemo'
44
import { useFacetEffect } from './useFacetEffect'
55
import { useFacetMap } from './useFacetMap'
66
import { createFacet } from '../facet'
7-
import { FacetProp, NO_VALUE, Value } from '../types'
7+
import { EqualityCheck, FacetProp, NO_VALUE, Value } from '../types'
88

99
it('wraps a value, updating the facet when it changes', () => {
1010
const mock = jest.fn()
@@ -203,3 +203,36 @@ describe('does not trigger effect updates on re-renders for the same value', ()
203203
testEffectUpdatesOnStaticValue({ key: 'value' }, false)
204204
})
205205
})
206+
207+
it('allows changing the equality check', () => {
208+
const mock = jest.fn()
209+
210+
const ComponentWithFacetEffect: React.FC<{ value: string; equalityCheck?: EqualityCheck<string> }> = ({
211+
value,
212+
equalityCheck,
213+
}) => {
214+
const facetValue = useFacetWrapMemo(value, equalityCheck)
215+
216+
useFacetEffect(
217+
(value) => {
218+
mock(value)
219+
},
220+
[],
221+
[facetValue],
222+
)
223+
return <span />
224+
}
225+
226+
const alwaysFalse = () => () => false
227+
228+
const dom = render(<ComponentWithFacetEffect value="value" />)
229+
expect(mock).toHaveBeenCalledTimes(1)
230+
expect(mock).toHaveBeenCalledWith('value')
231+
232+
mock.mockClear()
233+
234+
// as we change the equality check, it should re-initialize the inline facet, causing an update
235+
dom.rerender(<ComponentWithFacetEffect value="value" equalityCheck={alwaysFalse} />)
236+
expect(mock).toHaveBeenCalledTimes(1)
237+
expect(mock).toHaveBeenCalledWith('value')
238+
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function useFacetWrapMemo<T extends Value>(
2020
const inlineFacet = useMemo(
2121
() => createFacet<T>({ initialValue: isFacet(prop) ? prop.get() : prop, equalityCheck }),
2222
// eslint-disable-next-line react-hooks/exhaustive-deps
23-
[],
23+
[equalityCheck],
2424
)
2525

2626
useEffect(() => {

0 commit comments

Comments
 (0)