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()],
15 const nextOptimistic = removeHistoryItem(testReducer, optimistic, uniqueId());
16 expect(nextOptimistic).toEqual(optimistic);
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);
27 createTestDeterministicAction('add', 2),
28 createTestOptimisticHistoryItem('remove', 1),
29 createTestDeterministicAction('remove', 2),
32 const optimistic: OptimisticState<TestState> = { checkpoint, history };
33 const nextOptimistic = removeHistoryItem(testReducer, optimistic, first.id);
36 * first 2 actions in history were remove and checkpoint recomputed up
37 * until next optimistic action
39 expect(nextOptimistic.checkpoint?.items).toEqual([1, 2]);
40 expect(nextOptimistic.history).toEqual([history[2], history[3]]);
43 test('should pop optimistic history item from history', () => {
44 const optimisticAction = createTestOptimisticHistoryItem('remove', 1);
46 createTestDeterministicAction('add', 1),
48 createTestDeterministicAction('add', 2),
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]]);