|
| 1 | +import { beforeEach, afterEach, describe, it, jest } from "@jest/globals"; |
| 2 | +import { IndexableSyncExternalStore } from "./store.js"; |
| 3 | +import { IndexableStore } from "./types.js"; |
| 4 | + |
| 5 | +describe("IndexableSyncExternalStore", () => { |
| 6 | + describe("array data", () => { |
| 7 | + type Item = { id: number; value: string }; |
| 8 | + const mockAction = jest.fn(async (value: Item) => value); |
| 9 | + const mockStoreInitialState: IndexableStore<Item> = { |
| 10 | + data: [ |
| 11 | + { id: 2112, value: "hello world" }, |
| 12 | + { id: 13, value: "hola mundo" }, |
| 13 | + ], |
| 14 | + error: null, |
| 15 | + state: "unsent", |
| 16 | + }; |
| 17 | + |
| 18 | + class MockStore extends IndexableSyncExternalStore<IndexableStore<Item>> { |
| 19 | + constructor() { |
| 20 | + super(mockStoreInitialState); |
| 21 | + } |
| 22 | + |
| 23 | + private getIndex(item: Item) { |
| 24 | + const snapshot = this.getSnapshot(); |
| 25 | + if (!Array.isArray(snapshot.data)) { |
| 26 | + throw new Error("IndexableSyncExternalStore: data is not an array"); |
| 27 | + } |
| 28 | + const index = snapshot.data.findIndex((data) => data.id === item.id); |
| 29 | + |
| 30 | + if (index === -1) { |
| 31 | + return snapshot.data.length; |
| 32 | + } |
| 33 | + |
| 34 | + return index; |
| 35 | + } |
| 36 | + |
| 37 | + private async updateDataAction(value: Item) { |
| 38 | + const action = this.createAction({ |
| 39 | + action: mockAction, |
| 40 | + key: this.getIndex(value), |
| 41 | + }); |
| 42 | + return action(value); |
| 43 | + } |
| 44 | + public async updateData(value: Item) { |
| 45 | + await this.updateDataAction(value); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let store: MockStore; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + store = new MockStore(); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + jest.clearAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("updateData", () => { |
| 60 | + it("will update the store", async () => { |
| 61 | + //* Arrange |
| 62 | + const item = { id: 2112, value: "goodbye world" }; |
| 63 | + |
| 64 | + //* Act |
| 65 | + await store.updateData(item); |
| 66 | + const updatedSnapshot = store.getSnapshot(); |
| 67 | + |
| 68 | + //* Assert |
| 69 | + expect(mockAction).toHaveBeenCalledTimes(1); |
| 70 | + expect(updatedSnapshot.data[0]).toMatchObject(item); |
| 71 | + expect(updatedSnapshot.data[1]).toMatchObject(mockStoreInitialState.data[1]); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("object data", () => { |
| 77 | + type Item = { id: number; value: string }; |
| 78 | + const mockAction = jest.fn(async (value: Item) => value); |
| 79 | + const mockStoreInitialState: IndexableStore<Item> = { |
| 80 | + data: { |
| 81 | + 2112: { id: 2112, value: "hello world" }, |
| 82 | + 13: { id: 13, value: "hola mundo" }, |
| 83 | + }, |
| 84 | + error: null, |
| 85 | + state: "unsent", |
| 86 | + }; |
| 87 | + |
| 88 | + class MockStore extends IndexableSyncExternalStore<IndexableStore<Item>> { |
| 89 | + constructor() { |
| 90 | + super(mockStoreInitialState); |
| 91 | + } |
| 92 | + |
| 93 | + private getIndex(item: Item) { |
| 94 | + return item.id; |
| 95 | + } |
| 96 | + |
| 97 | + private async updateDataAction(value: Item) { |
| 98 | + const action = this.createAction({ |
| 99 | + action: mockAction, |
| 100 | + key: this.getIndex(value), |
| 101 | + }); |
| 102 | + return action(value); |
| 103 | + } |
| 104 | + public async updateData(value: Item) { |
| 105 | + await this.updateDataAction(value); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + let store: MockStore; |
| 110 | + |
| 111 | + beforeEach(() => { |
| 112 | + store = new MockStore(); |
| 113 | + }); |
| 114 | + |
| 115 | + afterEach(() => { |
| 116 | + jest.clearAllMocks(); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("updateData", () => { |
| 120 | + it("will update the store", async () => { |
| 121 | + //* Arrange |
| 122 | + const item = { id: 2112, value: "goodbye world" }; |
| 123 | + |
| 124 | + //* Act |
| 125 | + await store.updateData(item); |
| 126 | + const updatedSnapshot = store.getSnapshot(); |
| 127 | + |
| 128 | + //* Assert |
| 129 | + expect(mockAction).toHaveBeenCalledTimes(1); |
| 130 | + expect(updatedSnapshot.data[2112]).toMatchObject(item); |
| 131 | + expect(updatedSnapshot.data[13]).toMatchObject(mockStoreInitialState.data[13]); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments