49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import { test, expect, vi } from 'vitest';
|
|
|
|
import { buildMiddleware } from './middleware.js';
|
|
import { action } from './actions.js';
|
|
|
|
test('buildMiddleware, effects', async () => {
|
|
const effectMock = vi.fn();
|
|
|
|
const mw = buildMiddleware([
|
|
['*', (api) => (next) => (action) => effectMock()],
|
|
]);
|
|
|
|
mw({})(() => {})({});
|
|
|
|
expect(effectMock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
test('buildMiddleware, augmented api', async () => {
|
|
const myAction = action('myAction');
|
|
|
|
const mw = buildMiddleware(
|
|
[
|
|
[
|
|
'*',
|
|
(api) => (next) => (action) => {
|
|
expect(api.getState.mySelector()).toEqual(13);
|
|
api.dispatch(myAction());
|
|
next();
|
|
},
|
|
],
|
|
],
|
|
{
|
|
myAction,
|
|
},
|
|
{
|
|
mySelector: (state) => state?.selected,
|
|
},
|
|
);
|
|
|
|
const dispatch = vi.fn();
|
|
const getState = vi.fn(() => ({ selected: 13 }));
|
|
const next = vi.fn();
|
|
|
|
mw({ dispatch, getState })(next)(myAction());
|
|
|
|
expect(next).toHaveBeenCalledOnce();
|
|
expect(dispatch).toHaveBeenCalledWith(myAction());
|
|
});
|