Skip to content

Commit 1f2e083

Browse files
jboldaVldMrgnn
andauthored
confirm tests assert when run within an generator function (#55)
--------- Co-authored-by: Vlad Marginean <[email protected]>
1 parent ce7d484 commit 1f2e083

File tree

7 files changed

+200
-108
lines changed

7 files changed

+200
-108
lines changed

Diff for: deps.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export type {
2+
Callable,
3+
Channel,
4+
Instruction,
5+
Operation,
6+
Predicate,
7+
Queue,
8+
Reject,
9+
Resolve,
10+
Result,
11+
Scope,
12+
Signal,
13+
Stream,
14+
Subscription,
15+
Task,
16+
} from "https://deno.land/x/[email protected]/mod.ts";
17+
export {
18+
action,
19+
call,
20+
createChannel,
21+
createContext,
22+
createQueue,
23+
createScope,
24+
createSignal,
25+
each,
26+
ensure,
27+
Err,
28+
Ok,
29+
race,
30+
resource,
31+
run,
32+
SignalQueueFactory,
33+
sleep,
34+
spawn,
35+
suspend,
36+
useAbortSignal,
37+
useScope,
38+
} from "https://deno.land/x/[email protected]/mod.ts";
39+
40+
import React from "https://esm.sh/[email protected]?pin=v135";
41+
42+
export type { JSX } from "https://esm.sh/[email protected]?pin=v135";
43+
44+
export { React };
45+
export {
46+
Provider,
47+
useDispatch,
48+
useSelector,
49+
useStore,
50+
} from "https://esm.sh/[email protected]?pin=v135";
51+
export type {
52+
TypedUseSelectorHook,
53+
} from "https://esm.sh/[email protected]?pin=v135";
54+
export { createSelector } from "https://esm.sh/[email protected]?pin=v135";
55+
56+
export {
57+
enablePatches,
58+
produce,
59+
produceWithPatches,
60+
} from "https://esm.sh/[email protected]?pin=v135";
61+
export type { Patch } from "https://esm.sh/[email protected]?pin=v135";

Diff for: test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {
55
describe,
66
it,
77
} from "jsr:@std/testing/bdd";
8+
export * as assertType from "jsr:@std/testing/types";
89
export { assert } from "jsr:@std/assert";
910
export * as asserts from "jsr:@std/assert";
1011
export { expect } from "jsr:@std/expect";

Diff for: test/api.test.ts

+39-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import { describe, expect, it } from "../test.ts";
2-
import {
3-
createSchema,
4-
createStore,
5-
select,
6-
slice,
7-
updateStore,
8-
waitForLoader,
9-
} from "../store/mod.ts";
101
import {
112
AnyState,
3+
API_ACTION_PREFIX,
124
ApiCtx,
135
call,
146
createApi,
@@ -21,6 +13,15 @@ import {
2113
waitFor,
2214
} from "../mod.ts";
2315
import { useCache } from "../react.ts";
16+
import {
17+
createSchema,
18+
createStore,
19+
select,
20+
slice,
21+
updateStore,
22+
waitForLoader,
23+
} from "../store/mod.ts";
24+
import { describe, expect, it } from "../test.ts";
2425

2526
interface User {
2627
id: string;
@@ -48,6 +49,7 @@ const jsonBlob = (data: unknown) => {
4849
const tests = describe("createApi()");
4950

5051
it(tests, "POST", async () => {
52+
expect.assertions(2);
5153
const query = createApi();
5254
query.use(mdw.queryCtx);
5355
query.use(mdw.nameParser);
@@ -118,6 +120,7 @@ it(tests, "POST", async () => {
118120
});
119121

120122
it(tests, "POST with uri", () => {
123+
expect.assertions(1);
121124
const query = createApi();
122125
query.use(mdw.queryCtx);
123126
query.use(mdw.nameParser);
@@ -165,6 +168,7 @@ it(tests, "POST with uri", () => {
165168
});
166169

167170
it(tests, "middleware - with request fn", () => {
171+
expect.assertions(2);
168172
const query = createApi();
169173
query.use(mdw.queryCtx);
170174
query.use(mdw.nameParser);
@@ -185,6 +189,7 @@ it(tests, "middleware - with request fn", () => {
185189
});
186190

187191
it(tests, "run() on endpoint action - should run the effect", () => {
192+
expect.assertions(1);
188193
const api = createApi<TestCtx>();
189194
api.use(api.routes());
190195
let acc = "";
@@ -212,7 +217,8 @@ it(tests, "run() on endpoint action - should run the effect", () => {
212217
store.dispatch(action2());
213218
});
214219

215-
it(tests, "run() from a normal saga", () => {
220+
it(tests, "run() from a normal saga", async () => {
221+
expect.assertions(6);
216222
const api = createApi();
217223
api.use(api.routes());
218224
let acc = "";
@@ -226,28 +232,43 @@ it(tests, "run() from a normal saga", () => {
226232
acc += "a";
227233
},
228234
);
235+
const extractedResults = {
236+
actionType: null,
237+
actionPayload: null,
238+
name: null,
239+
payload: null,
240+
};
229241
const action2 = () => ({ type: "ACTION" });
230242
function* onAction() {
231243
const ctx = yield* safe(() => action1.run(action1({ id: "1" })));
232244
if (!ctx.ok) {
233245
throw new Error("no ctx");
234246
}
235-
const payload = { name: "/users/:id [GET]", options: { id: "1" } };
236-
expect(ctx.value.action.type).toEqual(`@@starfx${action1}`);
237-
expect(ctx.value.action.payload).toEqual(payload);
238-
expect(ctx.value.name).toEqual("/users/:id [GET]");
239-
expect(ctx.value.payload).toEqual({ id: "1" });
247+
Object.assign(extractedResults, {
248+
actionType: ctx.value.action.type,
249+
actionPayload: ctx.value.action.payload,
250+
name: ctx.value.name,
251+
payload: ctx.value.payload,
252+
});
240253
acc += "b";
241-
expect(acc).toEqual("ab");
242254
}
243-
244255
function* watchAction() {
245-
yield* takeEvery(`${action2}`, onAction);
256+
yield* takeEvery(action2, onAction);
246257
}
247258

248259
const store = createStore({ initialState: { users: {} } });
249260
store.run(() => keepAlive([api.bootup, watchAction]));
250261
store.dispatch(action2());
262+
263+
await new Promise((resolve) => setTimeout(resolve, 300));
264+
const payload = { name: "/users/:id [GET]", options: { id: "1" } };
265+
266+
expect(extractedResults.actionType).toEqual(`${API_ACTION_PREFIX}${action1}`);
267+
expect(extractedResults.actionPayload!["name"]).toEqual(payload.name);
268+
expect(extractedResults.actionPayload!["options"]).toEqual(payload.options);
269+
expect(extractedResults.name).toEqual("/users/:id [GET]");
270+
expect(extractedResults.payload).toEqual({ id: "1" });
271+
expect(acc).toEqual("ab");
251272
});
252273

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

Diff for: test/safe.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { call, run } from "../mod.ts";
44
const tests = describe("call()");
55

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

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

3234
it(tests, "should call a promise", async () => {
35+
expect.assertions(1);
3336
const me = () =>
3437
new Promise<string>((resolve) => {
3538
setTimeout(() => {

Diff for: test/schema.test.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { asserts, describe, it } from "../test.ts";
1+
import { describe, expect, it } from "../test.ts";
22
import { createSchema, createStore, select, slice } from "../store/mod.ts";
33

44
const tests = describe("createSchema()");
@@ -16,7 +16,7 @@ const emptyUser = { id: "", name: "" };
1616
it(tests, "default schema", async () => {
1717
const [schema, initialState] = createSchema();
1818
const store = createStore({ initialState });
19-
asserts.assertEquals(store.getState(), {
19+
expect(store.getState()).toEqual({
2020
cache: {},
2121
loaders: {},
2222
});
@@ -26,16 +26,17 @@ it(tests, "default schema", async () => {
2626
yield* schema.update(schema.cache.add({ "1": true }));
2727
});
2828

29-
asserts.assertEquals(schema.cache.selectTable(store.getState()), {
29+
expect(schema.cache.selectTable(store.getState())).toEqual({
3030
"1": true,
3131
});
32-
asserts.assertEquals(
32+
expect(
3333
schema.loaders.selectById(store.getState(), { id: "1" }).status,
3434
"loading",
3535
);
3636
});
3737

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

53-
asserts.assertEquals(store.getState(), {
54+
expect(store.getState()).toEqual({
5455
users: { "1": { id: "1", name: "wow" } },
5556
token: "",
5657
counter: 0,
@@ -60,7 +61,7 @@ it(tests, "general types and functionality", async () => {
6061
loaders: {},
6162
});
6263
const userMap = db.users.selectTable(store.getState());
63-
asserts.assertEquals(userMap, { "1": { id: "1", name: "wow" } });
64+
expect(userMap).toEqual({ "1": { id: "1", name: "wow" } });
6465

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

7172
const users = yield* select(db.users.selectTable);
72-
asserts.assertEquals(users, {
73+
expect(users).toEqual({
7374
"1": { id: "1", name: "zzz" },
7475
"2": { id: "2", name: "bob" },
7576
});
7677

7778
yield* db.update(db.counter.increment());
7879
const counter = yield* select(db.counter.select);
79-
asserts.assertEquals(counter, 1);
80+
expect(counter).toBe(1);
8081

8182
yield* db.update(db.currentUser.update({ key: "name", value: "vvv" }));
8283
const curUser = yield* select(db.currentUser.select);
83-
asserts.assertEquals(curUser, { id: "", name: "vvv" });
84+
expect(curUser).toEqual({ id: "", name: "vvv" });
8485

8586
yield* db.update(db.loaders.start({ id: "fetch-users" }));
8687
const fetchLoader = yield* select(db.loaders.selectById, {
8788
id: "fetch-users",
8889
});
89-
asserts.assertEquals(fetchLoader.id, "fetch-users");
90-
asserts.assertEquals(fetchLoader.status, "loading");
91-
asserts.assertNotEquals(fetchLoader.lastRun, 0);
90+
expect(fetchLoader.id).toBe("fetch-users");
91+
expect(fetchLoader.status).toBe("loading");
92+
expect(fetchLoader.lastRun).not.toBe(0);
9293
});
9394
});
9495

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

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

111113
yield* db.update(
112114
db.currentUser.update({ key: "roles", value: ["admin", "users"] }),
113115
);
114116
const curUser3 = yield* select(db.currentUser.select);
115-
asserts.assertEquals(curUser3, {
117+
expect(curUser3).toEqual({
116118
id: "",
117119
name: "vvv",
118120
roles: ["admin", "users"],

0 commit comments

Comments
 (0)