1 import type { PayloadAction } from '@reduxjs/toolkit';
2 import type { Action, Reducer } from 'redux';
4 import type { Maybe } from '@proton/pass/types';
6 import type { DeterministicHistoryItem, OptimisticHistoryItem } from '../types';
7 import { HistoryFlag } from '../types';
9 export const createTestAction = <T = any>(type?: string, payload?: T): PayloadAction<Maybe<T>> => ({
10 type: type ?? `action-${Math.random()}`,
14 export const createTestDeterministicAction = <T = any>(type?: string, payload?: T): DeterministicHistoryItem => ({
15 type: HistoryFlag.DETERMINISTIC,
16 action: createTestAction<T>(type, payload),
19 export const createTestOptimisticHistoryItem = (
23 ): OptimisticHistoryItem => {
25 type: HistoryFlag.OPTIMISTIC,
26 id: `id-${Math.random()}`,
27 action: createTestAction(type, payload),
28 ...(failed !== undefined ? { failed } : {}),
32 export type TestState<T = number> = { items: T[] };
34 export const createTestReducer =
35 <T = number>(): Reducer<TestState<T>> =>
36 (state = { items: [] }, action: Action) => {
37 switch (action.type) {
40 items: [...state.items, (action as any).payload],
44 return { items: state.items.filter((value) => value !== (action as any).payload) };
51 export const testReducer = createTestReducer();