Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / pass / store / optimistic / utils / remove-history-item.spec.ts
blobab6d6315212719317625ea14f83b885fe1cc50c9
1 import { uniqueId } from '@proton/pass/utils/string/unique-id';
3 import type { OptimisticState } from '../types';
4 import { removeHistoryItem } from './remove-history-item';
5 import type { TestState } from './testing.utils';
6 import { createTestDeterministicAction, createTestOptimisticHistoryItem, testReducer } from './testing.utils';
8 describe('removeHistoryItem', () => {
9     test('should early return if no items matched', () => {
10         const optimistic: OptimisticState<TestState> = {
11             checkpoint: undefined,
12             history: [createTestOptimisticHistoryItem()],
13         };
15         const nextOptimistic = removeHistoryItem(testReducer, optimistic, uniqueId());
16         expect(nextOptimistic).toEqual(optimistic);
17     });
19     test('should split history and optimize optimistic state if action to remove is the first in history', () => {
20         const first = createTestOptimisticHistoryItem('add', 1);
22         /* create checkpoint for current history stack */
23         const checkpoint = testReducer({ items: [] }, first.action);
25         const history = [
26             first,
27             createTestDeterministicAction('add', 2),
28             createTestOptimisticHistoryItem('remove', 1),
29             createTestDeterministicAction('remove', 2),
30         ];
32         const optimistic: OptimisticState<TestState> = { checkpoint, history };
33         const nextOptimistic = removeHistoryItem(testReducer, optimistic, first.id);
35         /**
36          * first 2 actions in history were remove and checkpoint recomputed up
37          * until next optimistic action
38          */
39         expect(nextOptimistic.checkpoint?.items).toEqual([1, 2]);
40         expect(nextOptimistic.history).toEqual([history[2], history[3]]);
41     });
43     test('should pop optimistic history item from history', () => {
44         const optimisticAction = createTestOptimisticHistoryItem('remove', 1);
45         const history = [
46             createTestDeterministicAction('add', 1),
47             optimisticAction,
48             createTestDeterministicAction('add', 2),
49         ];
51         const optimistic: OptimisticState<TestState> = { checkpoint: { items: [0] }, history };
52         const nextOptimistic = removeHistoryItem(testReducer, optimistic, optimisticAction.id);
54         expect(nextOptimistic.checkpoint).toBeDefined();
55         expect(nextOptimistic.checkpoint?.items).toEqual(optimistic.checkpoint?.items);
56         expect(nextOptimistic.history).toEqual([history[0], history[2]]);
57     });
58 });