|
| 1 | +import { z } from "zod" |
| 2 | +import { defineStorage } from "./define-storage.js" |
| 3 | +import { test, assertType } from "vitest" |
| 4 | + |
| 5 | +test("it's not possible to set a value that violates the schema", () => { |
| 6 | + // ARRANGE |
| 7 | + const { storage } = defineStorage({ |
| 8 | + someKey: z.string(), |
| 9 | + }) |
| 10 | + |
| 11 | + // ACT & ASSERT |
| 12 | + // @ts-expect-error -- Cannot set a value that violates the schema |
| 13 | + assertType(storage.set("someKey", 1)) |
| 14 | +}) |
| 15 | + |
| 16 | +test("it's not possible to access a non string key", () => { |
| 17 | + // ARRANGE |
| 18 | + const { storage } = defineStorage({ |
| 19 | + someKey: z.string(), |
| 20 | + }) |
| 21 | + |
| 22 | + // ACT & ASSERT |
| 23 | + // @ts-expect-error -- Cannot access a non string key |
| 24 | + assertType(storage.get(1)) |
| 25 | +}) |
| 26 | + |
| 27 | +test("it's not possible to access a non existent key", () => { |
| 28 | + // ARRANGE |
| 29 | + const { storage } = defineStorage({ |
| 30 | + someKey: z.string(), |
| 31 | + }) |
| 32 | + |
| 33 | + // ACT & ASSERT |
| 34 | + // @ts-expect-error -- Cannot access a non existent key |
| 35 | + assertType(storage.get("some-non-existent-key")) |
| 36 | +}) |
| 37 | + |
| 38 | +test("the value has a type of string", () => { |
| 39 | + // ARRANGE |
| 40 | + const { storage } = defineStorage({ |
| 41 | + someKey: z.string(), |
| 42 | + }) |
| 43 | + |
| 44 | + // ACT & ASSERT |
| 45 | + assertType<string | null>(storage.get("someKey")) |
| 46 | +}) |
| 47 | + |
| 48 | +test("the value has a type of boolean", () => { |
| 49 | + // ARRANGE |
| 50 | + const { storage } = defineStorage({ |
| 51 | + someBooleanKey: z.boolean(), |
| 52 | + }) |
| 53 | + |
| 54 | + // ACT & ASSERT |
| 55 | + assertType<boolean | null>(storage.get("someBooleanKey")) |
| 56 | +}) |
| 57 | + |
| 58 | +test("the value has a type of number", () => { |
| 59 | + // ARRANGE |
| 60 | + const { storage } = defineStorage({ |
| 61 | + someNumberKey: z.number(), |
| 62 | + }) |
| 63 | + |
| 64 | + // ACT & ASSERT |
| 65 | + assertType<number | null>(storage.get("someNumberKey")) |
| 66 | +}) |
| 67 | + |
| 68 | +test("value must have type of null", () => { |
| 69 | + // ARRANGE |
| 70 | + const { storage } = defineStorage({ |
| 71 | + someNumberKey: z.number(), |
| 72 | + }) |
| 73 | + |
| 74 | + // ACT & ASSERT |
| 75 | + assertType<number | null>(storage.get("someNumberKey")) |
| 76 | +}) |
| 77 | + |
| 78 | +test("returns nothing when removing an item", () => { |
| 79 | + // ARRANGE |
| 80 | + const { storage } = defineStorage({ |
| 81 | + someKey: z.string(), |
| 82 | + }) |
| 83 | + |
| 84 | + // ACT |
| 85 | + storage.remove("someKey") |
| 86 | + |
| 87 | + // ASSERT |
| 88 | + assertType<void>(storage.remove("someKey")) |
| 89 | +}) |
0 commit comments