1 import { createTestItem } from '@proton/pass/lib/items/item.test.utils';
2 import { uniqueId } from '@proton/pass/utils/string/unique-id';
4 import { updateItem, updateItems, withOptimisticItemsByShareId } from './items';
6 describe('items reducer', () => {
7 const slice = withOptimisticItemsByShareId.reducer(undefined, { type: '__TEST__' });
9 const shareA = uniqueId();
10 const shareB = uniqueId();
12 const login = createTestItem('login', { shareId: shareA });
13 const note = createTestItem('note', { shareId: shareA });
14 const alias = createTestItem('alias', { shareId: shareB });
16 slice[shareA] = { [login.itemId]: login, [note.itemId]: note };
17 slice[shareB] = { [alias.itemId]: alias };
19 describe('updateItem', () => {
20 test('should noop if item does not exist', () => {
21 const slice = withOptimisticItemsByShareId.reducer(undefined, { type: '__TEST__' });
22 const next = updateItem({ itemId: uniqueId(), shareId: uniqueId(), revision: 2 })(slice);
23 expect(next === slice).toBe(true);
24 expect(next).toStrictEqual(slice);
27 test('should update item if exists', () => {
28 const next = updateItem({ itemId: login.itemId, shareId: login.shareId, revision: 2 })(slice);
29 expect(next === slice).toBe(false);
30 expect(next[shareA]).toStrictEqual({ [login.itemId]: { ...login, revision: 2 }, [note.itemId]: note });
31 expect(next[shareB]).toStrictEqual(slice[shareB]);
35 describe('updateItems', () => {
36 test('should only apply updates to existing items', () => {
37 const nonExistingShareId = uniqueId();
39 const next = updateItems([
40 { itemId: uniqueId(), shareId: nonExistingShareId, revision: 2 },
41 { itemId: login.itemId, shareId: login.shareId, revision: 2 },
44 expect(next[shareA]).toStrictEqual({ [login.itemId]: { ...login, revision: 2 }, [note.itemId]: note });
45 expect(next[shareB]).toStrictEqual(slice[shareB]);
46 expect(next[nonExistingShareId]).toBeUndefined();