From b1239935da1dff2902d5f6af9a9622f4e887479f Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 9 Jul 2024 21:55:24 +0100 Subject: Add new testing utility for rendering with Redux stores --- src/tests/utils.tsx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/tests/utils.tsx (limited to 'src/tests') diff --git a/src/tests/utils.tsx b/src/tests/utils.tsx new file mode 100644 index 0000000..011291a --- /dev/null +++ b/src/tests/utils.tsx @@ -0,0 +1,36 @@ +/** @jsx jsx */ +import { jsx } from "@emotion/react"; +import { PropsWithChildren } from "react"; +import { render } from "@testing-library/react"; +import type { RenderOptions } from "@testing-library/react"; +import { Provider } from "react-redux"; + +import type { AppStore, RootState } from "../store"; +import { setupStore } from "../store"; + +interface ExtendedRenderOptions extends Omit { + preloadedState?: Partial + store?: AppStore +} + +export function renderWithProviders( + ui: React.ReactElement, + extendedRenderOptions: ExtendedRenderOptions = {} +) { + const { + preloadedState = {}, + // Automatically create a store instance if no store was passed in + store = setupStore(preloadedState), + ...renderOptions + } = extendedRenderOptions; + + const Wrapper = ({ children }: PropsWithChildren) => ( + {children} + ); + + // Return an object with the store and all of RTL"s query functions + return { + store, + ...render(ui, { wrapper: Wrapper, ...renderOptions }) + }; +} -- cgit v1.2.3 From 696b200ff8f0ea4203f0cbd0c4e68aa0fd3de91b Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 9 Jul 2024 21:55:34 +0100 Subject: Update App test to render with Redux store --- src/tests/App.test.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/tests') diff --git a/src/tests/App.test.tsx b/src/tests/App.test.tsx index 9ee1ec7..aec6be4 100644 --- a/src/tests/App.test.tsx +++ b/src/tests/App.test.tsx @@ -1,11 +1,12 @@ import React from "react"; -import {act, render, waitFor} from "@testing-library/react"; +import {act, waitFor} from "@testing-library/react"; +import { renderWithProviders } from "./utils"; import App from "../App"; test("renders app to body", async () => { await act(async () => { - const {container} = render(); + const {container} = renderWithProviders(); await waitFor(() => { expect(container).toBeInTheDocument(); }); -- cgit v1.2.3 From e586c951c14d1bac4cabc1f582aca4d12045f179 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 9 Jul 2024 21:55:42 +0100 Subject: Add new test suite for testing authorization splash --- src/tests/components/AuthorizationSplash.test.tsx | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/tests/components/AuthorizationSplash.test.tsx (limited to 'src/tests') diff --git a/src/tests/components/AuthorizationSplash.test.tsx b/src/tests/components/AuthorizationSplash.test.tsx new file mode 100644 index 0000000..7f44ba4 --- /dev/null +++ b/src/tests/components/AuthorizationSplash.test.tsx @@ -0,0 +1,62 @@ +/** @jsx jsx */ +import { jsx } from "@emotion/react"; +import { renderWithProviders } from "../utils"; +import AuthorizationSplash from "../../components/AuthorizationSplash"; +import { finishAuthorizing } from "../../slices/authorization"; +import { act } from "@testing-library/react"; + +test("authorization splash is hidden when not authorizing", () => { + const { container } = renderWithProviders(); + const splash = container.firstElementChild; + + expect(splash).not.toBe(null); + + if (splash) { + const style = window.getComputedStyle(splash); + expect(style.opacity).toBe("0"); + } +}); + +test("authorization splash is visible when authorizing state is set", () => { + const { container } = renderWithProviders(, { + preloadedState: { + authorization: { + authorizing: true + } + } + }); + const splash = container.firstElementChild; + + expect(splash).not.toBe(null); + + if (splash) { + const style = window.getComputedStyle(splash); + expect(style.opacity).toBe("1"); + } +}); + +test("test state transitions when authorization completes", () => { + const { store, container } = renderWithProviders(, { + preloadedState: { + authorization: { + authorizing: true + } + } + }); + + const splash = container.firstElementChild; + + expect(splash).not.toBe(null); + + if (splash) { + let style = window.getComputedStyle(splash); + expect(style.opacity).toBe("1"); + + act(() => { + store.dispatch(finishAuthorizing()); + }); + + style = window.getComputedStyle(splash); + expect(style.opacity).toBe("0"); + } +}); -- cgit v1.2.3