Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / store / optimistic / selectors / select-is-optimistic.spec.ts
blob21585fee85f984ec6946dcd41286e286d0e8e3a1
1 import { combineReducers } from 'redux';
3 import type { OptimisticReducersMapObject, WrappedOptimisticState } from '../types';
4 import type { TestState } from '../utils/testing.utils';
5 import {
6     createTestAction,
7     createTestDeterministicAction,
8     createTestOptimisticHistoryItem,
9     testReducer,
10 } from '../utils/testing.utils';
11 import withOptimistic from '../with-optimistic';
12 import selectIsOptimistic, { asIfNotOptimistic } from './select-is-optimistic';
14 describe('selector as if is optimistic', () => {
15     describe('asIfNotOptimistic', () => {
16         test('should return empty state object if provided empty reducer map entries', () => {
17             const reducers = { test: testReducer };
19             const rootReducer = combineReducers(reducers);
20             const state = rootReducer({ test: { items: [] } }, createTestAction());
22             const reducerMap = {} as OptimisticReducersMapObject<typeof state>;
23             const result = asIfNotOptimistic(state, reducerMap);
25             expect(result).toEqual({});
26         });
28         test('should only keep the subset state inferred from the reducer map', () => {
29             const state = {
30                 test: { items: [] },
31                 testOptimistic: {
32                     items: [],
33                     optimistic: {
34                         checkpoint: { items: [] },
35                         history: [],
36                     },
37                 } as WrappedOptimisticState<TestState>,
38             };
40             const reducerMap = {
41                 testOptimistic: withOptimistic([], testReducer).reducer,
42             };
44             const result = asIfNotOptimistic(state, reducerMap);
46             expect(Object.keys(result).length).toEqual(1);
47             expect(result.testOptimistic).toEqual(state.testOptimistic);
48         });
50         test('should compute optimistic substate without optimistic updates', () => {
51             const state = {
52                 testOptimistic: {
53                     items: [1, 2, 3],
54                     optimistic: {
55                         checkpoint: { items: [] },
56                         history: [
57                             createTestDeterministicAction('add', 1),
58                             createTestOptimisticHistoryItem('add', 2),
59                             createTestOptimisticHistoryItem('add', 3),
60                         ],
61                     },
62                 } as WrappedOptimisticState<TestState>,
63             };
65             const optimisticReducers: OptimisticReducersMapObject<typeof state> = {
66                 testOptimistic: withOptimistic([], testReducer).reducer,
67             };
69             const result = asIfNotOptimistic(state, optimisticReducers);
71             expect(result.testOptimistic?.items).toEqual([1]);
72         });
74         test('should handle deeply nested optimistic reducers', () => {
75             const state = {
76                 depth1: {
77                     depth2: {
78                         testOptimistic: {
79                             items: [1, 2, 3],
80                             optimistic: {
81                                 checkpoint: { items: [] },
82                                 history: [
83                                     createTestDeterministicAction('add', 1),
84                                     createTestOptimisticHistoryItem('add', 2),
85                                     createTestOptimisticHistoryItem('add', 3),
86                                 ],
87                             },
88                         } as WrappedOptimisticState<TestState>,
89                     },
90                 },
91             };
93             const optimisticReducers = {
94                 depth1: {
95                     depth2: {
96                         testOptimistic: withOptimistic([], testReducer).reducer,
97                     },
98                 },
99             };
101             const result = asIfNotOptimistic(state, optimisticReducers);
103             expect(result.depth1.depth2.testOptimistic.items).toEqual([1]);
104         });
105     });
107     describe('selectIsOptimistic', () => {
108         test('should return true if selected state without optimistic updates diverges', () => {
109             const state = {
110                 testOptimistic: {
111                     items: [1, 2, 3],
112                     optimistic: {
113                         checkpoint: { items: [] },
114                         history: [
115                             createTestDeterministicAction('add', 1),
116                             createTestOptimisticHistoryItem('add', 2),
117                             createTestOptimisticHistoryItem('add', 3),
118                         ],
119                     },
120                 } as WrappedOptimisticState<TestState>,
121             };
123             const reducerMap = {
124                 testOptimistic: withOptimistic([], testReducer).reducer,
125             };
127             const isOptimistic = selectIsOptimistic(state)(reducerMap)((s) => s.testOptimistic.items);
128             expect(isOptimistic).toBe(true);
129         });
131         test('should return false if both selected states with & without optimistic updates match', () => {
132             const state = {
133                 testOptimistic: {
134                     items: [1, 2, 3],
135                     optimistic: {
136                         checkpoint: { items: [] },
137                         history: [
138                             createTestDeterministicAction('add', 1),
139                             createTestDeterministicAction('add', 2),
140                             createTestDeterministicAction('add', 3),
141                         ],
142                     },
143                 } as WrappedOptimisticState<TestState>,
144             };
146             const reducerMap: OptimisticReducersMapObject<typeof state> = {
147                 testOptimistic: withOptimistic([], testReducer).reducer,
148             };
150             const isOptimistic = selectIsOptimistic(state)(reducerMap)((s) => s.testOptimistic.items);
151             expect(isOptimistic).toBe(false);
152         });
153     });