-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmapFacets.spec.ts
124 lines (97 loc) · 4.94 KB
/
mapFacets.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { createFacet } from '../facet'
import { NO_VALUE } from '../types'
import { mapFacetsCached, mapFacetsLightweight } from './mapFacets'
describe('mapFacetsCached', () => {
it('caches calls to the mapFunction', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceFacet], mapFunction)
// observe twice
mapFacet.observe(() => {})
mapFacet.observe(() => {})
// check that the map was called just once
expect(mapFunction).toHaveBeenCalledTimes(1)
})
it('caches calls to the mapFunction with equalityCheck', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceFacet], mapFunction, () => () => false)
// observe twice
mapFacet.observe(() => {})
mapFacet.observe(() => {})
// check that the map was called just once
expect(mapFunction).toHaveBeenCalledTimes(1)
})
it('gets NO_VALUE as a value from a single source if it also has NO_VALUE', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: NO_VALUE })
const mapFacet = mapFacetsCached([sourceFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe(NO_VALUE)
})
it('gets NO_VALUE as a value from multiple sources if they also have NO_VALUE', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceAFacet = createFacet({ initialValue: 'initial value' })
const sourceBFacet = createFacet({ initialValue: NO_VALUE })
const mapFacet = mapFacetsCached([sourceAFacet, sourceBFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe(NO_VALUE)
})
it('can get the mapped value from a single source before any subscription', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
})
it('can get the mapped value from multiple sources before any subscription', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceAFacet = createFacet({ initialValue: 'initial value' })
const sourceBFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceAFacet, sourceBFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
})
it('caches calls to the mapFunction through a get call before any subscription, given a single source', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
expect(mapFunction).toHaveBeenCalledTimes(1)
mapFunction.mockClear()
expect(mapFacet.get()).toBe('dummy')
expect(mapFunction).not.toHaveBeenCalled()
})
it('caches calls to the mapFunction though a get call before any subscription, given multiple sources', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceAFacet = createFacet({ initialValue: 'initial value' })
const sourceBFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsCached([sourceAFacet, sourceBFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
expect(mapFunction).toHaveBeenCalledTimes(1)
mapFunction.mockClear()
expect(mapFacet.get()).toBe('dummy')
expect(mapFunction).not.toHaveBeenCalled()
})
})
describe('mapFacetsLightweight', () => {
it('calls the mapFunction on every observe', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsLightweight([sourceFacet], mapFunction)
// observe twice
mapFacet.observe(() => {})
mapFacet.observe(() => {})
// check that the map was called twice (once for each observe)
expect(mapFunction).toHaveBeenCalledTimes(2)
})
it('gets the value mapped from a single source', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsLightweight([sourceFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
})
it('gets the value mapped from multiple sources', () => {
const mapFunction = jest.fn().mockReturnValue('dummy')
const sourceAFacet = createFacet({ initialValue: 'initial value' })
const sourceBFacet = createFacet({ initialValue: 'initial value' })
const mapFacet = mapFacetsLightweight([sourceAFacet, sourceBFacet], mapFunction, () => () => false)
expect(mapFacet.get()).toBe('dummy')
})
})