| 1 |
import * as React from 'react' |
| 2 |
import { createDynamicMiddleware } from '../react' |
| 3 |
import { configureStore } from '../../configureStore' |
| 4 |
import { makeProbeableMiddleware, probeMiddleware } from './index.test' |
| 5 |
import { render } from '@testing-library/react' |
| 6 |
import type { Dispatch } from 'redux' |
| 7 |
import type { ReactReduxContextValue } from 'react-redux' |
| 8 |
import { Provider } from 'react-redux' |
| 9 |
|
| 10 |
const staticMiddleware = makeProbeableMiddleware(1) |
| 11 |
|
| 12 |
describe('createReactDynamicMiddleware', () => { |
| 13 |
describe('createDispatchWithMiddlewareHook', () => { |
| 14 |
it('injects middleware upon creation', () => { |
| 15 |
const dynamicInstance = createDynamicMiddleware() |
| 16 |
const store = configureStore({ |
| 17 |
reducer: () => 0, |
| 18 |
middleware: (gDM) => |
| 19 |
gDM().prepend(dynamicInstance.middleware).concat(staticMiddleware), |
| 20 |
}) |
| 21 |
// normal, pre-inject |
| 22 |
expect(store.dispatch(probeMiddleware(2))).toEqual(probeMiddleware(2)) |
| 23 |
// static |
| 24 |
expect(store.dispatch(probeMiddleware(1))).toBe(1) |
| 25 |
|
| 26 |
const useDispatch = dynamicInstance.createDispatchWithMiddlewareHook( |
| 27 |
makeProbeableMiddleware(2), |
| 28 |
) |
| 29 |
|
| 30 |
// injected |
| 31 |
expect(store.dispatch(probeMiddleware(2))).toBe(2) |
| 32 |
}) |
| 33 |
|
| 34 |
it('returns dispatch', () => { |
| 35 |
const dynamicInstance = createDynamicMiddleware() |
| 36 |
const store = configureStore({ |
| 37 |
reducer: () => 0, |
| 38 |
middleware: (gDM) => |
| 39 |
gDM().prepend(dynamicInstance.middleware).concat(staticMiddleware), |
| 40 |
}) |
| 41 |
|
| 42 |
const useDispatch = dynamicInstance.createDispatchWithMiddlewareHook( |
| 43 |
makeProbeableMiddleware(2), |
| 44 |
) |
| 45 |
|
| 46 |
let dispatch: Dispatch | undefined |
| 47 |
function Component() { |
| 48 |
dispatch = useDispatch() |
| 49 |
|
| 50 |
return null |
| 51 |
} |
| 52 |
render(<Component />, { |
| 53 |
wrapper: ({ children }) => ( |
| 54 |
<Provider store={store}>{children}</Provider> |
| 55 |
), |
| 56 |
}) |
| 57 |
expect(dispatch).toBe(store.dispatch) |
| 58 |
}) |
| 59 |
}) |
| 60 |
describe('createDispatchWithMiddlewareHookFactory', () => { |
| 61 |
it('returns the correct store dispatch', () => { |
| 62 |
const dynamicInstance = createDynamicMiddleware() |
| 63 |
const store = configureStore({ |
| 64 |
reducer: () => 0, |
| 65 |
middleware: (gDM) => |
| 66 |
gDM().prepend(dynamicInstance.middleware).concat(staticMiddleware), |
| 67 |
}) |
| 68 |
const store2 = configureStore({ |
| 69 |
reducer: () => '', |
| 70 |
middleware: (gDM) => |
| 71 |
gDM().prepend(dynamicInstance.middleware).concat(staticMiddleware), |
| 72 |
}) |
| 73 |
|
| 74 |
const context = React.createContext<ReactReduxContextValue | null>(null) |
| 75 |
|
| 76 |
const createDispatchWithMiddlewareHook = |
| 77 |
dynamicInstance.createDispatchWithMiddlewareHookFactory(context) |
| 78 |
|
| 79 |
const useDispatch = createDispatchWithMiddlewareHook( |
| 80 |
makeProbeableMiddleware(2), |
| 81 |
) |
| 82 |
|
| 83 |
let dispatch: Dispatch | undefined |
| 84 |
function Component() { |
| 85 |
dispatch = useDispatch() |
| 86 |
|
| 87 |
return null |
| 88 |
} |
| 89 |
render(<Component />, { |
| 90 |
wrapper: ({ children }) => ( |
| 91 |
<Provider store={store}> |
| 92 |
<Provider context={context} store={store2}> |
| 93 |
{children} |
| 94 |
</Provider> |
| 95 |
</Provider> |
| 96 |
), |
| 97 |
}) |
| 98 |
expect(dispatch).toBe(store2.dispatch) |
| 99 |
}) |
| 100 |
}) |
| 101 |
}) |