Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

confirm tests assert when run within an generator function #55

Merged
merged 9 commits into from
Jan 4, 2025
61 changes: 61 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export type {
Callable,
Channel,
Instruction,
Operation,
Predicate,
Queue,
Reject,
Resolve,
Result,
Scope,
Signal,
Stream,
Subscription,
Task,
} from "https://deno.land/x/[email protected]/mod.ts";
export {
action,
call,
createChannel,
createContext,
createQueue,
createScope,
createSignal,
each,
ensure,
Err,
Ok,
race,
resource,
run,
SignalQueueFactory,
sleep,
spawn,
suspend,
useAbortSignal,
useScope,
} from "https://deno.land/x/[email protected]/mod.ts";

import React from "https://esm.sh/[email protected]?pin=v135";

export type { JSX } from "https://esm.sh/[email protected]?pin=v135";

export { React };
export {
Provider,
useDispatch,
useSelector,
useStore,
} from "https://esm.sh/[email protected]?pin=v135";
export type {
TypedUseSelectorHook,
} from "https://esm.sh/[email protected]?pin=v135";
export { createSelector } from "https://esm.sh/[email protected]?pin=v135";

export {
enablePatches,
produce,
produceWithPatches,
} from "https://esm.sh/[email protected]?pin=v135";
export type { Patch } from "https://esm.sh/[email protected]?pin=v135";
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export {
describe,
it,
} from "jsr:@std/testing/bdd";
export * as assertType from "jsr:@std/testing/types";
export { assert } from "jsr:@std/assert";
export * as asserts from "jsr:@std/assert";
export { expect } from "jsr:@std/expect";
Expand Down
57 changes: 39 additions & 18 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 @@ -48,6 +49,7 @@ const jsonBlob = (data: unknown) => {
const tests = describe("createApi()");

it(tests, "POST", async () => {
expect.assertions(2);
const query = createApi();
query.use(mdw.queryCtx);
query.use(mdw.nameParser);
Expand Down Expand Up @@ -118,6 +120,7 @@ it(tests, "POST", async () => {
});

it(tests, "POST with uri", () => {
expect.assertions(1);
const query = createApi();
query.use(mdw.queryCtx);
query.use(mdw.nameParser);
Expand Down Expand Up @@ -165,6 +168,7 @@ it(tests, "POST with uri", () => {
});

it(tests, "middleware - with request fn", () => {
expect.assertions(2);
const query = createApi();
query.use(mdw.queryCtx);
query.use(mdw.nameParser);
Expand All @@ -185,6 +189,7 @@ it(tests, "middleware - with request fn", () => {
});

it(tests, "run() on endpoint action - should run the effect", () => {
expect.assertions(1);
const api = createApi<TestCtx>();
api.use(api.routes());
let acc = "";
Expand Down Expand Up @@ -212,7 +217,8 @@ it(tests, "run() on endpoint action - should run the effect", () => {
store.dispatch(action2());
});

it(tests, "run() from a normal saga", () => {
it(tests, "run() from a normal saga", async () => {
expect.assertions(6);
const api = createApi();
api.use(api.routes());
let acc = "";
Expand All @@ -226,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.actionPayload!["name"]).toEqual(payload.name);
expect(extractedResults.actionPayload!["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
3 changes: 3 additions & 0 deletions test/safe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { call, run } from "../mod.ts";
const tests = describe("call()");

it(tests, "should call the generator function", async () => {
expect.assertions(1);
function* me() {
return "valid";
}
Expand All @@ -15,6 +16,7 @@ it(tests, "should call the generator function", async () => {
});

it(tests, "should return an Err()", async () => {
expect.assertions(1);
const err = new Error("bang!");
function* me() {
throw err;
Expand All @@ -30,6 +32,7 @@ it(tests, "should return an Err()", async () => {
});

it(tests, "should call a promise", async () => {
expect.assertions(1);
const me = () =>
new Promise<string>((resolve) => {
setTimeout(() => {
Expand Down
32 changes: 17 additions & 15 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { asserts, describe, it } from "../test.ts";
import { describe, expect, it } from "../test.ts";
import { createSchema, createStore, select, slice } from "../store/mod.ts";

const tests = describe("createSchema()");
Expand All @@ -16,7 +16,7 @@ const emptyUser = { id: "", name: "" };
it(tests, "default schema", async () => {
const [schema, initialState] = createSchema();
const store = createStore({ initialState });
asserts.assertEquals(store.getState(), {
expect(store.getState()).toEqual({
cache: {},
loaders: {},
});
Expand All @@ -26,16 +26,17 @@ it(tests, "default schema", async () => {
yield* schema.update(schema.cache.add({ "1": true }));
});

asserts.assertEquals(schema.cache.selectTable(store.getState()), {
expect(schema.cache.selectTable(store.getState())).toEqual({
"1": true,
});
asserts.assertEquals(
expect(
schema.loaders.selectById(store.getState(), { id: "1" }).status,
"loading",
);
});

it(tests, "general types and functionality", async () => {
expect.assertions(8);
const [db, initialState] = createSchema({
users: slice.table<User>({
initialState: { "1": { id: "1", name: "wow" } },
Expand All @@ -50,7 +51,7 @@ it(tests, "general types and functionality", async () => {
});
const store = createStore({ initialState });

asserts.assertEquals(store.getState(), {
expect(store.getState()).toEqual({
users: { "1": { id: "1", name: "wow" } },
token: "",
counter: 0,
Expand All @@ -60,7 +61,7 @@ it(tests, "general types and functionality", async () => {
loaders: {},
});
const userMap = db.users.selectTable(store.getState());
asserts.assertEquals(userMap, { "1": { id: "1", name: "wow" } });
expect(userMap).toEqual({ "1": { id: "1", name: "wow" } });

await store.run(function* () {
yield* db.update([
Expand All @@ -69,30 +70,31 @@ it(tests, "general types and functionality", async () => {
]);

const users = yield* select(db.users.selectTable);
asserts.assertEquals(users, {
expect(users).toEqual({
"1": { id: "1", name: "zzz" },
"2": { id: "2", name: "bob" },
});

yield* db.update(db.counter.increment());
const counter = yield* select(db.counter.select);
asserts.assertEquals(counter, 1);
expect(counter).toBe(1);

yield* db.update(db.currentUser.update({ key: "name", value: "vvv" }));
const curUser = yield* select(db.currentUser.select);
asserts.assertEquals(curUser, { id: "", name: "vvv" });
expect(curUser).toEqual({ id: "", name: "vvv" });

yield* db.update(db.loaders.start({ id: "fetch-users" }));
const fetchLoader = yield* select(db.loaders.selectById, {
id: "fetch-users",
});
asserts.assertEquals(fetchLoader.id, "fetch-users");
asserts.assertEquals(fetchLoader.status, "loading");
asserts.assertNotEquals(fetchLoader.lastRun, 0);
expect(fetchLoader.id).toBe("fetch-users");
expect(fetchLoader.status).toBe("loading");
expect(fetchLoader.lastRun).not.toBe(0);
});
});

it(tests, "can work with a nested object", async () => {
expect.assertions(3);
const [db, initialState] = createSchema({
currentUser: slice.obj<UserWithRoles>({ id: "", name: "", roles: [] }),
cache: slice.table({ empty: {} }),
Expand All @@ -102,17 +104,17 @@ it(tests, "can work with a nested object", async () => {
await store.run(function* () {
yield* db.update(db.currentUser.update({ key: "name", value: "vvv" }));
const curUser = yield* select(db.currentUser.select);
asserts.assertEquals(curUser, { id: "", name: "vvv", roles: [] });
expect(curUser).toEqual({ id: "", name: "vvv", roles: [] });

yield* db.update(db.currentUser.update({ key: "roles", value: ["admin"] }));
const curUser2 = yield* select(db.currentUser.select);
asserts.assertEquals(curUser2, { id: "", name: "vvv", roles: ["admin"] });
expect(curUser2).toEqual({ id: "", name: "vvv", roles: ["admin"] });

yield* db.update(
db.currentUser.update({ key: "roles", value: ["admin", "users"] }),
);
const curUser3 = yield* select(db.currentUser.select);
asserts.assertEquals(curUser3, {
expect(curUser3).toEqual({
id: "",
name: "vvv",
roles: ["admin", "users"],
Expand Down
Loading
Loading