From f88542406f37aa4bccbd1e4c20405bf51dec6896 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:26:05 +0330 Subject: [PATCH 1/5] CFE-10 fixed bug NextIntlProvider in _app --- src/pages/_app.jsx | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index 29ec464..e76fbcb 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -10,24 +10,22 @@ import TitlePage from "@/core/components/TitlePage"; const App = ({Component, pageProps}) => { - return ( - <> - - - - - {pageProps.messages ? : ''} - - - - - - - - - - - ); + return (<> + + + + + {pageProps.messages ? : ''} + + + + + + + + + + ) }; export default App; From 9a60b215dfdd1485e7b67d32de9c08039cd4b285 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:26:55 +0330 Subject: [PATCH 2/5] CFE-10 create mock _app with providers --- mocks/AppWithProvider.jsx | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 mocks/AppWithProvider.jsx diff --git a/mocks/AppWithProvider.jsx b/mocks/AppWithProvider.jsx new file mode 100644 index 0000000..46096f2 --- /dev/null +++ b/mocks/AppWithProvider.jsx @@ -0,0 +1,13 @@ +import App from "@/pages/_app"; +import fa from "&/locales/fa/app.json" + +const translations = {fa}; +const MockAppWithProviders = ({Component, locale, title, isBot}) => { + const pageProps = { + title: title || '', isBot: isBot || true, locale: locale || 'fa', messages: translations[locale || 'fa'] + } + + return () +} + +export default MockAppWithProviders \ No newline at end of file From 9565968aaf04a34eb72ce9bb9392bc2aeaf87c32 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:27:35 +0330 Subject: [PATCH 3/5] CFE-10 update handler msw with new route --- mocks/handler.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mocks/handler.js b/mocks/handler.js index 5eff8ae..49e0f99 100644 --- a/mocks/handler.js +++ b/mocks/handler.js @@ -1 +1,8 @@ -export const handler = [] \ No newline at end of file +import {GET_USER_ROUTE} from "@/core/data/apiRoutes"; +import {rest} from "msw"; + +export const handler = [rest.get(GET_USER_ROUTE, (req, res, ctx) => { + return res(ctx.json({ + id: 10 + })) +})] \ No newline at end of file From 8e736eeb280eaecfa1fe6488ef670cdd7133317d Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:28:27 +0330 Subject: [PATCH 4/5] CFE-10 add localStorage.clear() in beforeEach --- jest.setup.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/jest.setup.js b/jest.setup.js index a847f56..f23b841 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -6,6 +6,9 @@ jest.mock('next/router', () => jest.requireActual('next-router-mock')) beforeAll(() => { server.listen() }) +beforeEach(() => { + localStorage.clear(); +}) afterEach(() => { server.resetHandlers() From 866cfddd2ca1ce8d0385185bd0bddf45dd582754 Mon Sep 17 00:00:00 2001 From: AmirHossein Mahmoodi Date: Mon, 25 Sep 2023 11:29:30 +0330 Subject: [PATCH 5/5] 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 ? ( - - ) : ( - - )} +