Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-slobodian committed Nov 4, 2024
1 parent 267cc8d commit 1313904
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 12 deletions.
122 changes: 122 additions & 0 deletions src/app.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { render, screen, userEvent, waitFor } from "@testing";

import type { IProviderInfo } from "@peculiar/fortify-client-core";
import { FortifyAPI } from "@peculiar/fortify-client-core";
import { App } from "./app";

vi.mock("@peculiar/fortify-client-core");

describe("<App />", () => {
const providersMock = [
{
id: "provider1",
name: "Provider 1",
},
{
id: "provider2",
name: "Provider 2",
},
] as IProviderInfo[];

const certificatesMock = [
{
id: "1",
providerID: "provider1",
subject: {
CN: ["Certificate test 1"],
},
subjectName: "Certificate test 1",
raw: new ArrayBuffer(1),
type: "x509",
serialNumber: "1",
notAfter: new Date("2024-01-01"),
},
{
id: "2",
providerID: "provider1",
subject: {
CN: ["Certificate test 2"],
},
subjectName: "Certificate test 2",
raw: new ArrayBuffer(0),
type: "x509",
serialNumber: "2",
notAfter: new Date("2024-01-02"),
},
];

const mockFortifyAPIInstance: Partial<FortifyAPI> = {
challenge: vi.fn().mockResolvedValue(""),
login: vi.fn(),
getProviders: vi.fn().mockResolvedValue(providersMock),
isConnectionSupported: vi.fn().mockReturnValue(true),
isConnectionDetected: vi.fn().mockResolvedValue(true),
isConnectionDetectedAuto: vi.fn(),
connect: vi.fn(),
getProviderById: vi.fn().mockResolvedValue({
id: providersMock[0].id,
isLoggedIn: vi.fn().mockResolvedValue(true),
reset: vi.fn(),
login: vi.fn(),
logout: vi.fn(),
}),
getCertificatesByProviderId: vi.fn().mockResolvedValue(certificatesMock),
};

it("Should render, show providers & certificates", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Provider 1/)).toBeInTheDocument();
expect(screen.getByText(/Provider 2/)).toBeInTheDocument();
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
expect(screen.getByText(/Certificate test 2/)).toBeInTheDocument();
});
});

it("Should open provider info dialog", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

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

await waitFor(() => {
expect(screen.getByText(/Provider 1 information/)).toBeInTheDocument();
});
});

it("Should handle search & clear search", async () => {
vi.mocked(FortifyAPI).mockImplementation(
() => mockFortifyAPIInstance as FortifyAPI
);

render(<App />);

await waitFor(() => {
expect(screen.getByText(/Certificate test 1/)).toBeInTheDocument();
});

userEvent.type(screen.getByPlaceholderText("Search"), "test 1");

await waitFor(() => {
expect(screen.getAllByText("test 1")).toHaveLength(1);
});

await userEvent.click(screen.getByTestId("clear-search-button"));

await waitFor(() => {
expect(screen.queryByText("test 1")).not.toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ describe("useCertificateViewerDialog", () => {
},
] as IProviderInfo[];

it("Should initialize", () => {
const certificate = {
id: "1",
label: "Certificate name",
} as CertificateProps;

const defaultOpenProps = {
certificate,
providerId: providers[0].id,
};

it("Should initialize, open & close dialog", () => {
const { result } = renderHook(() =>
useCertificateViewerDialog({
providers,
Expand All @@ -22,21 +32,41 @@ describe("useCertificateViewerDialog", () => {
expect(result.current.dialog).toBeInstanceOf(Function);
expect(result.current.open).toBeInstanceOf(Function);

const certificate = {
id: "1",
label: "Certificate name",
} as CertificateProps;

act(() => {
result.current.open({
certificate,
providerId: providers[0].id,
});
result.current.open(defaultOpenProps);
});

const DialogComponent = result.current.dialog();

expect(DialogComponent).not.toBeNull();
expect(DialogComponent?.props.certificate).toBe(certificate);

act(() => {
DialogComponent?.props.onClose();
});

expect(result.current.dialog()).toBeNull();
});

it("Should close dialog if current provider is not found", async () => {
const { result, rerender } = renderHook(
(localProviders) =>
useCertificateViewerDialog({
providers: localProviders,
}),
{ initialProps: providers }
);

act(() => {
result.current.open(defaultOpenProps);
});

rerender([
{
id: "2",
},
] as IProviderInfo[]);

expect(result.current.dialog()).toBeNull();
});
});
30 changes: 29 additions & 1 deletion src/dialogs/provider-info-dialog/useProviderInfoDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("useProviderInfoDialog", () => {
},
] as IProviderInfo[];

it("Should initialize", async () => {
it("Should initialize, open & close", async () => {
const { result } = renderHook(() =>
useProviderInfoDialog({
providers,
Expand All @@ -29,5 +29,33 @@ describe("useProviderInfoDialog", () => {

expect(DialogComponent).not.toBeNull();
expect(DialogComponent?.props.data).toBe(providers[0]);

act(() => {
DialogComponent?.props.onDialogClose();
});

expect(result.current.dialog()).toBeNull();
});

it("Should close dialog if current provider is not found", async () => {
const { result, rerender } = renderHook(
(localProviders) =>
useProviderInfoDialog({
providers: localProviders,
}),
{ initialProps: providers }
);

act(() => {
result.current.open(providers[0]);
});

rerender([
{
id: "2",
},
] as IProviderInfo[]);

expect(result.current.dialog()).toBeNull();
});
});
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
exclude: [
"**/*.stories.*/**",
"**/*.test.tsx",
"**/index.ts",
"**/index.{ts,tsx}",
"**/types.ts",
"**/*.d.ts",
],
Expand Down

0 comments on commit 1313904

Please sign in to comment.