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