|
8 | 8 | NanostoresSyncObject, |
9 | 9 | SyncClient, |
10 | 10 | SyncObjectPool, |
| 11 | + type SyncStorage, |
11 | 12 | } from "./sync-client"; |
12 | 13 |
|
13 | 14 | enableMapSet(); |
@@ -217,6 +218,18 @@ test("support pool of objects", () => { |
217 | 218 | ); |
218 | 219 | }); |
219 | 220 |
|
| 221 | +test("merge state in pool object to partially restore from storage", () => { |
| 222 | + const store1 = atom(0); |
| 223 | + const store2 = atom(1); |
| 224 | + const objectPool = new SyncObjectPool([ |
| 225 | + new NanostoresSyncObject("store1", store1), |
| 226 | + new NanostoresSyncObject("store2", store2), |
| 227 | + ]); |
| 228 | + objectPool.setState(new Map([["store1", 2]])); |
| 229 | + expect(store1.get()).toEqual(2); |
| 230 | + expect(store2.get()).toEqual(1); |
| 231 | +}); |
| 232 | + |
220 | 233 | describe("nanostores sync object", () => { |
221 | 234 | test("sync initial state and exchange transactions", () => { |
222 | 235 | const emitter = createNanoEvents(); |
@@ -290,3 +303,79 @@ describe("nanostores sync object", () => { |
290 | 303 | expect(sendTransaction).toBeCalledTimes(0); |
291 | 304 | }); |
292 | 305 | }); |
| 306 | + |
| 307 | +describe("storages", () => { |
| 308 | + class TestStorage implements SyncStorage { |
| 309 | + name = "TestStorage"; |
| 310 | + value: undefined | number; |
| 311 | + constructor(value: undefined | number) { |
| 312 | + this.value = value; |
| 313 | + } |
| 314 | + sendTransaction = vi.fn(); |
| 315 | + subscribe(setState: (state: unknown) => void) { |
| 316 | + setState(this.value); |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + test("get initial state from storage", () => { |
| 321 | + const $store = atom(0); |
| 322 | + const client = new SyncClient({ |
| 323 | + role: "leader", |
| 324 | + object: new NanostoresSyncObject("nanostores", $store), |
| 325 | + storages: [new TestStorage(1)], |
| 326 | + }); |
| 327 | + client.connect({ signal: new AbortController().signal }); |
| 328 | + expect($store.get()).toEqual(1); |
| 329 | + }); |
| 330 | + |
| 331 | + test("fallback to current state when storage is empty", () => { |
| 332 | + const $store = atom(0); |
| 333 | + const client = new SyncClient({ |
| 334 | + role: "leader", |
| 335 | + object: new NanostoresSyncObject("nanostores", $store), |
| 336 | + storages: [new TestStorage(undefined)], |
| 337 | + }); |
| 338 | + client.connect({ signal: new AbortController().signal }); |
| 339 | + expect($store.get()).toEqual(0); |
| 340 | + }); |
| 341 | + |
| 342 | + test("get state from the first non-empty storage", () => { |
| 343 | + const $store = atom(0); |
| 344 | + const client = new SyncClient({ |
| 345 | + role: "leader", |
| 346 | + object: new NanostoresSyncObject("nanostores", $store), |
| 347 | + storages: [ |
| 348 | + new TestStorage(undefined), |
| 349 | + new TestStorage(1), |
| 350 | + new TestStorage(2), |
| 351 | + ], |
| 352 | + }); |
| 353 | + client.connect({ signal: new AbortController().signal }); |
| 354 | + expect($store.get()).toEqual(1); |
| 355 | + }); |
| 356 | + |
| 357 | + test("send transactions to all provided storages", () => { |
| 358 | + const $store = atom(0); |
| 359 | + const storage1 = new TestStorage(undefined); |
| 360 | + const storage2 = new TestStorage(undefined); |
| 361 | + const client = new SyncClient({ |
| 362 | + role: "leader", |
| 363 | + object: new NanostoresSyncObject("nanostores", $store), |
| 364 | + storages: [storage1, storage2], |
| 365 | + }); |
| 366 | + client.connect({ signal: new AbortController().signal }); |
| 367 | + $store.set(1); |
| 368 | + expect(storage1.sendTransaction).toBeCalledTimes(1); |
| 369 | + expect(storage1.sendTransaction).toHaveBeenCalledWith({ |
| 370 | + id: expect.any(String), |
| 371 | + object: "nanostores", |
| 372 | + payload: 1, |
| 373 | + }); |
| 374 | + expect(storage2.sendTransaction).toBeCalledTimes(1); |
| 375 | + expect(storage2.sendTransaction).toHaveBeenCalledWith({ |
| 376 | + id: expect.any(String), |
| 377 | + object: "nanostores", |
| 378 | + payload: 1, |
| 379 | + }); |
| 380 | + }); |
| 381 | +}); |
0 commit comments