repos / starfx

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

starfx / store / slice
Eric Bower · 12 Dec 23

table.test.ts

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