|
1 | 1 | import { BrowserRouter } from "react-router-dom"; |
| 2 | +import { QueryClient, QueryClientProvider } from "react-query"; |
2 | 3 | import { render, screen } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
3 | 5 | import "@testing-library/jest-dom"; |
4 | 6 |
|
5 | | -import RegisterNameDisputeSuccess from "../RegisterNameDisputeSuccess"; |
| 7 | +import RegisterNameDispute from "../RegisterNameDispute"; |
| 8 | + |
| 9 | +jest.mock("react-router-dom", () => { |
| 10 | + return { |
| 11 | + ...jest.requireActual("react-router-dom"), |
| 12 | + useSearchParams: () => [ |
| 13 | + new URLSearchParams({ "snap-name": "test-snap-id", store: "ubuntu" }), |
| 14 | + ], |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +jest.mock("react-query", () => ({ |
| 19 | + ...jest.requireActual("react-query"), |
| 20 | + useQuery: jest.fn().mockReturnValue({ |
| 21 | + data: [ |
| 22 | + { |
| 23 | + id: "ubuntu", |
| 24 | + name: "Global", |
| 25 | + roles: ["view", "access"], |
| 26 | + }, |
| 27 | + ], |
| 28 | + isLoading: false, |
| 29 | + }), |
| 30 | +})); |
| 31 | + |
| 32 | +const queryClient = new QueryClient(); |
6 | 33 |
|
7 | 34 | function renderComponent() { |
8 | 35 | return render( |
9 | 36 | <BrowserRouter> |
10 | | - <RegisterNameDisputeSuccess snapName="test-snap" /> |
| 37 | + <QueryClientProvider client={queryClient}> |
| 38 | + <RegisterNameDispute /> |
| 39 | + </QueryClientProvider> |
11 | 40 | </BrowserRouter>, |
12 | 41 | ); |
13 | 42 | } |
14 | 43 |
|
15 | | -describe("RegisterNameDisputeSuccess", () => { |
| 44 | +describe("RegisterNameDispute", () => { |
16 | 45 | test("the snap name is shown", () => { |
17 | 46 | renderComponent(); |
| 47 | + const el = screen.getByText("Claim the name", { exact: false }); |
| 48 | + expect(el.textContent).toEqual("Claim the name test-snap-id"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("store field shows correct store", () => { |
| 52 | + renderComponent(); |
| 53 | + expect(screen.getByLabelText("Store")).toHaveValue("Global"); |
| 54 | + }); |
| 55 | + |
| 56 | + test("snap name field show correct snap", () => { |
| 57 | + renderComponent(); |
| 58 | + expect(screen.getByLabelText("Snap name")).toHaveValue("test-snap-id"); |
| 59 | + }); |
| 60 | + |
| 61 | + test("'Register a new name' link is on page", () => { |
| 62 | + renderComponent(); |
| 63 | + expect( |
| 64 | + screen.getByRole("link", { name: "Register a new name" }), |
| 65 | + ).toHaveAttribute("href", "/register-snap"); |
| 66 | + }); |
| 67 | + |
| 68 | + test("'Submit name claim' button is disabled", () => { |
| 69 | + renderComponent(); |
| 70 | + expect( |
| 71 | + screen.getByRole("button", { name: "Submit name claim" }), |
| 72 | + ).toHaveAttribute("aria-disabled", "true"); |
| 73 | + }); |
| 74 | + |
| 75 | + test("'adding comment enables 'Submit name claim' button", async () => { |
| 76 | + const user = userEvent.setup(); |
| 77 | + renderComponent(); |
| 78 | + await user.type(screen.getByLabelText("Comment"), "Test comment"); |
18 | 79 | expect( |
19 | | - screen.getByRole("heading", { |
20 | | - level: 1, |
21 | | - name: /Thank you for requesting the name test-snap/, |
22 | | - }), |
23 | | - ); |
| 80 | + screen.getByRole("button", { name: "Submit name claim" }), |
| 81 | + ).not.toHaveAttribute("aria-disabled"); |
24 | 82 | }); |
25 | 83 | }); |
0 commit comments