Skip to content

Commit e3ac9a2

Browse files
committed
Use Storybook for non-redirect org settings tests
1 parent 7f520e7 commit e3ac9a2

File tree

2 files changed

+73
-64
lines changed

2 files changed

+73
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { Meta, StoryObj } from "@storybook/react";
2+
import { reactRouterParameters } from "storybook-addon-remix-react-router";
3+
import {
4+
MockDefaultOrganization,
5+
MockOrganization2,
6+
MockUser,
7+
} from "testHelpers/entities";
8+
import { organizationsKey } from "api/queries/organizations";
9+
import { permissionsToCheck } from "contexts/auth/permissions";
10+
import { withAuthProvider, withDashboardProvider } from "testHelpers/storybook";
11+
import OrganizationSettingsPage from "./OrganizationSettingsPage";
12+
13+
const meta: Meta<typeof OrganizationSettingsPage> = {
14+
title: "pages/OrganizationSettingsPage",
15+
component: OrganizationSettingsPage,
16+
decorators: [withAuthProvider, withDashboardProvider],
17+
parameters: {
18+
user: MockUser,
19+
permissions: { viewDeploymentValues: true },
20+
queries: [
21+
{
22+
key: ["organizations", [MockDefaultOrganization.id], "permissions"],
23+
data: {},
24+
},
25+
],
26+
},
27+
};
28+
29+
export default meta;
30+
type Story = StoryObj<typeof OrganizationSettingsPage>;
31+
32+
export const NoRedirectableOrganizations: Story = {};
33+
34+
export const OrganizationDoesNotExist: Story = {
35+
parameters: {
36+
reactRouter: reactRouterParameters({
37+
location: { pathParams: { organization: "does-not-exist" } },
38+
routing: { path: "/organizations/:organization" },
39+
}),
40+
},
41+
};
42+
43+
export const CannotEditOrganization: Story = {
44+
parameters: {
45+
reactRouter: reactRouterParameters({
46+
location: { pathParams: { organization: MockDefaultOrganization.name } },
47+
routing: { path: "/organizations/:organization" },
48+
}),
49+
},
50+
};
51+
52+
export const CanEditOrganization: Story = {
53+
parameters: {
54+
reactRouter: reactRouterParameters({
55+
location: { pathParams: { organization: MockDefaultOrganization.name } },
56+
routing: { path: "/organizations/:organization" },
57+
}),
58+
queries: [
59+
{
60+
key: ["organizations", [MockDefaultOrganization.id], "permissions"],
61+
data: {
62+
[MockDefaultOrganization.id]: {
63+
editOrganization: true,
64+
},
65+
},
66+
},
67+
],
68+
},
69+
};

site/src/pages/ManagementSettingsPage/OrganizationSettingsPage.test.tsx

+4-64
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,15 @@ import OrganizationSettingsPage from "./OrganizationSettingsPage";
1313

1414
jest.spyOn(console, "error").mockImplementation(() => {});
1515

16-
const renderRootPage = async () => {
16+
const renderPage = async () => {
1717
renderWithManagementSettingsLayout(<OrganizationSettingsPage />, {
1818
route: "/organizations",
1919
path: "/organizations/:organization?",
2020
});
2121
await waitForLoaderToBeRemoved();
2222
};
2323

24-
const renderPage = async (orgName: string) => {
25-
renderWithManagementSettingsLayout(<OrganizationSettingsPage />, {
26-
route: `/organizations/${orgName}`,
27-
path: "/organizations/:organization",
28-
});
29-
await waitForLoaderToBeRemoved();
30-
};
31-
3224
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-
4925
it("has no editable organizations", async () => {
5026
server.use(
5127
http.get("/api/v2/organizations", () => {
@@ -57,7 +33,7 @@ describe("OrganizationSettingsPage", () => {
5733
});
5834
}),
5935
);
60-
await renderRootPage();
36+
await renderPage();
6137
await screen.findByText("No organizations found");
6238
});
6339

@@ -75,7 +51,7 @@ describe("OrganizationSettingsPage", () => {
7551
});
7652
}),
7753
);
78-
await renderRootPage();
54+
await renderPage();
7955
const form = screen.getByTestId("org-settings-form");
8056
expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue(
8157
MockDefaultOrganization.name,
@@ -94,46 +70,10 @@ describe("OrganizationSettingsPage", () => {
9470
});
9571
}),
9672
);
97-
await renderRootPage();
73+
await renderPage();
9874
const form = screen.getByTestId("org-settings-form");
9975
expect(within(form).getByRole("textbox", { name: "Name" })).toHaveValue(
10076
MockOrganization2.name,
10177
);
10278
});
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-
120-
it("cannot edit organization", async () => {
121-
server.use(
122-
http.get("/api/v2/organizations", () => {
123-
return HttpResponse.json([MockDefaultOrganization]);
124-
}),
125-
http.post("/api/v2/authcheck", async () => {
126-
return HttpResponse.json({
127-
viewDeploymentValues: true,
128-
});
129-
}),
130-
);
131-
// No form since they cannot edit, instead sees the summary view.
132-
await renderPage(MockDefaultOrganization.name);
133-
expect(screen.queryByTestId("org-settings-form")).not.toBeInTheDocument();
134-
await screen.findByRole("heading", {
135-
level: 1,
136-
name: MockDefaultOrganization.display_name,
137-
});
138-
});
13979
});

0 commit comments

Comments
 (0)