From 899666da501dfd22267e96be8879dd271b3e4980 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Wed, 10 Jul 2024 04:17:30 +0100 Subject: Unit tests for Navigation component --- src/tests/pages/FormPage/Navigation.test.tsx | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/tests/pages/FormPage/Navigation.test.tsx (limited to 'src/tests') diff --git a/src/tests/pages/FormPage/Navigation.test.tsx b/src/tests/pages/FormPage/Navigation.test.tsx new file mode 100644 index 0000000..d4556ab --- /dev/null +++ b/src/tests/pages/FormPage/Navigation.test.tsx @@ -0,0 +1,48 @@ +/** @jsx jsx */ +import Cookies from "universal-cookie"; +import { jsx } from "@emotion/react"; +import { render } from "@testing-library/react"; +import Navigation from "../../../pages/FormPage/Navigation"; +import {MemoryRouter} from "react-router-dom"; +import { OAuthScopes, CookieNames } from "../../../api/auth"; + +test("navigation shows submit when form is open and no auth required", () => { + const { getByRole } = render(, { wrapper: MemoryRouter }); + const submitButton = getByRole("button"); + + expect(submitButton).not.toBeNull(); + expect(submitButton.innerHTML).toBe("Submit"); +}); + +test("submit button does not show submit on a closed form", () => { + const { queryByRole } = render(, { wrapper: MemoryRouter }); + const submitButton = queryByRole("button"); + + expect(submitButton).toBeNull(); +}); + +test("navigation shows oauth button when form is open and auth is required", () => { + const { getByRole } = render(, { wrapper: MemoryRouter }); + const loginButton = getByRole("button"); + + expect(loginButton).not.toBeNull(); + expect(loginButton.textContent).toBe("Login To Submit"); +}); + +test("navigation shows submit button when form is open and auth is present", () => { + new Cookies().set(CookieNames.Scopes, [OAuthScopes.Identify]); + const { getByRole } = render(, { wrapper: MemoryRouter }); + const loginButton = getByRole("button"); + + expect(loginButton).not.toBeNull(); + expect(loginButton.textContent).toBe("Submit"); +}); + +test("navigation shows login button when form is open and auth is present but insufficient scopes", () => { + new Cookies().set(CookieNames.Scopes, [OAuthScopes.Identify]); + const { getByRole } = render(, { wrapper: MemoryRouter }); + const loginButton = getByRole("button"); + + expect(loginButton).not.toBeNull(); + expect(loginButton.textContent).toBe("Login To Submit"); +}); -- cgit v1.2.3