-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for Catalog GroupBy Dropdown
- Loading branch information
1 parent
2acbbbc
commit 01fdeca
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
src/components/GroupByDropdown/__tests__/GroupByDropdown.unit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { render, screen, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { rest } from "msw"; | ||
import { setupServer } from "msw/node"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import GroupByDropdown from "./../index"; | ||
|
||
const server = setupServer( | ||
rest.get("/api/db/configs", (req, res, ctx) => { | ||
return res( | ||
ctx.json([ | ||
{ | ||
id: "1", | ||
name: "Config 1", | ||
tags: { | ||
tag1: "value1", | ||
tag2: "value2" | ||
} | ||
}, | ||
{ | ||
id: "2", | ||
name: "Config 2", | ||
tags: { | ||
tag3: "value3", | ||
tag4: "value4" | ||
} | ||
} | ||
]) | ||
); | ||
}) | ||
); | ||
|
||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
describe("GroupByDropdown", () => { | ||
it("renders the dropdown with the provided value", async () => { | ||
render( | ||
<MemoryRouter> | ||
<QueryClientProvider client={queryClient}> | ||
<GroupByDropdown value="name" /> | ||
</QueryClientProvider> | ||
</MemoryRouter> | ||
); | ||
await waitFor(() => { | ||
expect(screen.getByText("Group By:")).toBeInTheDocument(); | ||
}); | ||
expect(screen.getByText("Name")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders the dropdown with the tag options", async () => { | ||
render( | ||
<MemoryRouter> | ||
<QueryClientProvider client={queryClient}> | ||
<GroupByDropdown /> | ||
</QueryClientProvider> | ||
</MemoryRouter> | ||
); | ||
await waitFor(() => { | ||
expect(screen.getByText("Group By:")).toBeInTheDocument(); | ||
}); | ||
|
||
userEvent.click(screen.getByText("Group By:")); | ||
|
||
await expect(screen.findByText(/Tag1/i)).resolves.toBeInTheDocument(); | ||
await expect(screen.findByText(/Tag2/i)).resolves.toBeInTheDocument(); | ||
await expect(screen.findByText(/Tag3/i)).resolves.toBeInTheDocument(); | ||
await expect(screen.findByText(/Tag4/i)).resolves.toBeInTheDocument(); | ||
}); | ||
|
||
it("calls the onChange function when a new option is selected", async () => { | ||
const onChange = jest.fn(); | ||
render( | ||
<MemoryRouter> | ||
<QueryClientProvider client={queryClient}> | ||
<GroupByDropdown onChange={onChange} /> | ||
</QueryClientProvider> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("Group By:")).toBeInTheDocument(); | ||
}); | ||
|
||
userEvent.click(screen.getByText("Group By:")); | ||
|
||
await expect(screen.findByText(/Tag1/i)).resolves.toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByText(/Tag1/i)); | ||
|
||
await waitFor(() => { | ||
expect(onChange).toHaveBeenCalledWith("tag1"); | ||
}); | ||
}); | ||
}); |