From f7ff567b5d6b9c2bad7144c7d504f63a61bfe7c7 Mon Sep 17 00:00:00 2001 From: Bernt Christian Egeland Date: Sun, 30 Jul 2023 18:26:23 +0000 Subject: [PATCH] updated tests --- src/__tests__/components/inputField.test.tsx | 2 +- src/__tests__/pages/network/[id].test.tsx | 67 +++++++++++++++++--- src/__tests__/pages/network/index.test.tsx | 16 ++++- src/components/elements/inputField.tsx | 6 +- src/components/modules/changeLocale.tsx | 10 --- src/components/modules/networkMulticast.tsx | 4 +- src/i18n.ts | 66 ------------------- src/pages/login/forgotpassword/index.tsx | 6 +- src/pages/user-settings/account/index.tsx | 4 +- src/types/locale.ts | 8 --- 10 files changed, 83 insertions(+), 106 deletions(-) delete mode 100644 src/components/modules/changeLocale.tsx delete mode 100644 src/i18n.ts delete mode 100644 src/types/locale.ts diff --git a/src/__tests__/components/inputField.test.tsx b/src/__tests__/components/inputField.test.tsx index 7b37328b..ae5c409e 100644 --- a/src/__tests__/components/inputField.test.tsx +++ b/src/__tests__/components/inputField.test.tsx @@ -29,7 +29,7 @@ describe("InputField", () => { ); // Click the edit icon to render the inputs - fireEvent.click(screen.getByTestId("edit-icon")); + fireEvent.click(screen.getByTestId("view-form")); // Check if the form is rendered const formElement = container.querySelector("form"); diff --git a/src/__tests__/pages/network/[id].test.tsx b/src/__tests__/pages/network/[id].test.tsx index 6b595f99..4017a5a4 100644 --- a/src/__tests__/pages/network/[id].test.tsx +++ b/src/__tests__/pages/network/[id].test.tsx @@ -5,7 +5,8 @@ import NetworkById from "~/pages/network/[id]"; import userEvent from "@testing-library/user-event"; import { useRouter } from "next/router"; import { api } from "../../../utils/api"; - +import { NextIntlProvider } from "next-intl"; +import enTranslation from "~/locales/en/common.json"; // function createTestContext(network?: Network) { // return { // db: prisma, @@ -27,8 +28,29 @@ jest.mock("../../../utils/api", () => ({ { 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", + }, + ], }, members: [], + zombieMembers: [], }, isLoading: false, refetch: jest.fn(), @@ -98,8 +120,11 @@ describe("NetworkById component", () => { }); api.network.getNetworkById.useQuery = useQueryMock; - render(); - + render( + + + + ); // expect(screen.getByText(/loading/i)).toBeInTheDocument(); expect(screen.getByRole("progressbar")).toBeInTheDocument(); }); @@ -123,7 +148,11 @@ describe("NetworkById component", () => { }); api.network.getNetworkById.useQuery = useQueryMock; - render(); + render( + + + + ); // await waitForElementToBeRemoved(() => screen.queryByText(/loading/i)); expect(screen.getByText(/Network ID:/i)).toBeInTheDocument(); @@ -133,25 +162,41 @@ describe("NetworkById component", () => { }); test("renders Network Settings divider", () => { - render(); + render( + + + + ); expect(screen.getByText(/Network Settings/i)).toBeInTheDocument(); }); test("renders Network Members divider", () => { - render(); + render( + + + + ); const matches = screen.getAllByText(/Network Members/i); expect(matches.length).toBe(2); }); test("renders Network Start, Network End, and Network Cidr labels", () => { - render(); + render( + + + + ); expect(screen.getByText(/Network Start:/i)).toBeInTheDocument(); expect(screen.getByText(/Network End:/i)).toBeInTheDocument(); expect(screen.getByText(/Network Cidr:/i)).toBeInTheDocument(); }); test("renders warning message", () => { - render(); + render( + + + + ); expect( screen.getByText( /Join this network ID and the device will automatically be displayed/ @@ -161,7 +206,11 @@ describe("NetworkById component", () => { test("edit network name", async () => { // screen.debug(); - render(); + render( + + + + ); const editIcon = screen.getByTestId("changeNetworkName"); await userEvent.click(editIcon); diff --git a/src/__tests__/pages/network/index.test.tsx b/src/__tests__/pages/network/index.test.tsx index 2f24737e..f5bb77e9 100644 --- a/src/__tests__/pages/network/index.test.tsx +++ b/src/__tests__/pages/network/index.test.tsx @@ -4,6 +4,8 @@ import { useRouter } from "next/router"; import userEvent from "@testing-library/user-event"; import { api } from "../../../utils/api"; import Networks from "~/pages/network"; +import { NextIntlProvider } from "next-intl"; +import enTranslation from "~/locales/en/common.json"; jest.mock("../../../utils/api", () => ({ api: { @@ -39,8 +41,12 @@ describe("Networks page", () => { isLoading: true, refetch: jest.fn(), }); - render(); - expect(screen.getByText("loading")).toBeInTheDocument(); + render( + + + + ); + expect(screen.getByText("Loading")).toBeInTheDocument(); }); it("displays networks and handles add network button", async () => { @@ -65,7 +71,11 @@ describe("Networks page", () => { // }, // }); - render(); + render( + + + + ); expect(screen.getByText("Network 1")).toBeInTheDocument(); expect(screen.getByText("Network 2")).toBeInTheDocument(); diff --git a/src/components/elements/inputField.tsx b/src/components/elements/inputField.tsx index 697e38af..d2c3c89d 100644 --- a/src/components/elements/inputField.tsx +++ b/src/components/elements/inputField.tsx @@ -110,7 +110,11 @@ const InputField = ({
-
diff --git a/src/components/modules/changeLocale.tsx b/src/components/modules/changeLocale.tsx deleted file mode 100644 index b065f09b..00000000 --- a/src/components/modules/changeLocale.tsx +++ /dev/null @@ -1,10 +0,0 @@ -import React from "react"; -import i18n from "./i18n"; -const ChangeLocale = () => { - const changeLanguage = (lng) => { - i18n.changeLanguage(lng); - }; - return
; -}; - -export default ChangeLocale; diff --git a/src/components/modules/networkMulticast.tsx b/src/components/modules/networkMulticast.tsx index f1e0c867..b745ee88 100644 --- a/src/components/modules/networkMulticast.tsx +++ b/src/components/modules/networkMulticast.tsx @@ -103,7 +103,7 @@ export const NetworkMulticast = () => {
{ ) => updateNetwork( diff --git a/src/i18n.ts b/src/i18n.ts deleted file mode 100644 index e3558c3e..00000000 --- a/src/i18n.ts +++ /dev/null @@ -1,66 +0,0 @@ -import i18n from "i18next"; -import { initReactI18next } from "react-i18next"; -import LanuageDetector from "i18next-browser-languagedetector"; -import global_en from "~/locales/en/translation.json"; -import global_zh from "~/locales/zh/translation.json"; -import global_no from "~/locales/no/translation.json"; -import global_es from "~/locales/es/translation.json"; - -export const languages = [ - { - code: "en", - name: "English", - country_code: "gb", - }, - { - code: "zh", - name: "中文", - country_code: "cn", - }, - { - code: "no", - name: "Norsk", - country_code: "no", - }, - { - code: "es", - name: "Español", - country_code: "es", - }, -]; -export const initI18n = async () => { - await i18n - .use(initReactI18next) - .use(LanuageDetector) - .init({ - resources: { - en: { - translation: global_en, - }, - zh: { - translation: global_zh, - }, - no: { - translation: global_no, - }, - es: { - translation: global_es, - }, - }, - fallbackLng: "en", - - interpolation: { - escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape - }, - debug: false, - }); - - // await i18n.changeLanguage("en"); -}; - -export const changeLanguage = async (lng: string) => { - await i18n.changeLanguage(lng); -}; - -// eslint-disable-next-line no-console -initI18n().catch((error) => console.error(error)); diff --git a/src/pages/login/forgotpassword/index.tsx b/src/pages/login/forgotpassword/index.tsx index 275b2e9f..193edd1d 100644 --- a/src/pages/login/forgotpassword/index.tsx +++ b/src/pages/login/forgotpassword/index.tsx @@ -6,7 +6,7 @@ import { type ErrorData, type ZodErrorFieldErrors, } from "~/types/errorHandling"; -import { type GetStaticPropsContext } from "next"; +import { type GetServerSidePropsContext } from "next"; const ForgotPassword = () => { const router = useRouter(); @@ -109,14 +109,14 @@ const ForgotPassword = () => { ); }; -export async function getStaticProps(context: GetStaticPropsContext) { +export async function getServerSideProps(context: GetServerSidePropsContext) { return { props: { // You can get the messages from anywhere you like. The recommended // pattern is to put them in JSON files separated by locale and read // the desired one based on the `locale` received from Next.js. // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access - messages: (await import(`../../locales/${context.locale}/common.json`)) + messages: (await import(`~/locales/${context.locale}/common.json`)) .default, }, }; diff --git a/src/pages/user-settings/account/index.tsx b/src/pages/user-settings/account/index.tsx index 16de3c71..8034d79d 100644 --- a/src/pages/user-settings/account/index.tsx +++ b/src/pages/user-settings/account/index.tsx @@ -136,12 +136,10 @@ const Account = () => {