repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / store / slice
Vlad · 12 Sep 23

obj.test.ts

 1import { asserts, describe, it } from "../../test.ts";
 2import { configureStore, updateStore } from "../../store/mod.ts";
 3
 4import { createObj } from "./obj.ts";
 5const tests = describe("createObj()");
 6
 7export interface ICurrentUser {
 8  username: string;
 9  userId: number;
10  isadmin: boolean;
11  roles: string[];
12}
13
14const NAME = "currentUser";
15const crtInitialState = {
16  username: "",
17  userId: 0,
18  isadmin: false,
19  roles: [],
20};
21
22const slice = createObj<ICurrentUser>({
23  name: NAME,
24  initialState: crtInitialState,
25});
26
27it(tests, "sets up an obj", async () => {
28  const store = configureStore({
29    initialState: {
30      [NAME]: crtInitialState,
31    },
32  });
33
34  await store.run(function* () {
35    yield* updateStore(slice.set({
36      username: "bob",
37      userId: 1,
38      isadmin: true,
39      roles: ["admin", "user"],
40    }));
41  });
42
43  asserts.assertEquals(store.getState()["currentUser"], {
44    username: "bob",
45    userId: 1,
46    isadmin: true,
47    roles: ["admin", "user"],
48  });
49
50  await store.run(function* () {
51    yield* updateStore(slice.update({ key: "username", value: "alice" }));
52  });
53
54  asserts.assertEquals(store.getState()["currentUser"], {
55    username: "alice",
56    userId: 1,
57    isadmin: true,
58    roles: ["admin", "user"],
59  });
60
61  await store.run(function* () {
62    yield* updateStore(
63      slice.update({ key: "roles", value: ["admin", "superuser"] }),
64    );
65  });
66
67  asserts.assertEquals(store.getState()["currentUser"], {
68    username: "alice",
69    userId: 1,
70    isadmin: true,
71    roles: ["admin", "superuser"],
72  });
73});