Skip to content

Commit

Permalink
Extract mock user api
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-wratt committed Apr 11, 2024
1 parent 7febfa0 commit 67c2fbb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/mock/api/post-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { authenticationMock, FactorList } from "./authentication";
import { idTokenMock } from "./id-token";
import { multifactorMock } from "./multifactor";
import { redirectMock } from "./redirect";
import { userMock } from "./user";

export interface PostLoginOptions {
user?: Auth0.User;
Expand Down Expand Up @@ -102,9 +103,10 @@ export function postLogin({
const idToken = idTokenMock("PostLogin");
const multifactor = multifactorMock("PostLogin");
const redirect = redirectMock("PostLogin", { now, request: requestValue, user: userValue });
const userApiMock = userMock("PostLogin", { user: userValue });

const state: PostLoginState = {
user: userValue,
user: userApiMock.state,
access: access.state,
accessToken: accessToken.state,
authentication: authentication.state,
Expand Down Expand Up @@ -207,17 +209,8 @@ export function postLogin({

samlResponse,

user: {
setAppMetadata: (key, value) => {
state.user.app_metadata ??= {};
state.user.app_metadata[key] = value;
return api;
},
setUserMetadata: (key, value) => {
state.user.user_metadata ??= {};
state.user.user_metadata[key] = value;
return api;
},
get user() {
return userApiMock.build(api);
},

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

export function userMock(flow: string, { user }: { user: User}) {
const state = user

const build = <T>(api: T) => ({
setAppMetadata: (key: string, value: unknown) => {
state.app_metadata ??= {};
state.app_metadata[key] = value;
return api;
},
setUserMetadata: (key: string, value: unknown) => {
state.user_metadata ??= {};
state.user_metadata[key] = value;
return api;
},
});

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

test("user", async (t) => {
const baseApi = Symbol("Base API");

await t.test('setAppMetadata', (t) => {
const { build, state } = userMock("Another Flow", { user: user() });
const api = build(baseApi);

strictEqual(api.setAppMetadata("name", "Alice"), baseApi, "Expected base api to be returned");

deepStrictEqual(state.app_metadata, { name: "Alice" }, "Expected app metadata to be set");
});

await t.test('setUserMetadata', (t) => {
const { build, state } = userMock("Another Flow", { user: user() });
const api = build(baseApi);

strictEqual(api.setUserMetadata("name", "Alice"), baseApi, "Expected base api to be returned");

deepStrictEqual(state.user_metadata, { name: "Alice" }, "Expected user metadata to be set");
});
});

0 comments on commit 67c2fbb

Please sign in to comment.