write three test for first page component

This commit is contained in:
2023-09-26 17:33:14 +03:30
parent 7e768a46eb
commit e29083bc5c

View File

@@ -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(<FirstComponent/>, {wrapper: MockAppWithProviders})
const appNameElement = screen.getByTestId('app-name')
expect(appNameElement).toHaveTextContent('سامانه CRM')
});
describe("First Page Component", () => {
describe("Rendering", () => {
it("App Name Text Rendered", () => {
render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
const appNameElement = screen.queryByText(/سامانه CRM/i);
expect(appNameElement).toBeInTheDocument()
})
});
describe('behavior', () => {
it('Should see login button when is not auth', async () => {
render(<FirstComponent/>, {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(<FirstComponent/>, {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(<FirstComponent/>, {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(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
const authenticationButtonLogin = await screen.queryByText(/ورود کارشناس/i)
const authenticationButtonDashboard = await screen.queryByText(/داشبورد/i)
expect(authenticationButtonLogin).toBeInTheDocument()
expect(authenticationButtonDashboard).not.toBeInTheDocument()
})
});
});