import {render, screen, waitFor} 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 Component From First Page", () => {
    describe("Rendering", () => {
        it("App Name Text Rendered", () => {
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            const appNameElement = screen.queryByText(/سامانه جامع تسهیلات/i);
            expect(appNameElement).toBeInTheDocument()
        });
        it("App version Text Rendered", () => {
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            const versionControler = screen.queryByText(process.env.NEXT_PUBLIC_API_VERSION, {exact: false});
            expect(versionControler).toBeInTheDocument()
        });
        it("Powered By Rendered With Currect URL", () => {
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            const linkElement = screen.queryByText('توسعه یافته توسط وایتل');
            expect(linkElement).toBeInTheDocument()
            expect(linkElement).toHaveAttribute('href', process.env.NEXT_PUBLIC_POWERED_BY_URL);
        });
    });
    describe("Behavioral", () => {
        it("Show Login Button And Do Not Show Dashboard Button When User Is Not Authenticated", async () => {
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            await waitFor(() => {
                const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i)
                const authenticationButtonDashboard = screen.queryByText(/داشبورد/i)
                expect(authenticationButtonLogin).toBeInTheDocument()
                expect(authenticationButtonDashboard).not.toBeInTheDocument()
            })
        });
        it("Show Dashboard Button And Do Not Show Login Button When User Is Authenticated", async () => {
            localStorage.setItem("_token", 'token');
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            await waitFor(() => {
                const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i)
                const authenticationButtonDashboard = screen.queryByText(/داشبورد/i)
                expect(authenticationButtonLogin).not.toBeInTheDocument()
                expect(authenticationButtonDashboard).toBeInTheDocument()
            })
        });
        it("Show Login Button And Do Not Show Dashboard Button When User Authentication Is Expired", async () => {
            localStorage.setItem("_token", 'token');
            server.use(rest.get(GET_USER_ROUTE, (req, res, ctx) => {
                return res(ctx.status(401))
            }))
            render(<MockAppWithProviders><FirstComponent/></MockAppWithProviders>);
            await waitFor(() => {
                const authenticationButtonLogin = screen.queryByText(/ورود کارشناس/i)
                const authenticationButtonDashboard = screen.queryByText(/داشبورد/i)
                expect(authenticationButtonLogin).toBeInTheDocument()
                expect(authenticationButtonDashboard).not.toBeInTheDocument()
            })
        });
    });
});