Skip to content

Commit

Permalink
Extract mock validation api
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-wratt committed Apr 10, 2024
1 parent 0886434 commit 05345c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/mock/api/post-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { multifactorMock } from "./multifactor";
import { redirectMock } from "./redirect";
import { userMock } from "./user";
import { samlResponseMock } from "./saml-response";
import { validationMock } from "./validation";

export interface PostLoginOptions {
user?: Auth0.User;
Expand Down Expand Up @@ -106,6 +107,7 @@ export function postLogin({
const redirect = redirectMock("PostLogin", { now, request: requestValue, user: userValue });
const userApiMock = userMock("PostLogin", { user: userValue });
const samlResponse = samlResponseMock("PostLogin");
const validation = validationMock("PostLogin");

const state: PostLoginState = {
user: userApiMock.state,
Expand All @@ -116,9 +118,7 @@ export function postLogin({
idToken: idToken.state,
multifactor: multifactor.state,
samlResponse: samlResponse.state,
validation: {
error: null,
},
validation: validation.state,
redirect: redirect.state,
};

Expand Down Expand Up @@ -163,11 +163,8 @@ export function postLogin({
return userApiMock.build(api);
},

validation: {
error: (code, message) => {
state.validation.error = { code, message };
return api;
},
get validation() {
return validation.build(api);
},
};

Expand Down
16 changes: 16 additions & 0 deletions src/mock/api/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { error } from "console";

export function validationMock(flow: string) {
const state = {
error: null as null | { code: string; message: string },
};

const build = <T>(api: T) => ({
error: (code: string, message: string) => {
state.error = { code, message };
return api;
},
});

return { state, build };
}
20 changes: 20 additions & 0 deletions src/test/api/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from "node:test";
import { strictEqual, deepStrictEqual } from "node:assert";
import { validationMock } from "../../mock/api/validation";

test("Validation", async (t) => {
await t.test("error", async (t) => {
const baseApi = Symbol("Base API");
const { state, build } = validationMock("Another Flow");
const api = build(baseApi);

strictEqual(state.error, null);

strictEqual(api.error("E_KABOOM", "Something went wrong"), baseApi);

deepStrictEqual(state.error, {
code: "E_KABOOM",
message: "Something went wrong",
});
});
});

0 comments on commit 05345c5

Please sign in to comment.