Skip to content

Commit

Permalink
test: fix - api - run() from a normal saga
Browse files Browse the repository at this point in the history
  • Loading branch information
VldMrgnn committed Jan 1, 2025
1 parent 9812292 commit 5562414
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions test/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { describe, expect, it } from "../test.ts";
import {
createSchema,
createStore,
select,
slice,
updateStore,
waitForLoader,
} from "../store/mod.ts";
import {
AnyState,
API_ACTION_PREFIX,
ApiCtx,
call,
createApi,
Expand All @@ -21,6 +13,15 @@ import {
waitFor,
} from "../mod.ts";
import { useCache } from "../react.ts";
import {
createSchema,
createStore,
select,
slice,
updateStore,
waitForLoader,
} from "../store/mod.ts";
import { describe, expect, it } from "../test.ts";

interface User {
id: string;
Expand Down Expand Up @@ -216,8 +217,8 @@ it(tests, "run() on endpoint action - should run the effect", () => {
store.dispatch(action2());
});

it(tests, "run() from a normal saga", () => {
expect.assertions(5);
it(tests, "run() from a normal saga", async () => {
expect.assertions(6);
const api = createApi();
api.use(api.routes());
let acc = "";
Expand All @@ -231,28 +232,43 @@ it(tests, "run() from a normal saga", () => {
acc += "a";
},
);
const extractedResults = {
actionType: null,
actionPayload: null,
name: null,
payload: null,
};
const action2 = () => ({ type: "ACTION" });
function* onAction() {
const ctx = yield* safe(() => action1.run(action1({ id: "1" })));
if (!ctx.ok) {
throw new Error("no ctx");
}
const payload = { name: "/users/:id [GET]", options: { id: "1" } };
expect(ctx.value.action.type).toEqual(`@@starfx${action1}`);
expect(ctx.value.action.payload).toEqual(payload);
expect(ctx.value.name).toEqual("/users/:id [GET]");
expect(ctx.value.payload).toEqual({ id: "1" });
Object.assign(extractedResults, {
actionType: ctx.value.action.type,
actionPayload: ctx.value.action.payload,
name: ctx.value.name,
payload: ctx.value.payload,
});
acc += "b";
expect(acc).toEqual("ab");
}

function* watchAction() {
yield* takeEvery(`${action2}`, onAction);
yield* takeEvery(action2, onAction);
}

const store = createStore({ initialState: { users: {} } });
store.run(() => keepAlive([api.bootup, watchAction]));
store.dispatch(action2());

await new Promise((resolve) => setTimeout(resolve, 300));
const payload = { name: "/users/:id [GET]", options: { id: "1" } };

expect(extractedResults.actionType).toEqual(`${API_ACTION_PREFIX}${action1}`);
expect(extractedResults.payload!["name"]).toEqual(payload.name);
expect(extractedResults.payload!["options"]).toEqual(payload.options);
expect(extractedResults.name).toEqual("/users/:id [GET]");
expect(extractedResults.payload).toEqual({ id: "1" });
expect(acc).toEqual("ab");
});

it(tests, "with hash key on a large post", async () => {
Expand Down

0 comments on commit 5562414

Please sign in to comment.