Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / store / optimistic / selectors / select-optimistic-list.spec.ts
blob964f50f20aa4465144324ef59748a39ecbd04b46
1 import type { OptimisticReducersMapObject, WrappedOptimisticState } from '../types';
2 import type { TestState } from '../utils/testing.utils';
3 import {
4     createTestDeterministicAction,
5     createTestOptimisticHistoryItem,
6     createTestReducer,
7 } from '../utils/testing.utils';
8 import withOptimistic from '../with-optimistic';
9 import selectOptimisticList from './select-optimistic-list';
11 describe('selectOptimisticList', () => {
12     test('should map optimistic list items to optimistic state', () => {
13         type TestItem = { value: number };
15         const state = {
16             testOptimistic: {
17                 items: [{ value: 1 }, { value: 2 }, { value: 3 }],
18                 optimistic: {
19                     checkpoint: { items: [] },
20                     history: [
21                         createTestOptimisticHistoryItem('add', { value: 1 }, true),
22                         createTestDeterministicAction('add', { value: 2 }),
23                         createTestOptimisticHistoryItem('add', { value: 3 }, false),
24                     ],
25                 },
26             } as WrappedOptimisticState<TestState<TestItem>>,
27         };
29         const reducerMap: OptimisticReducersMapObject<typeof state> = {
30             testOptimistic: withOptimistic([], createTestReducer<TestItem>()).reducer,
31         };
32         const result = selectOptimisticList(reducerMap)(
33             (state) => state.testOptimistic.items,
34             (item) => item.value
35         )(state);
37         expect(result).toEqual([
38             { value: 1, failed: true, optimistic: true },
39             { value: 2, failed: false, optimistic: false },
40             { value: 3, failed: false, optimistic: true },
41         ]);
42     });
43 });