mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-09 09:47:28 +03:00
Created redux test
This commit is contained in:
parent
f04aece7df
commit
6696fb13d6
2 changed files with 66 additions and 5 deletions
|
@ -1,17 +1,17 @@
|
||||||
import { Action } from 'redux';
|
import { Action } from 'redux';
|
||||||
|
|
||||||
type ActionDispatcher<State, AT> = (currentState: State, action: AT) => State;
|
type ActionHandler<State, AT> = (currentState: State, action: AT) => State;
|
||||||
type ActionDispatcherMap<State, AT> = Record<string, ActionDispatcher<State, AT>>;
|
type ActionHandlerMap<State, AT> = Record<string, ActionHandler<State, AT>>;
|
||||||
|
|
||||||
export const buildReducer = <State, AT extends Action>(map: ActionDispatcherMap<State, AT>, initialState: State) => (
|
export const buildReducer = <State, AT extends Action>(map: ActionHandlerMap<State, AT>, initialState: State) => (
|
||||||
state: State | undefined,
|
state: State | undefined,
|
||||||
action: AT,
|
action: AT,
|
||||||
): State => {
|
): State => {
|
||||||
const { type } = action;
|
const { type } = action;
|
||||||
const actionDispatcher = map[type];
|
const actionHandler = map[type];
|
||||||
const currentState = state ?? initialState;
|
const currentState = state ?? initialState;
|
||||||
|
|
||||||
return actionDispatcher ? actionDispatcher(currentState, action) : currentState;
|
return actionHandler ? actionHandler(currentState, action) : currentState;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
|
export const buildActionCreator = <T extends string>(type: T) => (): Action<T> => ({ type });
|
||||||
|
|
61
test/utils/helpers/redux.test.ts
Normal file
61
test/utils/helpers/redux.test.ts
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import { Action } from 'redux';
|
||||||
|
import { buildActionCreator, buildReducer } from '../../../src/utils/helpers/redux';
|
||||||
|
|
||||||
|
describe('redux', () => {
|
||||||
|
beforeEach(jest.clearAllMocks);
|
||||||
|
|
||||||
|
describe('buildActionCreator', () => {
|
||||||
|
it.each([
|
||||||
|
[ 'foo', { type: 'foo' }],
|
||||||
|
[ 'bar', { type: 'bar' }],
|
||||||
|
[ 'something', { type: 'something' }],
|
||||||
|
])('returns an action creator', (type, expected) => {
|
||||||
|
const actionCreator = buildActionCreator(type);
|
||||||
|
|
||||||
|
expect(actionCreator).toBeInstanceOf(Function);
|
||||||
|
expect(actionCreator()).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('buildReducer', () => {
|
||||||
|
const fooActionHandler = jest.fn(() => 'foo result');
|
||||||
|
const barActionHandler = jest.fn(() => 'bar result');
|
||||||
|
const initialState = 'initial state';
|
||||||
|
let reducer: Function;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
reducer = buildReducer<string, Action>({
|
||||||
|
foo: fooActionHandler,
|
||||||
|
bar: barActionHandler,
|
||||||
|
}, initialState);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a reducer which returns initial state when provided with unknown action', () => {
|
||||||
|
expect(reducer(undefined, { type: 'unknown action' })).toEqual(initialState);
|
||||||
|
expect(fooActionHandler).not.toHaveBeenCalled();
|
||||||
|
expect(barActionHandler).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[ 'foo', 'foo result', fooActionHandler, barActionHandler ],
|
||||||
|
[ 'bar', 'bar result', barActionHandler, fooActionHandler ],
|
||||||
|
])(
|
||||||
|
'returns a reducer which calls corresponding action handler',
|
||||||
|
(type, expected, invokedActionHandler, notInvokedActionHandler) => {
|
||||||
|
expect(reducer(undefined, { type })).toEqual(expected);
|
||||||
|
expect(invokedActionHandler).toHaveBeenCalled();
|
||||||
|
expect(notInvokedActionHandler).not.toHaveBeenCalled();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
[ undefined, initialState ],
|
||||||
|
[ 'foo', 'foo' ],
|
||||||
|
[ 'something', 'something' ],
|
||||||
|
])('returns a reducer which calls action handler with provided state or initial', (state, expected) => {
|
||||||
|
reducer(state, { type: 'foo' });
|
||||||
|
|
||||||
|
expect(fooActionHandler).toHaveBeenCalledWith(expected, expect.anything());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue