Skip to content

Commit

Permalink
Fix for memo wrapping a facet with NO_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
pirelenito committed Jan 9, 2024
1 parent 9cae8c2 commit 1108578
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion packages/@react-facet/core/src/hooks/useFacetWrapMemo.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useFacetWrapMemo } from './useFacetWrapMemo'
import { useFacetEffect } from './useFacetEffect'
import { useFacetMap } from './useFacetMap'
import { createFacet } from '../facet'
import { FacetProp, Value } from '..'
import { FacetProp, NO_VALUE, Value } from '../types'

it('wraps a value, updating the facet when it changes', () => {
const mock = jest.fn()
Expand Down Expand Up @@ -78,6 +78,32 @@ it('forwards a facet', () => {
expect(mock).toHaveBeenCalledWith('changed')
})

it('forwards a facet with NO_VAUE', () => {
const demoFacet = createFacet<string>({ initialValue: NO_VALUE })
const mock = jest.fn()

const ComponentWithFacetEffect: React.FC = () => {
const facetifiedValue = useFacetWrapMemo(demoFacet)
useFacetEffect(
(value) => {
mock(value)
},
[],
[facetifiedValue],
)
return <span />
}

// On first render, it should not call the effect, as the wrapped facet has no value
render(<ComponentWithFacetEffect />)
expect(mock).not.toHaveBeenCalled()

mock.mockClear()
demoFacet.set('changed')
expect(mock).toHaveBeenCalledTimes(1)
expect(mock).toHaveBeenCalledWith('changed')
})

it('updates correctly if the facet instance change (ex: via a useFacetMap)', () => {
const demoFacet = createFacet({ initialValue: 'value' })

Expand Down
2 changes: 1 addition & 1 deletion packages/@react-facet/core/src/hooks/useFacetWrapMemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function useFacetWrapMemo<T extends Value>(
* facet value via the setter below.
*/
const inlineFacet = useMemo(
() => createFacet<T>({ initialValue: prop as T, equalityCheck }),
() => createFacet<T>({ initialValue: isFacet(prop) ? prop.get() : prop, equalityCheck }),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
)
Expand Down

0 comments on commit 1108578

Please sign in to comment.