From c81c3501365e41a6f4b632c73e0b6150ca6f9f7a Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Wed, 12 Jun 2024 23:11:06 +0200 Subject: [PATCH] Improve mock and stories --- src/login/KcContext/kcContextMocks.ts | 21 ++++++++---- stories/login/pages/Login.stories.tsx | 43 ++++++++++++++++++++++++ stories/login/pages/Register.stories.tsx | 21 ++++++++---- 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/login/KcContext/kcContextMocks.ts b/src/login/KcContext/kcContextMocks.ts index 816055cd..a5b3c8b9 100644 --- a/src/login/KcContext/kcContextMocks.ts +++ b/src/login/KcContext/kcContextMocks.ts @@ -99,13 +99,22 @@ export const kcContextCommonMock: KcContext.Common = { registrationEmailAsUsername: false }, messagesPerField: { - printIfExists: () => { - return undefined; - }, + get: () => "", existsError: () => false, - get: fieldName => `Fake error for ${fieldName}`, - exists: () => false, - getFirstError: fieldName => `Fake error for ${fieldName}` + printIfExists: function (fieldName: string, text: T) { + return this.get(fieldName) !== "" ? text : undefined; + }, + exists: function (fieldName) { + return this.get(fieldName) !== ""; + }, + getFirstError: function (...fieldNames) { + for (const fieldName of fieldNames) { + if (this.existsError(fieldName)) { + return this.get(fieldName); + } + } + return ""; + } }, locale: { supported: [ diff --git a/stories/login/pages/Login.stories.tsx b/stories/login/pages/Login.stories.tsx index 7c9fc242..22a89d05 100644 --- a/stories/login/pages/Login.stories.tsx +++ b/stories/login/pages/Login.stories.tsx @@ -17,6 +17,36 @@ export const Default: Story = { render: () => }; +export const WithInvalidCredential: Story = { + render: () => ( + { + const fieldNames = [fieldName, ...otherFieldNames]; + return fieldNames.includes("username") || fieldNames.includes("password"); + }, + get: (fieldName: string) => { + if (fieldName === "username" || fieldName === "password") { + return "Invalid username or password."; + } + return ""; + } + } + }} + /> + ) +}; + export const WithoutRegistration: Story = { render: () => ( ) }; + +export const WithErrorMessage: Story = { + render: () => ( + + ) +}; diff --git a/stories/login/pages/Register.stories.tsx b/stories/login/pages/Register.stories.tsx index 3aa91f19..b54b4ee4 100644 --- a/stories/login/pages/Register.stories.tsx +++ b/stories/login/pages/Register.stories.tsx @@ -17,22 +17,31 @@ export const Default: Story = { render: () => }; -export const WithFieldError: Story = { +export const WithEmailAlreadyExists: Story = { render: () => ( fieldName === "email", - exists: (fieldName: string) => fieldName === "email", - get: (fieldName: string) => (fieldName === "email" ? "I don't like your email address" : undefined), - printIfExists: (fieldName: string, x: T) => (fieldName === "email" ? x : undefined) + // NOTE: The other functions of messagesPerField are derived from get() and + // existsError() so they are the only ones that need to mock. + existsError: (fieldName: string, ...otherFieldNames: string[]) => [fieldName, ...otherFieldNames].includes("email"), + get: (fieldName: string) => (fieldName === "email" ? "Email already exists." : undefined) } }} />