repos / starfx

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

starfx / test
Eric Bower · 04 Mar 24

take.test.ts

  1import { describe, expect, it } from "../test.ts";
  2import type { AnyAction } from "../mod.ts";
  3import { put, sleep, spawn, take } from "../mod.ts";
  4import { createStore } from "../store/mod.ts";
  5
  6const takeTests = describe("take()");
  7
  8it(
  9  takeTests,
 10  "a put should complete before more `take` are added and then consumed automatically",
 11  async () => {
 12    const actual: AnyAction[] = [];
 13
 14    function* channelFn() {
 15      yield* sleep(10);
 16      yield* put({ type: "action-1", payload: 1 });
 17      yield* put({ type: "action-1", payload: 2 });
 18    }
 19
 20    function* root() {
 21      yield* spawn(channelFn);
 22
 23      actual.push(yield* take("action-1"));
 24      actual.push(yield* take("action-1"));
 25    }
 26
 27    const store = createStore({ initialState: {} });
 28    await store.run(root);
 29
 30    expect(actual).toEqual([
 31      { type: "action-1", payload: 1 },
 32      { type: "action-1", payload: 2 },
 33    ]);
 34  },
 35);
 36
 37it(takeTests, "take from default channel", async () => {
 38  function* channelFn() {
 39    yield* sleep(10);
 40    yield* put({ type: "action-*" });
 41    yield* put({ type: "action-1" });
 42    yield* put({ type: "action-2" });
 43    yield* put({ type: "unnoticeable-action" });
 44    yield* put({
 45      type: "",
 46      payload: {
 47        isAction: true,
 48      },
 49    });
 50    yield* put({
 51      type: "",
 52      payload: {
 53        isMixedWithPredicate: true,
 54      },
 55    });
 56    yield* put({
 57      type: "action-3",
 58    });
 59  }
 60
 61  const actual: AnyAction[] = [];
 62  function* genFn() {
 63    yield* spawn(channelFn);
 64
 65    try {
 66      actual.push(yield* take("*")); // take all actions
 67      actual.push(yield* take("action-1")); // take only actions of type 'action-1'
 68      actual.push(yield* take(["action-2", "action-2222"])); // take either type
 69      actual.push(yield* take((a: AnyAction) => a.payload?.isAction)); // take if match predicate
 70      actual.push(
 71        yield* take([
 72          "action-3",
 73          (a: AnyAction) => a.payload?.isMixedWithPredicate,
 74        ]),
 75      ); // take if match any from the mixed array
 76      actual.push(
 77        yield* take([
 78          "action-3",
 79          (a: AnyAction) => a.payload?.isMixedWithPredicate,
 80        ]),
 81      ); // take if match any from the mixed array
 82    } finally {
 83      actual.push({ type: "auto ended" });
 84    }
 85  }
 86
 87  const store = createStore({ initialState: {} });
 88  await store.run(genFn);
 89
 90  const expected = [
 91    {
 92      type: "action-*",
 93    },
 94    {
 95      type: "action-1",
 96    },
 97    {
 98      type: "action-2",
 99    },
100    {
101      type: "",
102      payload: {
103        isAction: true,
104      },
105    },
106    {
107      type: "",
108      payload: {
109        isMixedWithPredicate: true,
110      },
111    },
112    {
113      type: "action-3",
114    },
115    { type: "auto ended" },
116  ];
117  expect(actual).toEqual(expected);
118});