repos / starfx

a micro-mvc framework for react apps
git clone https://github.com/neurosnap/starfx.git

starfx / src / test / store / slice
Eric Bower  ·  2025-06-06

obj.test.ts

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