From 847bbcd0824c98aa019fa9c2239896810e156442 Mon Sep 17 00:00:00 2001 From: Herb Caudill Date: Wed, 27 Mar 2024 14:12:08 +0100 Subject: [PATCH] convert PAYLOAD_A etc to functions --- .../helpers/tests/storage-adapter-tests.ts | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts b/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts index a8a45c942..9d077bf8c 100644 --- a/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts +++ b/packages/automerge-repo/src/helpers/tests/storage-adapter-tests.ts @@ -2,9 +2,9 @@ import { describe, expect, it } from "vitest" import type { StorageAdapterInterface } from "../../storage/StorageAdapterInterface.js" -const PAYLOAD_A = new Uint8Array([0, 1, 127, 99, 154, 235]) -const PAYLOAD_B = new Uint8Array([1, 76, 160, 53, 57, 10, 230]) -const PAYLOAD_C = new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193]) +const PAYLOAD_A = () => new Uint8Array([0, 1, 127, 99, 154, 235]) +const PAYLOAD_B = () => new Uint8Array([1, 76, 160, 53, 57, 10, 230]) +const PAYLOAD_C = () => new Uint8Array([2, 111, 74, 131, 236, 96, 142, 193]) const LARGE_PAYLOAD = new Uint8Array(100000).map(() => Math.random() * 256) @@ -32,9 +32,9 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should return data that was saved", async () => { const { adapter, teardown } = await setup() - await adapter.save(["storage-adapter-id"], PAYLOAD_A) + await adapter.save(["storage-adapter-id"], PAYLOAD_A()) const actual = await adapter.load(["storage-adapter-id"]) - expect(actual).toStrictEqual(PAYLOAD_A) + expect(actual).toStrictEqual(PAYLOAD_A()) teardown() }) @@ -42,9 +42,9 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should work with composite keys", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) const actual = await adapter.load(["AAAAA", "sync-state", "xxxxx"]) - expect(actual).toStrictEqual(PAYLOAD_A) + expect(actual).toStrictEqual(PAYLOAD_A()) teardown() }) @@ -74,22 +74,22 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should return all the data that matches the key", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) - await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B) - await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) + await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B()) + await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C()) expect(await adapter.loadRange(["AAAAA"])).toStrictEqual( expect.arrayContaining([ - { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A }, - { key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B }, - { key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C }, + { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() }, + { key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B() }, + { key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() }, ]) ) expect(await adapter.loadRange(["AAAAA", "sync-state"])).toStrictEqual( expect.arrayContaining([ - { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A }, - { key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C }, + { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() }, + { key: ["AAAAA", "sync-state", "zzzzz"], data: PAYLOAD_C() }, ]) ) @@ -99,18 +99,18 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should only load values that match they key", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) - await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) + await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_C()) const actual = await adapter.loadRange(["AAAAA"]) expect(actual).toStrictEqual( expect.arrayContaining([ - { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A }, + { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_A() }, ]) ) expect(actual).toStrictEqual( expect.not.arrayContaining([ - { key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C }, + { key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_C() }, ]) ) @@ -122,7 +122,7 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("after removing, should be empty", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "snapshot", "xxxxx"], PAYLOAD_A) + await adapter.save(["AAAAA", "snapshot", "xxxxx"], PAYLOAD_A()) await adapter.remove(["AAAAA", "snapshot", "xxxxx"]) expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([]) @@ -138,11 +138,11 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should overwrite data saved with the same key", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_B()) expect(await adapter.loadRange(["AAAAA", "sync-state"])).toStrictEqual([ - { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B }, + { key: ["AAAAA", "sync-state", "xxxxx"], data: PAYLOAD_B() }, ]) teardown() @@ -153,14 +153,14 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should remove a range of records", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) - await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B) - await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) + await adapter.save(["AAAAA", "snapshot", "yyyyy"], PAYLOAD_B()) + await adapter.save(["AAAAA", "sync-state", "zzzzz"], PAYLOAD_C()) await adapter.removeRange(["AAAAA", "sync-state"]) expect(await adapter.loadRange(["AAAAA"])).toStrictEqual([ - { key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B }, + { key: ["AAAAA", "snapshot", "yyyyy"], data: PAYLOAD_B() }, ]) teardown() @@ -169,14 +169,14 @@ export function runStorageAdapterTests(_setup: SetupFn, title?: string): void { it("should not remove records that don't match", async () => { const { adapter, teardown } = await setup() - await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A) - await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_B) + await adapter.save(["AAAAA", "sync-state", "xxxxx"], PAYLOAD_A()) + await adapter.save(["BBBBB", "sync-state", "zzzzz"], PAYLOAD_B()) await adapter.removeRange(["AAAAA"]) const actual = await adapter.loadRange(["BBBBB"]) expect(actual).toStrictEqual([ - { key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_B }, + { key: ["BBBBB", "sync-state", "zzzzz"], data: PAYLOAD_B() }, ]) teardown()