From 866cfddd2ca1ce8d0385185bd0bddf45dd582754 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:29:30 +0330 Subject: [PATCH] CFE-10 testing first page --- src/components/first/__tests__/index.test.js | 44 ++++++++++++++++++++ src/components/first/index.jsx | 34 +++++---------- 2 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 src/components/first/__tests__/index.test.js diff --git a/src/components/first/__tests__/index.test.js b/src/components/first/__tests__/index.test.js new file mode 100644 index 0000000..987b7c5 --- /dev/null +++ b/src/components/first/__tests__/index.test.js @@ -0,0 +1,44 @@ +import {render, screen} from "@testing-library/react"; +import MockAppWithProviders from "../../../../mocks/AppWithProvider"; +import FirstComponent from "@/components/first"; +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() + const appNameElement = screen.getByTestId('app-name') + expect(appNameElement).toHaveTextContent('سامانه CRM') + }); + }); + describe('behavior', () => { + it('Should see login button when is not auth', async () => { + render() + 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() + 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() + const buttonLoginOrDashboard = await screen.findByTestId('button-login-or-dashboard') + expect(buttonLoginOrDashboard).toHaveTextContent('ورود کارشناس') + expect(buttonLoginOrDashboard).not.toHaveTextContent('داشبورد') + }); + + }); +}); \ No newline at end of file diff --git a/src/components/first/index.jsx b/src/components/first/index.jsx index fade07c..786f25f 100644 --- a/src/components/first/index.jsx +++ b/src/components/first/index.jsx @@ -14,31 +14,19 @@ const FirstComponent = () => { - + {t("app_name")} - {isAuth ? ( - - ) : ( - - )} +