-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #496 from huwshimi/client-identity-form-tests
WD-17058 - chore(tests): add tests for client and identity forms
- Loading branch information
Showing
6 changed files
with
151 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { screen } from "@testing-library/dom"; | ||
import { render } from "@testing-library/react"; | ||
import { faker } from "@faker-js/faker"; | ||
import { Formik } from "formik"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
import ClientForm from "./ClientForm"; | ||
import { ClientFormTypes } from "./ClientForm"; | ||
import { Label } from "./types"; | ||
|
||
const initialValues = { | ||
client_uri: "", | ||
client_name: "", | ||
grant_types: [], | ||
response_types: [], | ||
scope: "", | ||
redirect_uris: [], | ||
request_object_signing_alg: "", | ||
}; | ||
|
||
test("submits field data", async () => { | ||
const values = { | ||
client_uri: faker.word.sample(), | ||
client_name: faker.word.sample(), | ||
scope: faker.word.sample(), | ||
redirect_uris: [faker.word.sample()], | ||
request_object_signing_alg: faker.word.sample(), | ||
}; | ||
const onSubmit = vi.fn(); | ||
render( | ||
<Formik<ClientFormTypes> initialValues={initialValues} onSubmit={onSubmit}> | ||
{(formik) => ( | ||
<> | ||
<ClientForm formik={formik} /> | ||
<button onClick={() => void formik.submitForm()}>Submit</button> | ||
</> | ||
)} | ||
</Formik>, | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.NAME }), | ||
values.client_name, | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.SCOPE }), | ||
values.scope, | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.REDIRECT_URI }), | ||
values.redirect_uris[0], | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.CLIENT_URI }), | ||
values.client_uri, | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.SIGNING_ALGORITHM }), | ||
values.request_object_signing_alg, | ||
); | ||
await userEvent.click( | ||
screen.getByRole("checkbox", { name: "authorization_code" }), | ||
); | ||
await userEvent.click(screen.getByRole("checkbox", { name: "id_token" })); | ||
await userEvent.click(screen.getByRole("button")); | ||
expect(onSubmit.mock.calls[0][0]).toMatchObject({ | ||
...values, | ||
grant_types: ["authorization_code"], | ||
response_types: ["id_token"], | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum Label { | ||
NAME = "Name", | ||
SCOPE = "Scope", | ||
REDIRECT_URI = "Redirect uri", | ||
CLIENT_URI = "Client uri", | ||
SIGNING_ALGORITHM = "Signing algorithm", | ||
GRANT_TYPES = "Grant types", | ||
RESPONSE_TYPES = "Response types", | ||
} |
57 changes: 57 additions & 0 deletions
57
ui/src/pages/identities/IdentityForm/IdentityForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { screen } from "@testing-library/dom"; | ||
import { faker } from "@faker-js/faker"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { Formik } from "formik"; | ||
|
||
import IdentityForm from "./IdentityForm"; | ||
import { IdentityFormTypes } from "./IdentityForm"; | ||
import { Label } from "./types"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
import { axiosInstance } from "api/axios"; | ||
import { mockSchema } from "test/mocks/schemas"; | ||
import { renderComponent } from "test/utils"; | ||
|
||
const mock = new MockAdapter(axiosInstance); | ||
|
||
const initialValues = { | ||
email: "", | ||
schemaId: "", | ||
}; | ||
|
||
beforeEach(() => { | ||
mock.reset(); | ||
}); | ||
|
||
test("submits field data", async () => { | ||
const schemas = [mockSchema(), mockSchema()]; | ||
mock.onGet("/schemas?page_token=&page_size=50").reply(200, { data: schemas }); | ||
const values = { | ||
email: faker.word.sample(), | ||
schemaId: schemas[0].id, | ||
}; | ||
const onSubmit = vi.fn(); | ||
renderComponent( | ||
<Formik<IdentityFormTypes> | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
> | ||
{(formik) => ( | ||
<> | ||
<IdentityForm formik={formik} /> | ||
<button onClick={() => void formik.submitForm()}>Submit</button> | ||
</> | ||
)} | ||
</Formik>, | ||
); | ||
await userEvent.type( | ||
screen.getByRole("textbox", { name: Label.EMAIL }), | ||
values.email, | ||
); | ||
await screen.findByRole("option", { name: schemas[0].id }); | ||
await userEvent.selectOptions( | ||
screen.getByRole("combobox", { name: Label.SCHEMA }), | ||
values.schemaId, | ||
); | ||
await userEvent.click(screen.getByRole("button")); | ||
expect(onSubmit.mock.calls[0][0]).toMatchObject(values); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Label { | ||
EMAIL = "Email", | ||
SCHEMA = "Schema", | ||
} |