|
1 | | -import { describe, test, expect } from "vitest"; |
2 | | -import { createRoot } from "solid-js"; |
3 | | -import { createPrimitiveTemplate } from "../src"; |
4 | | - |
5 | | -describe("createPrimitiveTemplate", () => { |
6 | | - test("createPrimitiveTemplate return values", () => |
7 | | - createRoot(dispose => { |
8 | | - const [value, setValue] = createPrimitiveTemplate(true); |
9 | | - expect(value(), "initial value should be true").toBe(true); |
10 | | - setValue(false); |
11 | | - expect(value(), "value after change should be false").toBe(false); |
12 | | - dispose(); |
| 1 | +import { createEffect, createRoot, createSignal } from "solid-js"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | +import { createDerivedStaticStore, createHydratableStaticStore, createStaticStore } from "../src"; |
| 4 | + |
| 5 | +describe("createStaticStore", () => { |
| 6 | + test("individual keys only update when changed", () => { |
| 7 | + let aUpdates = -1; |
| 8 | + |
| 9 | + const { dispose, setState } = createRoot(dispose => { |
| 10 | + const _shape = { a: 1, b: 2, c: 3, d: [0, 1, 2] }; |
| 11 | + const [state, setState] = createStaticStore(_shape); |
| 12 | + |
| 13 | + expect(state).toEqual(_shape); |
| 14 | + expect(_shape, "original input shouldn't be mutated").toEqual({ |
| 15 | + a: 1, |
| 16 | + b: 2, |
| 17 | + c: 3, |
| 18 | + d: [0, 1, 2], |
| 19 | + }); |
| 20 | + |
| 21 | + setState({ a: 9, d: [3, 2, 1] }); |
| 22 | + |
| 23 | + expect(state).toEqual({ a: 9, b: 2, c: 3, d: [3, 2, 1] }); |
| 24 | + expect(_shape, "original input shouldn't be mutated").toEqual({ |
| 25 | + a: 1, |
| 26 | + b: 2, |
| 27 | + c: 3, |
| 28 | + d: [0, 1, 2], |
| 29 | + }); |
| 30 | + |
| 31 | + createEffect(() => { |
| 32 | + state.a; |
| 33 | + aUpdates++; |
| 34 | + }); |
| 35 | + |
| 36 | + return { dispose, setState }; |
| 37 | + }); |
| 38 | + |
| 39 | + expect(aUpdates).toBe(0); |
| 40 | + |
| 41 | + setState({ |
| 42 | + b: 3, |
| 43 | + }); |
| 44 | + expect(aUpdates).toBe(0); |
| 45 | + setState("a", 4); |
| 46 | + expect(aUpdates).toBe(1); |
| 47 | + |
| 48 | + dispose(); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("createHydratableStaticStore", () => { |
| 53 | + test("createHydratableStaticStore() - CSR", () => { |
| 54 | + const [state, setState] = createHydratableStaticStore({ foo: "server" }, () => ({ |
| 55 | + foo: "client", |
13 | 56 | })); |
| 57 | + expect(state).toEqual({ foo: "client" }); |
| 58 | + expect(setState).toBeInstanceOf(Function); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("createDerivedStaticStore", () => { |
| 63 | + test("individual keys only update when changed", () => { |
| 64 | + let aUpdates = -1; |
| 65 | + |
| 66 | + const { dispose, set } = createRoot(dispose => { |
| 67 | + const _shape = { a: 1, b: 2, c: 3, d: [0, 1, 2] }; |
| 68 | + const [s, set] = createSignal(_shape); |
| 69 | + const state = createDerivedStaticStore(s); |
| 70 | + |
| 71 | + expect(state).toEqual(_shape); |
| 72 | + expect(_shape, "original input shouldn't be mutated").toEqual({ |
| 73 | + a: 1, |
| 74 | + b: 2, |
| 75 | + c: 3, |
| 76 | + d: [0, 1, 2], |
| 77 | + }); |
| 78 | + |
| 79 | + set(p => ({ ...p, a: 9, d: [3, 2, 1] })); |
| 80 | + |
| 81 | + expect(state).toEqual({ a: 9, b: 2, c: 3, d: [3, 2, 1] }); |
| 82 | + expect(_shape, "original input shouldn't be mutated").toEqual({ |
| 83 | + a: 1, |
| 84 | + b: 2, |
| 85 | + c: 3, |
| 86 | + d: [0, 1, 2], |
| 87 | + }); |
| 88 | + |
| 89 | + createEffect(() => { |
| 90 | + state.a; |
| 91 | + aUpdates++; |
| 92 | + }); |
| 93 | + |
| 94 | + return { dispose, set }; |
| 95 | + }); |
| 96 | + |
| 97 | + expect(aUpdates).toBe(0); |
| 98 | + |
| 99 | + set(p => ({ ...p, b: 3 })); |
| 100 | + expect(aUpdates).toBe(0); |
| 101 | + set(p => ({ ...p, a: 4 })); |
| 102 | + expect(aUpdates).toBe(1); |
| 103 | + |
| 104 | + dispose(); |
| 105 | + }); |
14 | 106 | }); |
0 commit comments