-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathuseContext.test.tsx
45 lines (31 loc) · 1.25 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
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 value in context when props are updated', () => {
const TestContext = createContext('foo')
const wrapper: React.FC<{ contextValue: string }> = ({ contextValue, children }) => (
<TestContext.Provider value={contextValue}>{children}</TestContext.Provider>
)
const { result, hydrate, rerender } = renderHook(() => useContext(TestContext), {
wrapper,
initialProps: { contextValue: 'bar' }
})
hydrate()
rerender({ contextValue: 'baz' })
expect(result.current).toBe('baz')
})
})