Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WD-17716 - chore(tests): add tests for root app #506

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions ui/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
import { createRoot } from "react-dom/client";
import { BrowserRouter as Router } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import App from "./App";
import "./sass/styles.scss";
import { NotificationProvider } from "@canonical/react-components";
import { basePath } from "util/basePaths";
import { removeTrailingSlash } from "util/removeTrailingSlash";
import { init } from "root";

const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
retry: false,
// Cache queries for 30 seconds by default.
staleTime: 30000,
},
},
});

const rootElement = document.getElementById("app");
if (!rootElement) throw new Error("Failed to find the root element");
const root = createRoot(rootElement);
root.render(
<Router basename={removeTrailingSlash(basePath)}>
<QueryClientProvider client={queryClient}>
<NotificationProvider>
<App />
</NotificationProvider>
</QueryClientProvider>
</Router>,
);
init();
53 changes: 53 additions & 0 deletions ui/src/root.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { waitFor } from "@testing-library/dom";
import MockAdapter from "axios-mock-adapter";

import { authURLs } from "api/auth";
import { changeURL } from "test/utils";

import { init, Label } from "./root";
import { axiosInstance } from "./api/axios";
import { act } from "@testing-library/react";

const mock = new MockAdapter(axiosInstance);

const cleanRootNode = () =>
document.querySelectorAll("#app").forEach((root) => {
document.body.removeChild(root);
});

const createRootNode = () => {
const root = document.createElement("div");
root.setAttribute("id", "app");
document.body.appendChild(root);
};

beforeEach(() => {
mock.reset();
mock.onGet(authURLs.me).reply(200, {
data: {
email: "email",
name: "name",
nonce: "nonce",
sid: "sid",
sub: "sub",
},
});
createRootNode();
changeURL("/ui");
});

afterEach(() => {
cleanRootNode();
});

test("handles no root node", () => {
cleanRootNode();
expect(init).toThrowError(Label.ERROR);
});

test("initialises the index", async () => {
act(() => init());
await waitFor(() =>
expect(document.querySelector("#app-layout")).toBeInTheDocument(),
);
});
38 changes: 38 additions & 0 deletions ui/src/root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createRoot } from "react-dom/client";
import { BrowserRouter as Router } from "react-router-dom";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import App from "./App";
import "./sass/styles.scss";
import { NotificationProvider } from "@canonical/react-components";
import { basePath } from "util/basePaths";
import { removeTrailingSlash } from "util/removeTrailingSlash";

export enum Label {
ERROR = "Failed to find the root element",
}

export const init = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
retry: false,
// Cache queries for 30 seconds by default.
staleTime: 30000,
},
},
});

const rootElement = document.getElementById("app");
if (!rootElement) throw new Error(Label.ERROR);
const root = createRoot(rootElement);
root.render(
<Router basename={removeTrailingSlash(basePath)}>
<QueryClientProvider client={queryClient}>
<NotificationProvider>
<App />
</NotificationProvider>
</QueryClientProvider>
</Router>,
);
};
Loading