forked from reduxjs/react-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDispatch.spec.tsx
62 lines (56 loc) · 2.11 KB
/
useDispatch.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
import React from 'react'
import { createStore } from 'redux'
import { renderHook } from '@testing-library/react-hooks'
import {
Provider as ProviderMock,
useDispatch,
createDispatchHook,
} from '../../src/index'
import type { ProviderProps } from '../../src/'
import type { ReactReduxContextValue } from '../../src/components/Context'
const store = createStore((c: number = 1): number => c + 1)
const store2 = createStore((c: number = 1): number => c + 2)
describe('React', () => {
describe('hooks', () => {
describe('useDispatch', () => {
it("returns the store's dispatch function", () => {
type PropsType = Omit<ProviderProps, 'store'>
const { result } = renderHook(() => useDispatch(), {
wrapper: (props: PropsType) => (
<ProviderMock {...props} store={store} />
),
})
expect(result.current).toBe(store.dispatch)
})
})
describe('createDispatchHook', () => {
it("returns the correct store's dispatch function", () => {
const nestedContext =
React.createContext<ReactReduxContextValue | null>(null)
const useCustomDispatch = createDispatchHook(nestedContext)
const { result } = renderHook(() => useDispatch(), {
// eslint-disable-next-line react/prop-types
wrapper: ({ children, ...props }: ProviderProps) => (
<ProviderMock {...props} store={store}>
<ProviderMock context={nestedContext} store={store2}>
{children}
</ProviderMock>
</ProviderMock>
),
})
expect(result.current).toBe(store.dispatch)
const { result: result2 } = renderHook(() => useCustomDispatch(), {
// eslint-disable-next-line react/prop-types
wrapper: ({ children, ...props }: ProviderProps) => (
<ProviderMock {...props} store={store}>
<ProviderMock context={nestedContext} store={store2}>
{children}
</ProviderMock>
</ProviderMock>
),
})
expect(result2.current).toBe(store2.dispatch)
})
})
})
})