Skip to content

Commit

Permalink
feat: Add useQuery with a mocked response to home page.
Browse files Browse the repository at this point in the history
- the example confirms that react-query is working.
- it enables to write a test to confirm that all
basic functionality can be tested with the current setup.
  • Loading branch information
programmiri committed Nov 2, 2022
1 parent d324b25 commit be7b7a3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
7 changes: 6 additions & 1 deletion coral/src/app/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { RouterProvider } from "react-router-dom";
import router from "src/app/router";

import "@aivenio/design-system/dist/styles.css";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<RouterProvider router={router} />
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
</React.StrictMode>
);
49 changes: 46 additions & 3 deletions coral/src/app/pages/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
import HomePage from "src/app/pages";
import { render, cleanup, screen } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactElement } from "react";

export function renderWithClient(client: QueryClient, ui: ReactElement) {
const { rerender, ...result } = render(
<QueryClientProvider client={client}>{ui}</QueryClientProvider>
);
return {
...result,
rerender: (rerenderUi: ReactElement) =>
rerender(
<QueryClientProvider client={client}>{rerenderUi}</QueryClientProvider>
),
};
}

const loadingText = "data is loading";
const textFromApi = "hello";

describe("HomePage", () => {
beforeEach(() => {
render(<HomePage />);
const queryClient = new QueryClient();
renderWithClient(queryClient, <HomePage />);
});

afterEach(() => {
cleanup();
});
it("shoud render dummy content", () => {
expect(screen.getByText("Index")).toBeVisible();

it("renders dummy content", () => {
const heading = screen.getByRole("heading", { name: "Index" });

expect(heading).toBeVisible();
});

it("shows a loading information as long as data is not returned from 'API'", () => {
const loadingInfo = screen.getByText(loadingText);

expect(loadingInfo).toBeVisible();
});

it("shows the data returned from the 'API'", async () => {
const dataFromApi = await screen.findByText(textFromApi);

expect(dataFromApi).toBeVisible();
});

it("removes loading information when data is returned from the 'API'", async () => {
const loadingInfo = screen.queryByText(loadingText);
expect(loadingInfo).toBeVisible();

await screen.findByText(textFromApi);

expect(loadingInfo).not.toBeInTheDocument();
});
});
16 changes: 15 additions & 1 deletion coral/src/app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
const HomePage = () => <h1>Index</h1>;
import { useQuery } from "@tanstack/react-query";

const HomePage = () => {
const { isLoading, data } = useQuery(["testData"], () => {
return Promise.resolve({ data: "hello" });
});

return (
<>
<h1>Index</h1>
{isLoading && <p>data is loading</p>}
{!isLoading && data && <p>{data.data}</p>}
</>
);
};

export default HomePage;

0 comments on commit be7b7a3

Please sign in to comment.