Skip to content

Commit

Permalink
Merge pull request #82 from sinamics/more_tests
Browse files Browse the repository at this point in the history
Added more tests
  • Loading branch information
sinamics authored Aug 11, 2023
2 parents 22b3be0 + 8580c4e commit 53a0a78
Show file tree
Hide file tree
Showing 13 changed files with 1,860 additions and 987 deletions.
7 changes: 5 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"recommendations": ["prisma.prisma"]
}
"recommendations": [
"prisma.prisma",
"rome.rome"
]
}
1,877 changes: 1,036 additions & 841 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"tailwindcss": "^3.2.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"type-fest": "^4.2.0",
"typescript": "^5.1.6"
},
"ct3aMetadata": {
Expand Down
173 changes: 173 additions & 0 deletions src/__tests__/__mocks__/networkById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import "@testing-library/jest-dom";

jest.mock("../../utils/api", () => ({
api: {
admin: {
getAllOptions: {
useQuery: () => ({
data: {},
isLoading: false,
refetch: jest.fn(),
}),
},
},
networkMember: {
getNetworkMemberById: {
useQuery: () => ({
data: {},
isLoading: false,
refetch: jest.fn(),
}),
},
updateNetworkMember: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
deleteNetworkMember: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
stash: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
UpdateDatabaseOnly: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
create: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
delete: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
Update: {
useMutation: () => ({
mutate: jest.fn(),
}),
}
},
network: {
getNetworkById: {
useQuery: () => ({
data: {
network: {
nwid: "1234567890",
name: "Test Network",
private: true,
ipAssignmentPools: [
{ ipRangeStart: "10.0.0.1", ipRangeEnd: "10.0.0.254" },
],
routes: [{ target: "10.0.0.0/24" }],
dns: {
domain: "",
servers: [],
},
tags: [],
multicastLimit: 32,
enableBroadcast: true,
rutes: [
{
target: "172.25.28.0/24",
via: null,
},
],
rules: [
{
not: false,
or: false,
type: "ACTION_ACCEPT",
},
],
v4AssignMode: {
zt: false, // Changed state to checked
},
},
members: [],
zombieMembers: [],
},
isLoading: false,
refetch: jest.fn(),
}),
},
updateNetwork: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
getFlowRule: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
setFlowRule: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
deleteNetwork: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
inviteUserByMail: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
networkName: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
networkDescription: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
privatePublicNetwork: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
enableIpv4AutoAssign: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
easyIpAssignment: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
advancedIpAssignment: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
managedRoutes: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
dns: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
multiCast: {
useMutation: () => ({
mutate: jest.fn(),
}),
},
},
},
}));
6 changes: 3 additions & 3 deletions src/__tests__/components/loginForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ describe("LoginForm", () => {
});

it("renders the LoginForm component", () => {
render(<LoginForm />);
render(<LoginForm setViewForgotForm={() => {}} />);
expect(
screen.getByRole("heading", { name: /Sign In/i }),
).toBeInTheDocument();
});

it("updates email and password inputs on change", () => {
render(<LoginForm />);
render(<LoginForm setViewForgotForm={() => {}} />);

const emailInput = screen.getByPlaceholderText("[email protected]");
const passwordInput = screen.getByPlaceholderText("Enter your password");
Expand All @@ -41,7 +41,7 @@ describe("LoginForm", () => {
});

it("submits the form with correct email and password", async () => {
render(<LoginForm />);
render(<LoginForm setViewForgotForm={() => {}} />);

(signIn as jest.Mock).mockResolvedValue({});

Expand Down
92 changes: 92 additions & 0 deletions src/__tests__/components/networkDns.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import "../__mocks__/networkById";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import { NetworkDns } from "~/components/modules/networkDns";
import { NextRouter, useRouter } from "next/router";
import { api } from "~/utils/api";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { NextIntlProvider } from "next-intl";
import enTranslation from "~/locales/en/common.json";

// Mocking the next/router module
jest.mock("next/router", () => ({
useRouter: jest.fn(),
}));

const mockedRouter: Partial<NextRouter> = {
query: {
id: "test-id",
},
};
const mockUseRouter = useRouter as jest.MockedFunction<typeof useRouter>;
mockUseRouter.mockReturnValue(mockedRouter as NextRouter);

describe("<NetworkDns />", () => {
let queryClient: QueryClient;

beforeEach(() => {
queryClient = new QueryClient();
(useRouter as jest.Mock).mockImplementation(() => ({
query: {
id: "test-id",
},
}));
});
it("sends the correct DNS IP to the server when submitted", async () => {
const mockMutation = jest.fn();

const useQueryMock = jest.fn().mockReturnValue({
data: {
network: {
nwid: "test-id",
dns: {
domain: "",
servers: [],
},
},
},
isLoading: false,
refetch: jest.fn(),
});

api.network.getNetworkById.useQuery = useQueryMock;
api.network.dns.useMutation = jest.fn().mockReturnValue({
mutate: mockMutation,
});

render(
<QueryClientProvider client={queryClient}>
<NextIntlProvider locale="en" messages={enTranslation}>
<NetworkDns />
</NextIntlProvider>
</QueryClientProvider>,
);

// Simulate typing in the DNS IP address
const dnsInput = screen.getByPlaceholderText("10.147.20.190");
fireEvent.change(dnsInput, { target: { value: "192.168.1.1" } });

const dnsDomain = screen.getByPlaceholderText("home.arpa");
fireEvent.change(dnsDomain, { target: { value: "test-domain" } });

// Simulate clicking the submit button
const submitButton = screen.getByText("Submit");
fireEvent.click(submitButton);

// Verify the mock API mutation is called with the right value
await waitFor(() => {
expect(mockMutation).toHaveBeenCalledWith(
expect.objectContaining({
central: false,
nwid: "test-id",
updateParams: {
dns: {
domain: "test-domain",
servers: ["192.168.1.1"],
},
},
}),
expect.anything(),
);
});
});
});
Loading

0 comments on commit 53a0a78

Please sign in to comment.