-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.test.ts
More file actions
124 lines (96 loc) · 3.1 KB
/
client.test.ts
File metadata and controls
124 lines (96 loc) · 3.1 KB
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 { act, renderHook } from '@testing-library/react-hooks'
import createStore from '.'
describe('createStore', () => {
describe('initial state', () => {
test('returns default state', () => {
const useExampleStore = createStore()
const {
result: {
current: [state],
},
} = renderHook(() => useExampleStore())
expect(state).toBe(undefined)
})
test('returns provided state', () => {
const initialState = ['a', 'b', 'c']
const useExampleStore = createStore({ initialState })
const {
result: {
current: [state],
},
} = renderHook(() => useExampleStore())
expect(state).toBe(initialState)
})
})
describe('provides basic state updater', () => {
const initialState = ['a', 'b', 'c']
const newState = ['c', 'b', 'a']
const useExampleStore = createStore({ initialState })
test('is a function', () => {
const { result } = renderHook(() => useExampleStore())
expect(typeof result.current[1]).toBe('function')
})
test('sets state', () => {
const { result } = renderHook(() => useExampleStore())
act(() => {
result.current[1](newState)
})
expect(result.current[0]).toBe(newState)
})
test('updates states', () => {
const { result } = renderHook(() => useExampleStore())
act(() => {
result.current[1]((prevState: []) => ['d', ...prevState])
})
expect(result.current[0]).toEqual(['d', ...newState])
})
})
describe('subscriptions', () => {
interface State {
name: string
age: number
}
test('subscribes to updates', () => {
const useExampleStore = createStore<State>({
initialState: { age: 30, name: 'John' },
})
const a = renderHook(() => useExampleStore())
const b = renderHook(() => useExampleStore())
const c = renderHook(() => useExampleStore(({ age }) => ({ age })))
const cInitial = c.result.current[0]
expect(b.result.current[0]).toBe(a.result.current[0])
act(() => {
a.result.current[1]((prevState: State) => ({
...prevState,
name: 'Dave',
}))
})
expect(a.result.current[0]).toEqual({ age: 30, name: 'Dave' })
expect(b.result.current[0]).toEqual({ age: 30, name: 'Dave' })
expect(c.result.current[0]).toBe(cInitial)
act(() => {
a.result.current[1]((prevState: State) => ({
...prevState,
age: 33,
}))
})
expect(c.result.current[0]).toEqual({ age: 33 })
})
test('unsubscribes to updates', () => {
const useExampleStore = createStore<State>({
initialState: { age: 30, name: 'John' },
})
const a = renderHook(() => useExampleStore())
const b = renderHook(() => useExampleStore())
expect(b.result.current[0]).toBe(a.result.current[0])
act(() => {
b.unmount()
a.result.current[1]((prevState: State) => ({
...prevState,
age: 33,
}))
})
expect(b.result.current[0]).not.toBe(a.result.current[0])
})
})
})