Skip to content

Commit 214c5f7

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

File tree

2 files changed

+76
-64
lines changed

2 files changed

+76
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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: [
17+
withAuthProvider,
18+
withDashboardProvider,
19+
],
20+
parameters: {
21+
user: MockUser,
22+
permissions: { viewDeploymentValues: true },
23+
queries: [
24+
{
25+
key: ["organizations", [MockDefaultOrganization.id], "permissions"],
26+
data: {},
27+
},
28+
],
29+
},
30+
};
31+
32+
export default meta;
33+
type Story = StoryObj<typeof OrganizationSettingsPage>;
34+
35+
export const NoRedirectableOrganizations: Story = {};
36+
37+
export const OrganizationDoesNotExist: Story = {
38+
parameters: {
39+
reactRouter: reactRouterParameters({
40+
location: { pathParams: { organization: "does-not-exist" } },
41+
routing: { path: "/organizations/:organization" },
42+
}),
43+
},
44+
};
45+
46+
export const CannotEditOrganization: Story = {
47+
parameters: {
48+
reactRouter: reactRouterParameters({
49+
location: { pathParams: { organization: MockDefaultOrganization.name } },
50+
routing: { path: "/organizations/:organization" },
51+
}),
52+
},
53+
};
54+
55+
export const CanEditOrganization: Story = {
56+
parameters: {
57+
reactRouter: reactRouterParameters({
58+
location: { pathParams: { organization: MockDefaultOrganization.name } },
59+
routing: { path: "/organizations/:organization" },
60+
}),
61+
queries: [
62+
{
63+
key: ["organizations", [MockDefaultOrganization.id], "permissions"],
64+
data: {
65+
[MockDefaultOrganization.id]: {
66+
editOrganization: true,
67+
},
68+
},
69+
},
70+
],
71+
},
72+
};

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)