diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/App.test.tsx | 5 | ||||
-rw-r--r-- | src/tests/components/AuthorizationSplash.test.tsx | 62 | ||||
-rw-r--r-- | src/tests/utils.tsx | 36 |
3 files changed, 101 insertions, 2 deletions
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(<App/>); + const {container} = renderWithProviders(<App/>); await waitFor(() => { expect(container).toBeInTheDocument(); }); 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(<AuthorizationSplash />); + 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(<AuthorizationSplash />, { + 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(<AuthorizationSplash />, { + 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"); + } +}); 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<RenderOptions, "queries"> { + preloadedState?: Partial<RootState> + 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) => ( + <Provider store={store}>{children}</Provider> + ); + + // Return an object with the store and all of RTL"s query functions + return { + store, + ...render(ui, { wrapper: Wrapper, ...renderOptions }) + }; +} |