Skip to content

Commit

Permalink
Add & fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Nov 21, 2024
1 parent ef0b190 commit 940f5a8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,76 @@ vi.mock("@peculiar/certificates-viewer-react", () => ({
}));

describe("<CertificateViewerDialog />", () => {
const certificate = {
raw: new ArrayBuffer(0),
subjectName: "Certificate name",
type: "x509",
label: "Certificate name",
subject: {
commonName: "Certificate name",
const certificates = [
{
raw: new ArrayBuffer(0),
subjectName: "Certificate name 1",
type: "x509",
label: "Certificate name 1",
subject: {
commonName: "Certificate name 1",
},
},
} as unknown as CertificateProps;
{
raw: new ArrayBuffer(0),
subjectName: "Certificate name 2",
type: "x509",
label: "Certificate name 2",
subject: {
commonName: "Certificate name 2",
},
},
] as unknown as CertificateProps[];

it("Should render as x509 and handle close", async () => {
const onCloseMock = vi.fn();

render(
<CertificateViewerDialog
certificates={[certificate]}
certificates={[certificates[0]]}
onClose={onCloseMock}
/>
);

expect(screen.getByText(`“${certificate.label}” details`));
expect(screen.getByText(/x509 certificate viewer component/));
expect(
screen.getByText(`“${certificates[0].label}” details`)
).toBeInTheDocument();
expect(
screen.getByText(/x509 certificate viewer component/)
).toBeInTheDocument();

await userEvent.click(screen.getByRole("button", { name: /Close/ }));

expect(onCloseMock).toBeCalledTimes(1);
});

it("Should render & switches tabs", async () => {
render(
<CertificateViewerDialog certificates={certificates} onClose={vi.fn()} />
);

expect(
screen.getByText(`${certificates[0].label}`).closest("button")
).toHaveAttribute("aria-selected", "true");

await userEvent.click(screen.getByText(`${certificates[1].label}`));

expect(
screen.getByText(`${certificates[1].label}`).closest("button")
).toHaveAttribute("aria-selected", "true");
});

it("Should render as CSR", () => {
render(
<CertificateViewerDialog
certificates={[
{ ...certificate, type: "csr" } as unknown as CertificateProps,
{ ...certificates[0], type: "csr" } as unknown as CertificateProps,
]}
onClose={vi.fn()}
/>
);
expect(screen.getByText(/CSR certificate viewer component/));
expect(
screen.getByText(/CSR certificate viewer component/)
).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("useCertificateViewerDialog", () => {
providerId: providers[0].id,
};

it("Should initialize", () => {
it("Should initialize, open & close dialog", () => {
const { result } = renderHook(() =>
useCertificateViewerDialog(defaultProps)
);
Expand All @@ -60,9 +60,12 @@ describe("useCertificateViewerDialog", () => {

expect(DialogComponent).not.toBeNull();
expect(DialogComponent?.props.certificates).toStrictEqual([certificate]);

DialogComponent?.props.onClose();
expect(result.current.dialog()).toBeNull();
});

it("should close the dialog when the current provider is not found", () => {
it("Should close the dialog when the current provider is not found", () => {
const { result } = renderHook(() =>
useCertificateViewerDialog(defaultProps)
);
Expand All @@ -78,7 +81,7 @@ describe("useCertificateViewerDialog", () => {
expect(DialogComponent).toBeNull();
});

it("should make a chain of certificates", async () => {
it("Should make a chain of certificates", async () => {
const mockFortifyClient: Partial<FortifyAPI> = {
getProviderById: vi.fn().mockResolvedValue({
certStorage: {
Expand Down

0 comments on commit 940f5a8

Please sign in to comment.