Improve mock and stories

This commit is contained in:
Joseph Garrone 2024-06-12 23:11:06 +02:00
parent f90dc8bc7e
commit c81c350136
3 changed files with 73 additions and 12 deletions

View File

@ -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 <T>(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: [

View File

@ -17,6 +17,36 @@ export const Default: Story = {
render: () => <KcPageStory />
};
export const WithInvalidCredential: Story = {
render: () => (
<KcPageStory
kcContext={{
message: {
summary: "Invalid username or password.",
type: "error"
},
login: {
username: "johndoe"
},
messagesPerField: {
// 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[]) => {
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: () => (
<KcPageStory
@ -180,3 +210,16 @@ export const WithoutPasswordField: Story = {
/>
)
};
export const WithErrorMessage: Story = {
render: () => (
<KcPageStory
kcContext={{
message: {
summary: "Please restart the login process.",
type: "error"
}
}}
/>
)
};

View File

@ -17,22 +17,31 @@ export const Default: Story = {
render: () => <KcPageStory />
};
export const WithFieldError: Story = {
export const WithEmailAlreadyExists: Story = {
render: () => (
<KcPageStory
kcContext={{
profile: {
attributesByName: {
username: {
value: "johndoe"
},
email: {
value: "max.mustermann@gmail.com"
value: "jhon.doe@gmail.com"
},
firstName: {
value: "John"
},
lastName: {
value: "Doe"
}
}
},
messagesPerField: {
existsError: (fieldName: string) => fieldName === "email",
exists: (fieldName: string) => fieldName === "email",
get: (fieldName: string) => (fieldName === "email" ? "I don't like your email address" : undefined),
printIfExists: <T,>(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)
}
}}
/>