From e29083bc5cfcf52b6a3d24471316192b20423962 Mon Sep 17 00:00:00 2001 From: Mohammad Jalali Date: Tue, 26 Sep 2023 17:33:14 +0330 Subject: [PATCH 1/2] write three test for first page component --- src/components/first/__tests__/index.test.js | 52 ++++++-------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/src/components/first/__tests__/index.test.js b/src/components/first/__tests__/index.test.js index 7e60576..c098d67 100644 --- a/src/components/first/__tests__/index.test.js +++ b/src/components/first/__tests__/index.test.js @@ -1,42 +1,22 @@ -import FirstComponent from "@/components/first"; import {render, screen} from "@testing-library/react"; +import FirstComponent from "@/components/first"; import MockAppWithProviders from "../../../../mocks/AppWithProvider"; -import {server} from "../../../../mocks/server"; -import {rest} from "msw"; -import {GET_USER_ROUTE} from "@/core/data/apiRoutes"; -describe('First page component', () => { - describe('Rendering', () => { - it('Should see app name text', () => { - render(, {wrapper: MockAppWithProviders}) - const appNameElement = screen.getByTestId('app-name') - expect(appNameElement).toHaveTextContent('سامانه CRM') - }); +describe("First Page Component", () => { + describe("Rendering", () => { + it("App Name Text Rendered", () => { + render(); + const appNameElement = screen.queryByText(/سامانه CRM/i); + expect(appNameElement).toBeInTheDocument() + }) }); - describe('behavior', () => { - it('Should see login button when is not auth', async () => { - render(, {wrapper: MockAppWithProviders}) - const buttonLoginOrDashboard = await screen.findByTestId('button-login-or-dashboard') - expect(buttonLoginOrDashboard).toHaveTextContent('ورود کارشناس') - expect(buttonLoginOrDashboard).not.toHaveTextContent('داشبورد') - }); - it('Should see dashboard button when is auth', async () => { - localStorage.setItem("_token", 'token'); - render(, {wrapper: MockAppWithProviders}) - const buttonLoginOrDashboard = await screen.findByTestId('button-login-or-dashboard') - expect(buttonLoginOrDashboard).not.toHaveTextContent('ورود کارشناس') - expect(buttonLoginOrDashboard).toHaveTextContent('داشبورد') - }); - it('Should see login button when token expire', async () => { - localStorage.setItem("_token", 'token'); - server.use([rest.get(GET_USER_ROUTE, (req, res, ctx) => { - return res(ctx.status(403)) - })]) - render(, {wrapper: MockAppWithProviders}) - const buttonLoginOrDashboard = await screen.findByTestId('button-login-or-dashboard') - expect(buttonLoginOrDashboard).toHaveTextContent('ورود کارشناس') - expect(buttonLoginOrDashboard).not.toHaveTextContent('داشبورد') - }); - + describe("Behavioral", () => { + it("Show Login Button And Do Not Show Dashboard Button When User Is Not Authenticated", async () => { + render(); + const authenticationButtonLogin = await screen.queryByText(/ورود کارشناس/i) + const authenticationButtonDashboard = await screen.queryByText(/داشبورد/i) + expect(authenticationButtonLogin).toBeInTheDocument() + expect(authenticationButtonDashboard).not.toBeInTheDocument() + }) }); }); \ No newline at end of file From 742741387d850c113b7a07933bf818554c7da542 Mon Sep 17 00:00:00 2001 From: Mohammad Jalali Date: Wed, 27 Sep 2023 11:51:08 +0330 Subject: [PATCH 2/2] CFE-17 write 6 test for first component from first page and add Witel company url --- example.env.local | 1 + public/locales/fa/app.json | 1 + src/components/first/__tests__/index.test.js | 55 +++++++++++++++++--- src/components/first/index.jsx | 18 +++++-- src/components/login-expert/index.jsx | 2 +- 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/example.env.local b/example.env.local index 078bd0d..0be16f0 100644 --- a/example.env.local +++ b/example.env.local @@ -7,5 +7,6 @@ NEXT_PUBLIC_PRIMARY_MAIN = "#1b4332" NEXT_PUBLIC_SECONDARY_MAIN = "#800e13" NEXT_PUBLIC_BASE_URL = "https://crm.witel.ir" +NEXT_PUBLIC_POWERED_BY_URL = "https://witel.ir" NODE_ENV = "development" \ No newline at end of file diff --git a/public/locales/fa/app.json b/public/locales/fa/app.json index dd72e71..e813849 100644 --- a/public/locales/fa/app.json +++ b/public/locales/fa/app.json @@ -2,6 +2,7 @@ "app_name": "سامانه CRM", "app_short_name": "سامانه CRM", "dashboard": "داشبورد", + "powered_by_witel": "توسعه یافته توسط وایتل", "first_page": "خوش آمدید", "login": "ورود", "login_expert": "ورود کارشناس", diff --git a/src/components/first/__tests__/index.test.js b/src/components/first/__tests__/index.test.js index c098d67..6710f8c 100644 --- a/src/components/first/__tests__/index.test.js +++ b/src/components/first/__tests__/index.test.js @@ -1,22 +1,61 @@ -import {render, screen} from "@testing-library/react"; +import {render, screen, waitFor} from "@testing-library/react"; import FirstComponent from "@/components/first"; import MockAppWithProviders from "../../../../mocks/AppWithProvider"; +import {server} from "../../../../mocks/server"; +import {rest} from "msw"; +import {GET_USER_ROUTE} from "@/core/data/apiRoutes"; -describe("First Page Component", () => { +describe("First Component From First Page", () => { describe("Rendering", () => { it("App Name Text Rendered", () => { render(); const appNameElement = screen.queryByText(/سامانه CRM/i); expect(appNameElement).toBeInTheDocument() - }) + }); + it("App version Text Rendered", () => { + render(); + const versionControler = screen.queryByText(process.env.NEXT_PUBLIC_API_VERSION, {exact: false}); + expect(versionControler).toBeInTheDocument() + }); + it("Powered By Rendered With Currect URL", () => { + render(); + const linkElement = screen.queryByText('توسعه یافته توسط وایتل'); + expect(linkElement).toBeInTheDocument() + expect(linkElement).toHaveAttribute('href', process.env.NEXT_PUBLIC_POWERED_BY_URL); + }); }); describe("Behavioral", () => { it("Show Login Button And Do Not Show Dashboard Button When User Is Not Authenticated", async () => { render(); - const authenticationButtonLogin = await screen.queryByText(/ورود کارشناس/i) - const authenticationButtonDashboard = await screen.queryByText(/داشبورد/i) - expect(authenticationButtonLogin).toBeInTheDocument() - expect(authenticationButtonDashboard).not.toBeInTheDocument() - }) + await waitFor(() => { + const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i) + const authenticationButtonDashboard = screen.queryByText(/داشبورد/i) + expect(authenticationButtonLogin).toBeInTheDocument() + expect(authenticationButtonDashboard).not.toBeInTheDocument() + }) + }); + it("Show Dashboard Button And Do Not Show Login Button When User Is Authenticated", async () => { + localStorage.setItem("_token", 'token'); + render(); + await waitFor(() => { + const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i) + const authenticationButtonDashboard = screen.queryByText(/داشبورد/i) + expect(authenticationButtonLogin).not.toBeInTheDocument() + expect(authenticationButtonDashboard).toBeInTheDocument() + }) + }); + it("Show Login Button And Do Not Show Dashboard Button When User Authentication Is Expired", async () => { + localStorage.setItem("_token", 'token'); + server.use([rest.get(GET_USER_ROUTE, (req, res, ctx) => { + return res(ctx.status(403)) + })]) + render(); + await waitFor(() => { + const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i) + const authenticationButtonDashboard = screen.queryByText(/داشبورد/i) + expect(authenticationButtonLogin).toBeInTheDocument() + expect(authenticationButtonDashboard).not.toBeInTheDocument() + }) + }); }); }); \ No newline at end of file diff --git a/src/components/first/index.jsx b/src/components/first/index.jsx index 786f25f..1d7166d 100644 --- a/src/components/first/index.jsx +++ b/src/components/first/index.jsx @@ -1,4 +1,4 @@ -import {NextLinkComposed} from "@/core/components/LinkRouting"; +import LinkRouting, {NextLinkComposed} from "@/core/components/LinkRouting"; import CenterLayout from "@/layouts/CenterLayout"; import FullPageLayout from "@/layouts/FullPageLayout"; import useUser from "@/lib/app/hooks/useUser"; @@ -14,7 +14,7 @@ const FirstComponent = () => { - + {t("app_name")}