From 05345c520894fcce2dd08b964a0188eee91d1ce4 Mon Sep 17 00:00:00 2001 From: Matthew Wratt Date: Thu, 11 Apr 2024 10:44:19 +1200 Subject: [PATCH] Extract mock validation api --- src/mock/api/post-login.ts | 13 +++++-------- src/mock/api/validation.ts | 16 ++++++++++++++++ src/test/api/validation.test.ts | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 src/mock/api/validation.ts create mode 100644 src/test/api/validation.test.ts diff --git a/src/mock/api/post-login.ts b/src/mock/api/post-login.ts index f7e942a..54f3d78 100644 --- a/src/mock/api/post-login.ts +++ b/src/mock/api/post-login.ts @@ -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; @@ -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, @@ -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, }; @@ -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); }, }; diff --git a/src/mock/api/validation.ts b/src/mock/api/validation.ts new file mode 100644 index 0000000..220c28c --- /dev/null +++ b/src/mock/api/validation.ts @@ -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 = (api: T) => ({ + error: (code: string, message: string) => { + state.error = { code, message }; + return api; + }, + }); + + return { state, build }; +} diff --git a/src/test/api/validation.test.ts b/src/test/api/validation.test.ts new file mode 100644 index 0000000..284cc73 --- /dev/null +++ b/src/test/api/validation.test.ts @@ -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", + }); + }); +});