From 374c6a4d6bf5c3ff86e225c246cb82298ae38e93 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Sun, 5 Nov 2023 11:50:41 +0330 Subject: [PATCH] LFFE-14 Create test for first page --- example.env.local | 1 + mocks/handler.js | 6 +- mocks/handlers/user.js | 19 ++++++ package.json | 2 +- public/locales/fa/app.json | 1 + src/components/first/__tests__/index.test.js | 61 ++++++++++++++++++++ src/components/first/index.jsx | 51 ++++++++-------- src/pages/_app.jsx | 2 +- src/pages/index.jsx | 1 + 9 files changed, 115 insertions(+), 29 deletions(-) create mode 100644 mocks/handlers/user.js create mode 100644 src/components/first/__tests__/index.test.js diff --git a/example.env.local b/example.env.local index b9a7e4c..7eb8d64 100644 --- a/example.env.local +++ b/example.env.local @@ -7,6 +7,7 @@ NEXT_PUBLIC_PRIMARY_MAIN = "#084070" NEXT_PUBLIC_SECONDARY_MAIN = "#FF4E00" NEXT_PUBLIC_BASE_URL = "https://loan.witel.ir" +NEXT_PUBLIC_POWERED_BY_URL = "https://witel.ir" #["NEXT_PUBLIC_HAS_WIDGET" , "NEXT_PUBLIC_HAS_NOTIFICATION"] NEXT_PUBLIC_HAS_VALUE=["NEXT_PUBLIC_HAS_NOTIFICATION"] diff --git a/mocks/handler.js b/mocks/handler.js index eda0ea5..a4e5e8f 100644 --- a/mocks/handler.js +++ b/mocks/handler.js @@ -1 +1,5 @@ -export const handler = []; +import {userHandler} from "./handlers/user"; + +export const handler = [ + ...userHandler +]; diff --git a/mocks/handlers/user.js b/mocks/handlers/user.js new file mode 100644 index 0000000..10677de --- /dev/null +++ b/mocks/handlers/user.js @@ -0,0 +1,19 @@ +import {rest} from "msw"; +import {GET_USER_ROUTE} from "@/core/data/apiRoutes"; + +export const userHandler = [ + rest.get(GET_USER_ROUTE, (req, res, ctx) => { + return res(ctx.json({ + data: { + id: 10, + full_name: "Witel Company", + position: "Software Engineer", + permissions: [ + "manage_users", + "manage_roles", + "manage_boss" + ], + } + })) + }), +] diff --git a/package.json b/package.json index 479f98d..6459d69 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "eslint-plugin-testing-library": "^6.1.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", - "msw": "^2.0.3", + "msw": "^1.3.1", "next-router-mock": "^0.9.10", "run-script-os": "^1.1.6" } diff --git a/public/locales/fa/app.json b/public/locales/fa/app.json index 2a47ea5..9d3297f 100644 --- a/public/locales/fa/app.json +++ b/public/locales/fa/app.json @@ -2,6 +2,7 @@ "app_name": "سامانه جامع تسهیلات", "app_short_name": "سامانه تسهیلات", "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 new file mode 100644 index 0000000..e6aff67 --- /dev/null +++ b/src/components/first/__tests__/index.test.js @@ -0,0 +1,61 @@ +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 Component From First Page", () => { + describe("Rendering", () => { + it("App Name Text Rendered", () => { + render(); + const appNameElement = screen.queryByText(/سامانه جامع تسهیلات/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(); + 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(401)) + })) + 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 cd5b5a5..2d144f1 100644 --- a/src/components/first/index.jsx +++ b/src/components/first/index.jsx @@ -1,11 +1,10 @@ -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"; import {Button, Stack, Typography} from "@mui/material"; import {useTranslations} from "next-intl"; import SvgDashboard from "@/core/components/svgs/SvgDashboard"; -import process from "next/dist/build/webpack/loaders/resolve-url-loader/lib/postcss"; const FirstComponent = () => { const t = useTranslations(); @@ -18,28 +17,16 @@ const FirstComponent = () => { {t("app_name")} - {isAuth ? ( - - ) : ( - - )} + { color: 'primary.main', fontFamily: 'Arial', fontWeight: 'bold' - }}>v{process.env.NEXT_PUBLIC_API_VERSION} + }} + > + v{process.env.NEXT_PUBLIC_API_VERSION} + + + + + {t("powered_by_witel")} + ); }; -export default FirstComponent; +export default FirstComponent; \ No newline at end of file diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index 342f2a4..d7c2169 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -15,7 +15,7 @@ const App = ({Component, pageProps}) => { <> - + {pageProps.messages ? : ''} diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 2e146c6..996ba03 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -16,6 +16,7 @@ export async function getServerSideProps({req, locale}) { messages: (await import(`&/locales/${locale}/app.json`)).default, title: "first_page", isBot, + locale }, }; }