Skip to content

Commit c7a10cf

Browse files
authored
Fix createStaticFacet triggering NO_VALUE (#164)
* Fix createStaticFacet triggering NO_VALUE * Unit test createFacet to check parity
1 parent 585bff9 commit c7a10cf

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

packages/@react-facet/core/src/facet/createFacet.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,12 @@ describe('setWithCallback', () => {
235235
expect(listenerMock).not.toHaveBeenCalled()
236236
})
237237
})
238+
239+
it('avoids triggering the listener if initialized with NO_VALUE', () => {
240+
const update = jest.fn()
241+
const initialValue = NO_VALUE
242+
const mock = createFacet({ initialValue })
243+
244+
mock.observe(update)
245+
expect(update).not.toHaveBeenCalled()
246+
})
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1+
import { NO_VALUE } from '../types'
12
import { createStaticFacet } from './createStaticFacet'
23

3-
describe('createStaticFacet', () => {
4-
it(`it can be read but not mutated`, () => {
5-
const initialValue = {}
6-
const mock = createStaticFacet(initialValue)
4+
it('allows reading, but not mutation', () => {
5+
const initialValue = {}
6+
const mock = createStaticFacet(initialValue)
77

8-
expect(mock.get()).toBe(initialValue)
9-
expect('set' in mock).toBe(false)
10-
})
8+
expect(mock.get()).toBe(initialValue)
9+
expect('set' in mock).toBe(false)
10+
})
11+
12+
it('responds with the same value if you observe it and warns you in a non-production environment', () => {
13+
const update = jest.fn()
14+
const initialValue = {}
15+
const mock = createStaticFacet(initialValue)
1116

12-
it(`it responds with the same value if you observe it and warns you in a non-production environment`, () => {
13-
const update = jest.fn()
14-
const initialValue = {}
15-
const mock = createStaticFacet(initialValue)
17+
mock.observe(update)
18+
expect(update).toHaveBeenCalledTimes(1)
19+
expect(update).toHaveBeenCalledWith(initialValue)
1620

17-
mock.observe(update)
18-
expect(update).toHaveBeenCalledTimes(1)
19-
expect(update).toHaveBeenCalledWith(initialValue)
21+
update.mockClear()
22+
23+
mock.observe(update)
24+
expect(update).toHaveBeenCalledTimes(1)
25+
expect(update).toHaveBeenCalledWith(initialValue)
26+
})
2027

21-
update.mockClear()
28+
it('avoids triggering the listener if initialized with NO_VALUE', () => {
29+
const update = jest.fn()
30+
const initialValue = NO_VALUE
31+
const mock = createStaticFacet(initialValue)
2232

23-
mock.observe(update)
24-
expect(update).toHaveBeenCalledTimes(1)
25-
expect(update).toHaveBeenCalledWith(initialValue)
26-
})
33+
mock.observe(update)
34+
expect(update).not.toHaveBeenCalled()
2735
})

packages/@react-facet/core/src/facet/createStaticFacet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Facet } from '../types'
1+
import { Facet, NO_VALUE } from '../types'
22
/**
33
* Creates a nonwritable barebones static facet to be used when you need an initial facet value outside the react context
44
* that's meant to be replaced later by a real facet. Ex: with `createContext()`
@@ -7,7 +7,9 @@ export function createStaticFacet<T>(value: T): Facet<T> {
77
const facet: Facet<T> = {
88
get: () => value,
99
observe: (listener) => {
10-
listener(value)
10+
if (value !== NO_VALUE) {
11+
listener(value)
12+
}
1113
return () => {}
1214
},
1315
}

0 commit comments

Comments
 (0)