From 31e6caa6cffa89e900bc3d58ce112713db5a7d9b Mon Sep 17 00:00:00 2001 From: Vlad Marginean Date: Wed, 1 Jan 2025 23:34:14 +0200 Subject: [PATCH] test: fix -thunks - add expected assertion count an review tests --- test/thunk.test.ts | 161 ++++++++++++++++++++++++--------------------- 1 file changed, 86 insertions(+), 75 deletions(-) diff --git a/test/thunk.test.ts b/test/thunk.test.ts index 6268eb8..2064570 100644 --- a/test/thunk.test.ts +++ b/test/thunk.test.ts @@ -1,7 +1,14 @@ -import { API_ACTION_PREFIX } from '../action.ts'; -import { call, createThunks, put, sleep as delay, takeEvery, waitFor } from '../mod.ts'; -import { createStore, updateStore } from '../store/mod.ts'; -import { assertLike, asserts, describe, expect, it } from '../test.ts'; +import { API_ACTION_PREFIX } from "../action.ts"; +import { + call, + createThunks, + put, + sleep as delay, + takeEvery, + waitFor, +} from "../mod.ts"; +import { createStore, updateStore } from "../store/mod.ts"; +import { assertLike, describe, expect, it } from "../test.ts"; import type { Next, ThunkCtx } from "../mod.ts"; // deno-lint-ignore no-explicit-any @@ -121,6 +128,7 @@ it( tests, "when create a query fetch pipeline - execute all middleware and save to redux", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); api.use(convertNameToUrl); @@ -136,7 +144,7 @@ it( store.dispatch(fetchUsers()); - asserts.assertEquals(store.getState(), { + expect(store.getState()).toEqual({ users: { [mockUser.id]: deserializeUser(mockUser) }, tickets: {}, }); @@ -147,6 +155,7 @@ it( tests, "when providing a generator the to api.create function - should call that generator before all other middleware", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); api.use(convertNameToUrl); @@ -176,7 +185,7 @@ it( store.run(api.bootup); store.dispatch(fetchTickets()); - asserts.assertEquals(store.getState(), { + expect(store.getState()).toEqual({ users: { [mockUser.id]: deserializeUser(mockUser) }, tickets: { [mockTicket.id]: deserializeTicket(mockTicket) }, }); @@ -184,6 +193,7 @@ it( ); it(tests, "error handling", () => { + expect.assertions(1); let called; const api = createThunks(); api.use(api.routes()); @@ -203,10 +213,11 @@ it(tests, "error handling", () => { const store = createStore({ initialState: {} }); store.run(api.bootup); store.dispatch(action()); - asserts.assertStrictEquals(called, true); + expect(called).toBe(true); }); it(tests, "error handling inside create", () => { + expect.assertions(1); let called = false; const api = createThunks(); api.use(api.routes()); @@ -228,10 +239,11 @@ it(tests, "error handling inside create", () => { const store = createStore({ initialState: {} }); store.run(api.bootup); store.dispatch(action()); - asserts.assertStrictEquals(called, true); + expect(called).toBe(true); }); it(tests, "error inside endpoint mdw", () => { + expect.assertions(1); let called = false; const query = createThunks(); query.use(function* (_, next) { @@ -259,14 +271,15 @@ it(tests, "error inside endpoint mdw", () => { }); store.run(query.bootup); store.dispatch(fetchUsers()); - asserts.assertEquals(called, true); + expect(called).toBe(true); }); it(tests, "create fn is an array", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); api.use(function* (ctx, next) { - asserts.assertEquals(ctx.request, { + expect(ctx.request).toEqual({ method: "POST", body: { test: "me", @@ -330,51 +343,56 @@ it(tests, "run() on endpoint action - should run the effect", () => { }, }); expect(curCtx.name).toBe("/users"); - expect(curCtx.request).toMatchObject({ method: "expect this" }); + expect(curCtx.request).toEqual({ method: "expect this" }); }); -it(tests, "run() on endpoint action with payload - should run the effect", () => { - expect.assertions(4); - const api = createThunks(); - api.use(api.routes()); +it( + tests, + "run() on endpoint action with payload - should run the effect", + () => { + expect.assertions(4); + const api = createThunks(); + api.use(api.routes()); - let acc = ""; - let curCtx: RoboCtx = {} as RoboCtx; + let acc = ""; + let curCtx: RoboCtx = {} as RoboCtx; - const action1 = api.create<{ id: string }>( - "/users", - { supervisor: takeEvery }, - function* (ctx, next) { - yield* next(); - ctx.request = { method: "expect this" }; - acc += "a"; - }, - ); - const action2 = api.create( - "/users2", - { supervisor: takeEvery }, - function* (_, next) { - yield* next(); - curCtx = yield* call(() => action1.run({ id: "1" })); - acc += "b"; - }, - ); + const action1 = api.create<{ id: string }>( + "/users", + { supervisor: takeEvery }, + function* (ctx, next) { + yield* next(); + ctx.request = { method: "expect this" }; + acc += "a"; + }, + ); + const action2 = api.create( + "/users2", + { supervisor: takeEvery }, + function* (_, next) { + yield* next(); + curCtx = yield* call(() => action1.run({ id: "1" })); + acc += "b"; + }, + ); - const store = createStore({ initialState: {} }); - store.run(api.bootup); - store.dispatch(action2()); - expect(acc).toBe("ab"); - expect(curCtx.action).toMatchObject({ - type: `${API_ACTION_PREFIX}${action1}`, - payload: { - name: "/users", - }, - }); - expect(curCtx.name).toBe("/users"); - expect(curCtx.request).toMatchObject({ method: "expect this" }); -}); + const store = createStore({ initialState: {} }); + store.run(api.bootup); + store.dispatch(action2()); + expect(acc).toBe("ab"); + expect(curCtx.action).toMatchObject({ + type: `${API_ACTION_PREFIX}${action1}`, + payload: { + name: "/users", + }, + }); + expect(curCtx.name).toBe("/users"); + expect(curCtx.request).toEqual({ method: "expect this" }); + }, +); it(tests, "middleware order of execution", async () => { + expect.assertions(1); let acc = ""; const api = createThunks(); api.use(api.routes()); @@ -411,10 +429,11 @@ it(tests, "middleware order of execution", async () => { store.dispatch(action()); await store.run(waitFor(() => acc === "abcdefg")); - asserts.assert(acc === "abcdefg"); + expect(acc).toBe("abcdefg"); }); it(tests, "retry with actionFn", async () => { + expect.assertions(1); let acc = ""; let called = false; @@ -440,10 +459,11 @@ it(tests, "retry with actionFn", async () => { store.dispatch(action()); await store.run(waitFor(() => acc === "agag")); - asserts.assertEquals(acc, "agag"); + expect(acc).toBe("agag"); }); it(tests, "retry with actionFn with payload", async () => { + expect.assertions(1); let acc = ""; const api = createThunks(); api.use(api.routes()); @@ -470,10 +490,11 @@ it(tests, "retry with actionFn with payload", async () => { store.dispatch(action({ page: 1 })); await store.run(waitFor(() => acc === "agag")); - asserts.assertEquals(acc, "agag"); + expect(acc).toBe("agag"); }); it(tests, "should only call thunk once", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); let acc = ""; @@ -498,10 +519,11 @@ it(tests, "should only call thunk once", () => { const store = createStore({ initialState: {} }); store.run(api.bootup); store.dispatch(action2()); - asserts.assertEquals(acc, "a"); + expect(acc).toBe("a"); }); it(tests, "should be able to create thunk after `register()`", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); const store = createStore({ initialState: {} }); @@ -512,10 +534,11 @@ it(tests, "should be able to create thunk after `register()`", () => { acc += "a"; }); store.dispatch(action()); - asserts.assertEquals(acc, "a"); + expect(acc).toBe("a"); }); it(tests, "should warn when calling thunk before registered", () => { + expect.assertions(1); const err = console.warn; let called = false; console.warn = () => { @@ -527,11 +550,12 @@ it(tests, "should warn when calling thunk before registered", () => { const action = api.create("/users"); store.dispatch(action()); - asserts.assertEquals(called, true); + expect(called).toBe(true); console.warn = err; }); it(tests, "it should call the api once even if we register it twice", () => { + expect.assertions(1); const api = createThunks(); api.use(api.routes()); const store = createStore({ initialState: {} }); @@ -543,13 +567,14 @@ it(tests, "it should call the api once even if we register it twice", () => { acc += "a"; }); store.dispatch(action()); - asserts.assertEquals(acc, "a"); + expect(acc).toBe("a"); }); it( tests, "Should call the API only once, even if registered multiple times, with multiple APIs defined.", () => { + expect.assertions(2); const api1 = createThunks(); api1.use(api1.routes()); @@ -571,11 +596,7 @@ it( }); store.dispatch(action()); - asserts.assertEquals( - acc, - "b", - "Expected 'b' after first API call, but got: " + acc, - ); + expect(acc).toBe("b"); let acc2 = ""; const action2 = api2.create("/users", function* () { @@ -583,11 +604,7 @@ it( }); store.dispatch(action2()); - asserts.assertEquals( - acc2, - "c", - "Expected 'c' after second API call, but got: " + acc2, - ); + expect(acc2).toBe("c"); }, ); @@ -595,6 +612,7 @@ it( tests, "should unregister the thunk when the registration function exits", async () => { + expect.assertions(1); const api1 = createThunks(); api1.use(api1.routes()); @@ -609,15 +627,12 @@ it( }); store.dispatch(action()); - asserts.assertEquals( - acc, - "b", - "Expected 'b' after first API call, but got: " + acc, - ); + expect(acc).toBe("b"); }, ); it(tests, "should allow multiple stores to register a thunk", () => { + expect.assertions(1); const api1 = createThunks(); api1.use(api1.routes()); const storeA = createStore({ initialState: {} }); @@ -631,9 +646,5 @@ it(tests, "should allow multiple stores to register a thunk", () => { storeA.dispatch(action()); storeB.dispatch(action()); - asserts.assertEquals( - acc, - "bb", - "Expected 'bb' after first API call, but got: " + acc, - ); + expect(acc).toBe("bb"); });