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(<MockAppWithProviders Component={FirstComponent}/>)
            const appNameElement = screen.getByTestId('app-name')
            expect(appNameElement).toHaveTextContent('سامانه CRM')
        });
    });
    describe('behavior', () => {
        it('Should see login button when is not auth', async () => {
            render(<MockAppWithProviders Component={FirstComponent}/>)
            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(<MockAppWithProviders Component={FirstComponent}/>)
            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(<MockAppWithProviders Component={FirstComponent}/>)
            const buttonLoginOrDashboard = await screen.findByTestId('button-login-or-dashboard')
            expect(buttonLoginOrDashboard).toHaveTextContent('ورود کارشناس')
            expect(buttonLoginOrDashboard).not.toHaveTextContent('داشبورد')
        });

    });
});