Skip to content

Commit

Permalink
Add mock prompt API for testing rendering of forms
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanKramer authored and Aupajo committed Nov 18, 2024
1 parent 96a09eb commit 79799e6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kilterset/auth0-actions-testing",
"version": "0.3.1",
"version": "0.3.2",
"description": "Test and develop Auth0 Actions or Okta CIC Actions locally.",
"repository": "https://github.com/kilterset/auth0-actions-testing",
"homepage": "https://github.com/kilterset/auth0-actions-testing#readme",
Expand Down
6 changes: 6 additions & 0 deletions src/mock/api/post-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { accessMock } from "./access";
import { authenticationMock, FactorList } from "./authentication";
import { idTokenMock } from "./id-token";
import { multifactorMock } from "./multifactor";
import { promptMock } from "./prompt";
import { redirectMock } from "./redirect";
import { userMock } from "./user";
import { samlResponseMock } from "./saml-response";
Expand Down Expand Up @@ -103,6 +104,7 @@ export function postLogin({
});
const idToken = idTokenMock("PostLogin");
const multifactor = multifactorMock("PostLogin");
const prompt = promptMock("PostLogin");
const redirect = redirectMock("PostLogin", {
now,
request: requestValue,
Expand Down Expand Up @@ -151,6 +153,10 @@ export function postLogin({
return multifactor.build(api);
},

get prompt() {
return prompt.build(api);
},

get redirect() {
return redirect.build(api);
},
Expand Down
20 changes: 20 additions & 0 deletions src/mock/api/prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MultifactorEnableOptions } from "../../types";

interface PromptState {
promptId: string;
promptOptions?: { [key: string]: unknown };
}

export function promptMock(flow: string) {
const state: { rendered: PromptState | null } = {
rendered: null,
};

const build = <T>(api: T) => ({
render: (promptId: string, promptOptions?: { [key: string]: unknown }) => {
state.rendered = { promptId, promptOptions };
},
});

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

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

await t.test("render", async (t) => {
const { build, state } = promptMock("Another Flow");
const api = build(baseApi);

strictEqual(state.rendered, null);

api.render("prompt-id", { fields: { foo: "bar" } });

ok(Boolean(state.rendered), "Expected prompt to be rendered");

deepStrictEqual(state.rendered, {
promptId: "prompt-id",
promptOptions: { fields: { foo: "bar" } },
});
});
});
4 changes: 4 additions & 0 deletions src/types/api/post-login.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export interface PostLogin {
enable(provider: string, options?: MultifactorEnableOptions): PostLogin;
};

readonly prompt: {
render(promptId: string, promptOptions?: { [key: string]: unknown }): void;
};

readonly rules: {
wasExecuted(ruleId: string): boolean;
};
Expand Down

0 comments on commit 79799e6

Please sign in to comment.