Skip to content

Commit

Permalink
chore(tests): add tests for the identity list
Browse files Browse the repository at this point in the history
  • Loading branch information
huwshimi committed Dec 9, 2024
1 parent 3bb0555 commit 8d4a9d5
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions ui/src/pages/identities/DeleteIdentityBtn/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from "./DeleteIdentityBtn";
export { TestId as DeleteIdentityBtnTestId } from "./test-types";
3 changes: 3 additions & 0 deletions ui/src/pages/identities/DeleteIdentityBtn/test-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum TestId {
COMPONENT = "DeleteIdentityBtn",
}
95 changes: 95 additions & 0 deletions ui/src/pages/identities/IdentityList/IdentityList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { screen, within } from "@testing-library/dom";
import MockAdapter from "axios-mock-adapter";
import userEvent from "@testing-library/user-event";
import { faker } from "@faker-js/faker";

import { axiosInstance } from "api/axios";
import { customWithin } from "test/queries/within";
import { isoTimeToString } from "util/date";
import { LoaderTestId } from "components/Loader";
import { mockIdentity } from "test/mocks/identities";
import { mockPaginatedResponse } from "test/mocks/responses";
import { PAGE_SIZE } from "util/api";
import { renderComponent } from "test/utils";

import { DeleteIdentityBtnTestId } from "../DeleteIdentityBtn";

import { Label } from "./types";
import IdentityList from "./IdentityList";
import { Location } from "react-router-dom";
import { panels } from "util/usePanelParams";

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
mock
.onGet(`/identities?page_token=&page_size=${PAGE_SIZE}`)
.reply(200, mockPaginatedResponse([], { _meta: {} }));
});

test("displays loading state", () => {
renderComponent(<IdentityList />);
expect(screen.getByTestId(LoaderTestId.COMPONENT)).toBeInTheDocument();
});

test("displays empty state", async () => {
renderComponent(<IdentityList />);
expect(
await within(await screen.findByRole("grid")).findByText(Label.NO_DATA),
).toBeInTheDocument();
});

test("displays identity rows", async () => {
const createdAt = faker.date.anytime().toISOString();
const identity = mockIdentity({
created_at: createdAt,
});
const identities = [identity];
mock
.onGet(`/identities?page_token=&size=${PAGE_SIZE}`)
.reply(200, mockPaginatedResponse(identities, { _meta: {} }));
renderComponent(<IdentityList />);
const row = await screen.findByRole("row", {
name: new RegExp(identity.id),
});
expect(
customWithin(row).getCellByHeader(Label.HEADER_ID, { role: "rowheader" }),
).toHaveTextContent(identity.id);
expect(
customWithin(row).getCellByHeader(Label.HEADER_SCHEMA, {
role: "gridcell",
hasRowHeader: true,
}),
).toHaveTextContent(identity.schema_id);
expect(
customWithin(row).getCellByHeader(Label.HEADER_CREATED_AT, {
role: "gridcell",
hasRowHeader: true,
}),
).toHaveTextContent(isoTimeToString(createdAt));
});

test("opens add identity panel", async () => {
let location: Location | null = null;
renderComponent(<IdentityList />, {
setLocation: (newLocation) => {
location = newLocation;
},
});
await userEvent.click(screen.getByRole("button", { name: Label.ADD }));
expect((location as Location | null)?.search).toBe(
`?panel=${panels.identityCreate}`,
);
});

test("displays delete button", async () => {
const identities = [mockIdentity()];
mock
.onGet(`/identities?page_token=&size=${PAGE_SIZE}`)
.reply(200, mockPaginatedResponse(identities, { _meta: {} }));
renderComponent(<IdentityList />);
expect(
await screen.findByTestId(DeleteIdentityBtnTestId.COMPONENT),

Check failure on line 93 in ui/src/pages/identities/IdentityList/IdentityList.test.tsx

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/pages/identities/IdentityList/IdentityList.test.tsx > displays delete button

TestingLibraryElementError: Unable to find an element by: [data-testid="DeleteIdentityBtn"] Ignored nodes: comments, script, style <body> <div> <div class="p-panel" > <div class="p-panel__header " > <div class="p-panel__title" > <h1 class="p-heading--4 u-no-margin--bottom" > Identities </h1> </div> <div class="p-panel__controls" > <button class="p-button--positive" > Add identity </button> </div> </div> <div class="p-panel__content" > <div class="row" > <div class="col-12" > <table class="u-table-layout--auto p-table--mobile-card" role="grid" > <thead> <tr role="row" > <th role="columnheader" > Id </th> <th role="columnheader" > Schema </th> <th role="columnheader" > Created at </th> <th role="columnheader" > Actions </th> </tr> </thead> <tbody> <tr role="row" > <td aria-hidden="false" class="" data-heading="Id" role="rowheader" > unfortunate </td> <td aria-hidden="false" class="" data-heading="Schema" role="gridcell" > formula </td> <td aria-hidden="false" class="" data-heading="Created at" role="gridcell" /> <td aria-hidden="false" class="" data-heading="Actions" role="gridcell" > <button class="u-no-margin--bottom p-action-button p-button" title="Delete identity" > Delete </button> </td> </tr> </tbody> </table> </div> </div> </div> </div> </div> <div /> </body> Ignored nodes: comments, script, style <body> <div> <div class="p-panel" > <div class="p-panel__header " > <div class="p-panel__title" > <h1 class="p-heading--4 u-no-margin--bottom" > Identities </h1> </div> <div class="p-panel__controls" > <button class="p-button--positive" > Add identity </button> </div> </div> <div class="p-panel__content" > <div class="row" > <div class="col-12" > <table class="u-table-layout--auto p-table--mobile-card" role="grid" > <thead> <tr role="row" > <th role="columnheader" > Id </th> <th role="columnheader" > Schema </th>
).toBeInTheDocument();
});
20 changes: 7 additions & 13 deletions ui/src/pages/identities/IdentityList/IdentityList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import usePanelParams from "util/usePanelParams";
import { usePagination } from "util/usePagination";
import Pagination from "components/Pagination";
import DeleteIdentityBtn from "pages/identities/DeleteIdentityBtn";
import { Label } from "./types";

const IdentityList: FC = () => {
const panelParams = usePanelParams();
Expand All @@ -36,7 +37,7 @@ const IdentityList: FC = () => {
appearance="positive"
onClick={panelParams.openIdentityCreate}
>
Add identity
{Label.ADD}
</Button>
</div>
</div>
Expand All @@ -48,39 +49,32 @@ const IdentityList: FC = () => {
className="u-table-layout--auto"
responsive
headers={[
{ content: "Id" },
{ content: "Schema" },
{ content: "Created at" },
{ content: "Actions" },
{ content: Label.HEADER_ID },
{ content: Label.HEADER_SCHEMA },
{ content: Label.HEADER_CREATED_AT },
{ content: Label.HEADER_ACTIONS },
]}
rows={response?.data.map((identity) => {
return {
columns: [
{
content: identity.traits?.email ?? identity.id,
role: "rowheader",
"aria-label": "Id",
},
{
content: identity.schema_id,
role: "rowheader",
"aria-label": "Schema",
},
{
content: identity.created_at
? isoTimeToString(identity.created_at)
: "",
role: "rowheader",
"aria-label": "Created at",
},
{
content: (
<>
<DeleteIdentityBtn identity={identity} />
</>
),
role: "rowheader",
"aria-label": "Actions",
},
],
};
Expand All @@ -89,7 +83,7 @@ const IdentityList: FC = () => {
isLoading ? (
<Loader text="Loading identities..." />
) : (
"No data to display"
Label.NO_DATA
)
}
/>
Expand Down
8 changes: 8 additions & 0 deletions ui/src/pages/identities/IdentityList/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum Label {
ADD = "Add identity",
HEADER_ACTIONS = "Actions",
HEADER_CREATED_AT = "Created at",
HEADER_ID = "Id",
HEADER_SCHEMA = "Schema",
NO_DATA = "No data to display",
}

0 comments on commit 8d4a9d5

Please sign in to comment.