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

table.test.ts

  1import { updateStore } from "../../../store/fx.js";
  2import { createTable, table } from "../../../store/slice/table.js";
  3import { configureStore } from "../../../store/store.js";
  4import { expect, test } from "../../../test.js";
  5
  6type TUser = {
  7  id: number;
  8  user: string;
  9};
 10
 11const NAME = "table";
 12const empty = { id: 0, user: "" };
 13const slice = createTable<TUser>({
 14  name: NAME,
 15  empty,
 16});
 17
 18const initialState = {
 19  [NAME]: slice.initialState,
 20};
 21
 22const first = { id: 1, user: "A" };
 23const second = { id: 2, user: "B" };
 24const third = { id: 3, user: "C" };
 25
 26test("sets up a table", async () => {
 27  const store = configureStore({
 28    initialState,
 29  });
 30
 31  await store.run(function* () {
 32    yield* updateStore(slice.set({ [first.id]: first }));
 33  });
 34  expect(store.getState()[NAME]).toEqual({ [first.id]: first });
 35});
 36
 37test("adds a row", async () => {
 38  const store = configureStore({
 39    initialState,
 40  });
 41
 42  await store.run(function* () {
 43    yield* updateStore(slice.set({ [second.id]: second }));
 44  });
 45  expect(store.getState()[NAME]).toEqual({ 2: second });
 46});
 47
 48test("removes a row", async () => {
 49  const store = configureStore({
 50    initialState: {
 51      ...initialState,
 52      [NAME]: { [first.id]: first, [second.id]: second } as Record<
 53        string,
 54        TUser
 55      >,
 56    },
 57  });
 58
 59  await store.run(function* () {
 60    yield* updateStore(slice.remove(["1"]));
 61  });
 62  expect(store.getState()[NAME]).toEqual({ [second.id]: second });
 63});
 64
 65test("updates a row", async () => {
 66  const store = configureStore({
 67    initialState,
 68  });
 69  await store.run(function* () {
 70    const updated = { id: second.id, user: "BB" };
 71    yield* updateStore(slice.patch({ [updated.id]: updated }));
 72  });
 73  expect(store.getState()[NAME]).toEqual({
 74    [second.id]: { ...second, user: "BB" },
 75  });
 76});
 77
 78test("gets a row", async () => {
 79  const store = configureStore({
 80    initialState,
 81  });
 82  await store.run(function* () {
 83    yield* updateStore(
 84      slice.add({ [first.id]: first, [second.id]: second, [third.id]: third }),
 85    );
 86  });
 87
 88  const row = slice.selectById(store.getState(), { id: "2" });
 89  expect(row).toEqual(second);
 90});
 91
 92test("when the record doesnt exist, it returns empty record", () => {
 93  const store = configureStore({
 94    initialState,
 95  });
 96
 97  const row = slice.selectById(store.getState(), { id: "2" });
 98  expect(row).toEqual(empty);
 99});
100
101test("gets all rows", async () => {
102  const store = configureStore({
103    initialState,
104  });
105  const data = { [first.id]: first, [second.id]: second, [third.id]: third };
106  await store.run(function* () {
107    yield* updateStore(slice.add(data));
108  });
109  expect(store.getState()[NAME]).toEqual(data);
110});
111
112// checking types of `result` here
113test("with empty", async () => {
114  const tbl = table<TUser>({ empty: first })("users");
115  const store = configureStore({
116    initialState,
117  });
118
119  expect(tbl.empty).toEqual(first);
120  await store.run(function* () {
121    yield* updateStore(tbl.set({ [first.id]: first }));
122  });
123  expect(tbl.selectTable(store.getState())).toEqual({
124    [first.id]: first,
125  });
126  const result = tbl.selectById(store.getState(), { id: 1 });
127  expect(result).toEqual(first);
128});
129
130// checking types of `result` here
131test("with no empty", async () => {
132  const tbl = table<TUser>()("users");
133  const store = configureStore({
134    initialState,
135  });
136
137  expect(tbl.empty).toEqual(undefined);
138  await store.run(function* () {
139    yield* updateStore(tbl.set({ [first.id]: first }));
140  });
141  expect(tbl.selectTable(store.getState())).toEqual({
142    [first.id]: first,
143  });
144  const result = tbl.selectById(store.getState(), { id: 1 });
145  expect(result).toEqual(first);
146});