|
| 1 | +import { BrowserRouter } from "react-router-dom"; |
| 2 | +import { RecoilRoot } from "recoil"; |
| 3 | +import { QueryClient, QueryClientProvider } from "react-query"; |
| 4 | +import { render, screen } from "@testing-library/react"; |
| 5 | +import userEvent from "@testing-library/user-event"; |
| 6 | +import "@testing-library/jest-dom"; |
| 7 | + |
| 8 | +import PublisherSettingsForm from "../PublisherSettingsForm"; |
| 9 | + |
| 10 | +const queryClient = new QueryClient(); |
| 11 | + |
| 12 | +jest.mock("react-router-dom", () => ({ |
| 13 | + ...jest.requireActual("react-router-dom"), |
| 14 | + useParams: () => ({ |
| 15 | + snapId: "test-snap-id", |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +const mockSettings = { |
| 20 | + blacklist_countries: [], |
| 21 | + countries: [{ key: "GB", name: "United Kingdom" }], |
| 22 | + private: false, |
| 23 | + snap_id: "test-snap-id", |
| 24 | + snap_name: "test-snap", |
| 25 | + span_title: "test-snap", |
| 26 | + status: "published", |
| 27 | + store: "Global", |
| 28 | + unlisted: false, |
| 29 | + update_metadata_on_release: true, |
| 30 | + visibility_locked: false, |
| 31 | + whitelist_countries: ["GB"], |
| 32 | +}; |
| 33 | + |
| 34 | +const updateMetadataOnReleaseNotification = |
| 35 | + "This snap is set to have its metadata updated when a new revision is published in the stable channel. Any changes you make here will be overwritten by the contents of any snap published. If this is not desirable, please disable “Update metadata on release” for this snap."; |
| 36 | + |
| 37 | +function renderComponent() { |
| 38 | + render( |
| 39 | + <BrowserRouter> |
| 40 | + <RecoilRoot> |
| 41 | + <QueryClientProvider client={queryClient}> |
| 42 | + <PublisherSettingsForm settings={mockSettings} /> |
| 43 | + </QueryClientProvider> |
| 44 | + </RecoilRoot> |
| 45 | + </BrowserRouter>, |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +describe("PublisherSettingsForm", () => { |
| 50 | + test("'Revert' and 'Save' buttons disabled by default", () => { |
| 51 | + renderComponent(); |
| 52 | + expect(screen.getByRole("button", { name: "Revert" })).toHaveAttribute( |
| 53 | + "aria-disabled", |
| 54 | + "true", |
| 55 | + ); |
| 56 | + expect(screen.getByRole("button", { name: "Save" })).toHaveAttribute( |
| 57 | + "aria-disabled", |
| 58 | + "true", |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("sets visibility", () => { |
| 63 | + renderComponent(); |
| 64 | + expect(screen.getByLabelText(/Public/)).toBeChecked(); |
| 65 | + }); |
| 66 | + |
| 67 | + test("sets selected territories", () => { |
| 68 | + renderComponent(); |
| 69 | + expect(screen.getByLabelText("Selected territories")).toBeChecked(); |
| 70 | + }); |
| 71 | + |
| 72 | + test("sets 'Update metadata on release'", () => { |
| 73 | + renderComponent(); |
| 74 | + expect(screen.getByLabelText("Update metadata on release:")).toBeChecked(); |
| 75 | + }); |
| 76 | + |
| 77 | + test("links to collaborators in dashboard", () => { |
| 78 | + renderComponent(); |
| 79 | + expect( |
| 80 | + screen.getByRole("link", { |
| 81 | + name: "Manage collaborators in dashboard.snapcraft.io", |
| 82 | + }), |
| 83 | + ).toBeInTheDocument(); |
| 84 | + }); |
| 85 | + |
| 86 | + test("sets store", () => { |
| 87 | + renderComponent(); |
| 88 | + expect(screen.getByLabelText("Store:")).toHaveTextContent("Global"); |
| 89 | + }); |
| 90 | + |
| 91 | + test("sets status", () => { |
| 92 | + renderComponent(); |
| 93 | + expect(screen.getByLabelText("Status:")).toHaveTextContent("Published"); |
| 94 | + }); |
| 95 | + |
| 96 | + test("changing field enables buttons", async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + renderComponent(); |
| 99 | + await user.click(screen.getByLabelText("Update metadata on release:")); |
| 100 | + expect(screen.getByRole("button", { name: "Revert" })).not.toHaveAttribute( |
| 101 | + "aria-disabled", |
| 102 | + ); |
| 103 | + expect(screen.getByRole("button", { name: "Save" })).not.toHaveAttribute( |
| 104 | + "aria-disabled", |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + test("'Update metadata on release' shows warning if selected", () => { |
| 109 | + renderComponent(); |
| 110 | + expect( |
| 111 | + screen.getByText(updateMetadataOnReleaseNotification), |
| 112 | + ).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + test("'Update metadata on release' warning not shown if not selected", async () => { |
| 116 | + const user = userEvent.setup(); |
| 117 | + renderComponent(); |
| 118 | + await user.click(screen.getByLabelText("Update metadata on release:")); |
| 119 | + expect( |
| 120 | + screen.queryByText(updateMetadataOnReleaseNotification), |
| 121 | + ).not.toBeInTheDocument(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments