-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMap.spec.tsx
165 lines (126 loc) · 3.83 KB
/
Map.spec.tsx
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React from 'react'
import { act, render } from '@react-facet/dom-fiber-testing-library'
import { createFacet } from '../facet'
import { Facet } from '../types'
import { useFacetEffect, useFacetMap } from '../hooks'
import { Map } from '.'
it('renders all items in a Facet of array', () => {
type Input = { a: string }
const data = createFacet({
initialValue: [{ a: '1' }, { a: '2' }, { a: '3' }, { a: '4' }, { a: '5' }],
})
const ExampleContent = ({ item, index }: { item: Facet<Input>; index: number }) => {
return (
<span>
<fast-text text={useFacetMap(({ a }) => a, [], [item])} />
<span>{index}</span>
</span>
)
}
const inputEqualityCheck = () => {
const previous: Partial<Input> = {}
return (current: Input) => {
if (current.a === previous.a) {
return true
}
previous.a = current.a
return false
}
}
const Example = () => {
return (
<Map array={data} equalityCheck={inputEqualityCheck}>
{(item, index) => <ExampleContent item={item} index={index} />}
</Map>
)
}
const scenario = <Example />
const { container } = render(scenario)
expect(container).toMatchSnapshot()
})
it('unmounts components when the array reduces in size', () => {
interface Item {
value: string
}
const data = createFacet<Item[]>({
initialValue: [{ value: '1' }, { value: '2' }, { value: '3' }, { value: '4' }, { value: '5' }],
})
const Item = ({ item, index }: { item: Facet<Item>; index: number }) => (
<span>
<fast-text text={useFacetMap(({ value }) => value, [], [item])} />
<span>{index}</span>
</span>
)
const itemEqualityCheck = () => {
const previous: Partial<Item> = {}
return (current: Item) => {
if (current.value === previous.value) {
return true
}
previous.value = current.value
return false
}
}
const Example = () => {
return (
<Map array={data} equalityCheck={itemEqualityCheck}>
{(item, index) => <Item item={item} index={index} />}
</Map>
)
}
const scenario = <Example />
const { container } = render(scenario)
expect(container).toMatchSnapshot()
data.set([{ value: '1' }, { value: '2' }])
expect(container).toMatchSnapshot()
})
it('updates only items that have changed', () => {
type Input = { a: string }
const data = createFacet({
initialValue: [{ a: '1' }, { a: '2' }, { a: '3' }, { a: '4' }, { a: '5' }],
})
const mock = jest.fn()
const ExampleContent = ({ item }: { item: Facet<Input> }) => {
useFacetEffect(mock, [], [item])
return null
}
const inputEqualityCheck = () => {
const previous: Partial<Input> = {}
return (current: Input) => {
if (current.a === previous.a) {
return true
}
previous.a = current.a
return false
}
}
const Example = () => {
return (
<Map array={data} equalityCheck={inputEqualityCheck}>
{(item) => <ExampleContent item={item} />}
</Map>
)
}
const scenario = <Example />
render(scenario)
expect(mock).toHaveBeenCalledTimes(5)
mock.mockClear()
act(() => {
data.set([{ a: '6' }, { a: '2' }, { a: '3' }, { a: '4' }, { a: '5' }])
})
expect(mock).toHaveBeenCalledTimes(1)
expect(mock).toHaveBeenCalledWith({ a: '6' })
})
it('provides the length of the array', () => {
const data = createFacet({
initialValue: [{ a: '1' }, { a: '2' }, { a: '3' }],
})
const ExampleContent = ({ index, length }: { index: number; length: number }) => {
return <>{length === index + 1 && <div data-testid={'length'}>{length}</div>}</>
}
const Example = () => {
return <Map array={data}>{(_, index, length) => <ExampleContent index={index} length={length} />}</Map>
}
const { getByTestId } = render(<Example />)
expect(getByTestId('length')).toHaveTextContent('3')
})