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() 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 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 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 ? ( - - ) : ( - - )} + { - return ( - <> - - - - - {pageProps.messages ? : ''} - - - - - - - - - - - ); + return (<> + + + + + {pageProps.messages ? : ''} + + + + + + + + + + ) }; export default App;