Skip to content

Commit

Permalink
chore(tests): add tests for the provider list
Browse files Browse the repository at this point in the history
  • Loading branch information
huwshimi committed Dec 8, 2024
1 parent 3bb0555 commit 0f11b62
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 10 deletions.
3 changes: 2 additions & 1 deletion ui/src/components/EditPanelButton/EditPanelButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import usePanelParams from "util/usePanelParams";

import { Label, Props } from "./types";

const EditPanelButton: FC<Props> = ({ openPanel }: Props) => {
const EditPanelButton: FC<Props> = ({ openPanel, ...props }: Props) => {
const panelParams = usePanelParams();

return (
<Button
className="u-no-margin--bottom"
hasIcon
onClick={() => openPanel(panelParams)}
{...props}
>
<Icon name="edit" />
<span>{Label.EDIT}</span>
Expand Down
1 change: 1 addition & 0 deletions ui/src/pages/providers/DeleteProviderBtn/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from "./DeleteProviderBtn";
export { TestId as DeleteProviderBtnTestId } from "./test-types";
3 changes: 3 additions & 0 deletions ui/src/pages/providers/DeleteProviderBtn/test-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum TestId {
COMPONENT = "DeleteProviderBtn",
}
4 changes: 4 additions & 0 deletions ui/src/pages/providers/EditProviderBtn/EditProviderBtn.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { FC } from "react";

import EditPanelButton from "components/EditPanelButton";
import { testId } from "test/utils";

import { TestId } from "./test-types";

interface Props {
providerId: string;
Expand All @@ -10,6 +13,7 @@ const EditProviderBtn: FC<Props> = ({ providerId }) => {
return (
<EditPanelButton
openPanel={(panelParams) => panelParams.openProviderEdit(providerId)}
{...testId(TestId.COMPONENT)}
/>
);
};
Expand Down
1 change: 1 addition & 0 deletions ui/src/pages/providers/EditProviderBtn/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from "./EditProviderBtn";
export { TestId as EditProviderBtnTestId } from "./test-types";
3 changes: 3 additions & 0 deletions ui/src/pages/providers/EditProviderBtn/test-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum TestId {
COMPONENT = "EditProviderBtn",
}
76 changes: 76 additions & 0 deletions ui/src/pages/providers/ProviderList/ProviderList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { screen } 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 { mockIdentityProvider } from "test/mocks/providers";
import { mockPaginatedResponse } from "test/mocks/responses";
import { renderComponent } from "test/utils";

import { DeleteProviderBtnTestId } from "../DeleteProviderBtn";
import { EditProviderBtnTestId } from "../EditProviderBtn";

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

const mock = new MockAdapter(axiosInstance);

beforeEach(() => {
mock.reset();
mock.onGet("/idps").reply(200, mockPaginatedResponse([], { _meta: {} }));
});

test("displays provider rows", async () => {
const provider = {
id: faker.word.sample(),
provider: faker.word.sample(),
};
const providers = [mockIdentityProvider(provider)];
mock
.onGet("/idps")
.reply(200, mockPaginatedResponse(providers, { _meta: {} }));
renderComponent(<ProviderList />);
const row = await screen.findByRole("row", {
name: new RegExp(provider.id),
});
expect(
customWithin(row).getCellByHeader(Label.HEADER_NAME, { role: "rowheader" }),
).toHaveTextContent(provider.id);
expect(
customWithin(row).getCellByHeader(Label.HEADER_PROVIDER, {
role: "gridcell",
hasRowHeader: true,
}),
).toHaveTextContent(provider.provider);
});

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

test("displays edit and delete buttons", async () => {
const providers = [mockIdentityProvider()];
mock
.onGet("/idps")
.reply(200, mockPaginatedResponse(providers, { _meta: {} }));
renderComponent(<ProviderList />);
expect(
await screen.findByTestId(EditProviderBtnTestId.COMPONENT),
).toBeInTheDocument();
expect(
await screen.findByTestId(DeleteProviderBtnTestId.COMPONENT),

Check failure on line 74 in ui/src/pages/providers/ProviderList/ProviderList.test.tsx

View workflow job for this annotation

GitHub Actions / Test (20.x)

src/pages/providers/ProviderList/ProviderList.test.tsx > displays edit and delete buttons

TestingLibraryElementError: Unable to find an element by: [data-testid="DeleteProviderBtn"] 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" > Identity Providers </h1> </div> <div class="p-panel__controls" > <button class="p-button--positive" > Add ID provider </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 aria-sort="none" role="columnheader" > Name </th> <th aria-sort="none" role="columnheader" > Provider </th> <th role="columnheader" > Actions </th> </tr> </thead> <tbody> <tr role="row" > <td aria-hidden="false" class="" data-heading="Name" role="rowheader" /> <td aria-hidden="false" class="" data-heading="Provider" role="gridcell" /> <td aria-hidden="false" class="" data-heading="Actions" role="gridcell" > <button class="p-button has-icon u-no-margin--bottom" data-testid="EditProviderBtn" > <i class="p-icon--edit" /> <span> Edit </span> </button> <button class="p-button u-no-margin--bottom" title="Confirm delete" > 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" > Identity Providers </h1> </div> <div class="p-panel__controls" > <button class="p-button--positive" > Add ID provider </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 aria-sort="none" role="columnheader" > Name </th> <th aria-sort="none"
).toBeInTheDocument();
});
14 changes: 5 additions & 9 deletions ui/src/pages/providers/ProviderList/ProviderList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { fetchProviders } from "api/provider";
import usePanelParams from "util/usePanelParams";
import EditProviderBtn from "pages/providers/EditProviderBtn";
import DeleteProviderBtn from "pages/providers/DeleteProviderBtn";
import { Label } from "./types";

const ProviderList: FC = () => {
const panelParams = usePanelParams();
Expand All @@ -34,7 +35,7 @@ const ProviderList: FC = () => {
appearance="positive"
onClick={panelParams.openProviderCreate}
>
Add ID provider
{Label.ADD}
</Button>
</div>
</div>
Expand All @@ -47,22 +48,19 @@ const ProviderList: FC = () => {
sortable
responsive
headers={[
{ content: "Name", sortKey: "id" },
{ content: "Provider", sortKey: "provider" },
{ content: "Actions" },
{ content: Label.HEADER_NAME, sortKey: "id" },
{ content: Label.HEADER_PROVIDER, sortKey: "provider" },
{ content: Label.HEADER_ACTIONS },
]}
rows={providers?.data.map((provider) => {
return {
columns: [
{
content: provider.id,
role: "rowheader",
"aria-label": "Name",
},
{
content: provider.provider,
role: "rowheader",
"aria-label": "Provider",
},
{
content: (
Expand All @@ -71,8 +69,6 @@ const ProviderList: FC = () => {
<DeleteProviderBtn provider={provider} />
</>
),
role: "rowheader",
"aria-label": "Actions",
},
],
sortData: {
Expand Down
6 changes: 6 additions & 0 deletions ui/src/pages/providers/ProviderList/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum Label {
ADD = "Add ID provider",
HEADER_ACTIONS = "Actions",
HEADER_NAME = "Name",
HEADER_PROVIDER = "Provider",
}

0 comments on commit 0f11b62

Please sign in to comment.