-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathuseContext.test.tsx
63 lines (43 loc) · 1.66 KB
/
useContext.test.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
import React, { createContext, useContext } from 'react'
import { renderHook } from '..'
describe('useContext tests', () => {
test('should get default value from context', () => {
const TestContext = createContext('foo')
const { result } = renderHook(() => useContext(TestContext))
const value = result.current
expect(value).toBe('foo')
})
test('should get value from context provider', () => {
const TestContext = createContext('foo')
const wrapper: React.FC = ({ children }) => (
<TestContext.Provider value="bar">{children}</TestContext.Provider>
)
const { result } = renderHook(() => useContext(TestContext), { wrapper })
expect(result.current).toBe('bar')
})
test('should update mutated value in context', () => {
const TestContext = createContext('foo')
const value = { current: 'bar' }
const wrapper: React.FC = ({ children }) => (
<TestContext.Provider value={value.current}>{children}</TestContext.Provider>
)
const { result, rerender } = renderHook(() => useContext(TestContext), { wrapper })
value.current = 'baz'
rerender()
expect(result.current).toBe('baz')
})
test('should update value in context when props are updated', () => {
const TestContext = createContext('foo')
const wrapper: React.FC<{ current: string }> = ({ current, children }) => (
<TestContext.Provider value={current}>{children}</TestContext.Provider>
)
const { result, rerender } = renderHook(() => useContext(TestContext), {
wrapper,
initialProps: {
current: 'bar'
}
})
rerender({ current: 'baz' })
expect(result.current).toBe('baz')
})
})