|
| 1 | +import { screen, within } from "@testing-library/react"; |
| 2 | +import { HttpResponse, http } from "msw"; |
| 3 | +import { |
| 4 | + MockDefaultOrganization, |
| 5 | + MockOrganization2, |
| 6 | +} from "testHelpers/entities"; |
| 7 | +import { |
| 8 | + renderWithManagementSettingsLayout, |
| 9 | + waitForLoaderToBeRemoved, |
| 10 | +} from "testHelpers/renderHelpers"; |
| 11 | +import { server } from "testHelpers/server"; |
| 12 | +import OrganizationSettingsPage from "./OrganizationSettingsPage"; |
| 13 | + |
| 14 | +jest.spyOn(console, "error").mockImplementation(() => {}); |
| 15 | + |
| 16 | +const renderRootPage = async () => { |
| 17 | + renderWithManagementSettingsLayout(<OrganizationSettingsPage />, { |
| 18 | + route: "/organizations", |
| 19 | + path: "/organizations/:organization?", |
| 20 | + }); |
| 21 | + await waitForLoaderToBeRemoved(); |
| 22 | +}; |
| 23 | + |
| 24 | +const renderPage = async (orgName: string) => { |
| 25 | + renderWithManagementSettingsLayout(<OrganizationSettingsPage />, { |
| 26 | + route: `/organizations/${orgName}`, |
| 27 | + path: "/organizations/:organization", |
| 28 | + }); |
| 29 | + await waitForLoaderToBeRemoved(); |
| 30 | +}; |
| 31 | + |
| 32 | +describe("OrganizationSettingsPage", () => { |
| 33 | + it("has no organizations", async () => { |
| 34 | + server.use( |
| 35 | + http.get("/api/v2/organizations", () => { |
| 36 | + return HttpResponse.json([]); |
| 37 | + }), |
| 38 | + http.post("/api/v2/authcheck", async () => { |
| 39 | + return HttpResponse.json({ |
| 40 | + [`${MockDefaultOrganization.id}.editOrganization`]: true, |
| 41 | + viewDeploymentValues: true, |
| 42 | + }); |
| 43 | + }), |
| 44 | + ); |
| 45 | + await renderRootPage(); |
| 46 | + await screen.findByText("No organizations found"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("has no editable organizations", async () => { |
| 50 | + server.use( |
| 51 | + http.get("/api/v2/organizations", () => { |
| 52 | + return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); |
| 53 | + }), |
| 54 | + http.post("/api/v2/authcheck", async () => { |
| 55 | + return HttpResponse.json({ |
| 56 | + viewDeploymentValues: true, |
| 57 | + }); |
| 58 | + }), |
| 59 | + ); |
| 60 | + await renderRootPage(); |
| 61 | + await screen.findByText("No organizations found"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("redirects to default organization", async () => { |
| 65 | + server.use( |
| 66 | + http.get("/api/v2/organizations", () => { |
| 67 | + // Default always preferred regardless of order. |
| 68 | + return HttpResponse.json([MockOrganization2, MockDefaultOrganization]); |
| 69 | + }), |
| 70 | + http.post("/api/v2/authcheck", async () => { |
| 71 | + return HttpResponse.json({ |
| 72 | + [`${MockDefaultOrganization.id}.editOrganization`]: true, |
| 73 | + [`${MockOrganization2.id}.editOrganization`]: true, |
| 74 | + viewDeploymentValues: true, |
| 75 | + }); |
| 76 | + }), |
| 77 | + ); |
| 78 | + await renderRootPage(); |
| 79 | + const form = screen.getByTestId("org-settings-form"); |
| 80 | + expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue( |
| 81 | + MockDefaultOrganization.name, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it("redirects to non-default organization", async () => { |
| 86 | + server.use( |
| 87 | + http.get("/api/v2/organizations", () => { |
| 88 | + return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); |
| 89 | + }), |
| 90 | + http.post("/api/v2/authcheck", async () => { |
| 91 | + return HttpResponse.json({ |
| 92 | + [`${MockOrganization2.id}.editOrganization`]: true, |
| 93 | + viewDeploymentValues: true, |
| 94 | + }); |
| 95 | + }), |
| 96 | + ); |
| 97 | + await renderRootPage(); |
| 98 | + const form = screen.getByTestId("org-settings-form"); |
| 99 | + expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue( |
| 100 | + MockOrganization2.name, |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it("cannot find organization", async () => { |
| 105 | + server.use( |
| 106 | + http.get("/api/v2/organizations", () => { |
| 107 | + return HttpResponse.json([MockDefaultOrganization, MockOrganization2]); |
| 108 | + }), |
| 109 | + http.post("/api/v2/authcheck", async () => { |
| 110 | + return HttpResponse.json({ |
| 111 | + [`${MockOrganization2.id}.editOrganization`]: true, |
| 112 | + viewDeploymentValues: true, |
| 113 | + }); |
| 114 | + }), |
| 115 | + ); |
| 116 | + await renderPage("the-endless-void"); |
| 117 | + await screen.findByText("Organization not found"); |
| 118 | + }); |
| 119 | +}); |
0 commit comments