Skip to content

Commit cd3a42f

Browse files
committed
Tidy space creation tests and cover a little more of the setup process
1 parent 0f777aa commit cd3a42f

1 file changed

Lines changed: 73 additions & 59 deletions

File tree

test/unit-tests/components/structures/SpaceRoomView-test.tsx

Lines changed: 73 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalink
1818
import RightPanelStore from "../../../../src/stores/right-panel/RightPanelStore.ts";
1919
import DMRoomMap from "../../../../src/utils/DMRoomMap.ts";
2020
import { type IOpts } from "../../../../src/createRoom.ts";
21+
import SpaceStore from "../../../../src/stores/spaces/SpaceStore.ts";
2122

2223
describe("SpaceRoomView", () => {
2324
let cli: MockedObject<MatrixClient>;
@@ -128,99 +129,112 @@ describe("SpaceRoomView", () => {
128129
});
129130
});
130131

131-
describe("SpaceSetupFirstRooms", () => {
132-
beforeEach(async () => {
133-
await renderSpaceRoomView({
132+
describe("Spaces: creating a new community space", () => {
133+
it("asks what topics you want to discuss, creates rooms for them and offers to share", async () => {
134+
cli.createRoom.mockResolvedValueOnce({ room_id: "room1" }).mockResolvedValueOnce({ room_id: "room2" });
135+
SpaceStore.instance.addRoomToSpace = jest.fn();
136+
137+
// Given we are creating a space
138+
const view = await renderSpaceRoomView({
134139
createOpts: { preset: Preset.PublicChat },
140+
name: "My MySpace Space",
135141
});
136-
});
137142

138-
it("renders SpaceSetupFirstRooms with correct title and description", () => {
139-
expect(
140-
screen.getByText("What are some things you want to discuss in !space:example.org?"),
141-
).toBeInTheDocument();
142-
// using regex here since there's a stray <br />
143-
expect(screen.getByText(/let's create a room for each of them/i)).toBeInTheDocument();
143+
// Then we are asked what topics we want
144144
expect(
145-
screen.getByText(/you can add more later too, including already existing ones/i),
145+
view.getByRole("heading", { name: "What are some things you want to discuss in My MySpace Space?" }),
146146
).toBeInTheDocument();
147-
});
148147

149-
it("renders three input fields with correct placeholders", () => {
150-
expect(screen.getAllByPlaceholderText(/general/i)).toHaveLength(1);
151-
expect(screen.getAllByPlaceholderText(/random/i)).toHaveLength(1);
152-
expect(screen.getAllByPlaceholderText(/support/i)).toHaveLength(1);
153-
});
148+
// And some defaults are suggested
149+
expect(view.getByPlaceholderText(/general/i)).toBeInTheDocument();
150+
expect(view.getByPlaceholderText(/random/i)).toBeInTheDocument();
151+
expect(view.getByPlaceholderText(/support/i)).toBeInTheDocument();
154152

155-
it("updates input value when typed", () => {
156-
const input = screen.getAllByRole("textbox")[0];
157-
fireEvent.change(input, { target: { value: "My Room" } });
158-
expect(input).toHaveValue("My Room");
159-
});
153+
// When we enter some room names
154+
const input1 = view.getAllByRole("textbox")[0];
155+
const input2 = view.getAllByRole("textbox")[1];
156+
fireEvent.change(input1, { target: { value: "Room 1" } });
157+
fireEvent.change(input2, { target: { value: "Room 2" } });
160158

161-
it("shows 'Skip for now' when all fields are empty, 'Continue' when any field is filled", () => {
162-
// Clear all fields first
163-
screen.getAllByRole("textbox").forEach((input) => fireEvent.change(input, { target: { value: "" } }));
159+
// And click "Continue"
160+
const button = view.getByRole("button", { name: "Continue" });
161+
fireEvent.click(button);
164162

165-
// Should say 'Skip for now'
166-
const button = screen.getByRole("button");
167-
expect(button).toHaveValue("Skip for now");
163+
// Then we create 2 rooms
164+
await waitFor(() => {
165+
expect(cli.createRoom).toHaveBeenCalledTimes(2);
166+
});
168167

169-
// Fill a field
170-
fireEvent.change(screen.getAllByRole("textbox")[0], { target: { value: "Room" } });
171-
expect(button).toHaveValue("Continue");
172-
});
168+
// And offer the user to share this space
169+
await waitFor(() =>
170+
expect(view.getByRole("heading", { name: "Share My MySpace Space" })).toBeInTheDocument(),
171+
);
172+
expect(view.getByRole("button", { name: /Share invite link/ })).toBeInTheDocument();
173173

174-
it("calls onFinished with no argument when skipping", () => {
175-
const button = screen.getByRole("button");
176-
fireEvent.click(button);
177-
// Since onFinished is handled internally, check that SpaceSetupFirstRooms is no longer rendered
178-
expect(screen.queryByText(/setup_rooms_community_heading/i)).not.toBeInTheDocument();
174+
// And allow them to continue to the first room
175+
expect(view.getByRole("button", { name: "Go to my first room" })).toBeInTheDocument();
179176
});
180177

181-
it("calls createRoom for each non-empty field and onFinished with first room id", async () => {
182-
cli.createRoom.mockResolvedValueOnce({ room_id: "room1" }).mockResolvedValueOnce({ room_id: "room2" });
178+
it("shows 'Skip for now' when all fields are empty, 'Continue' when any field is filled", async () => {
179+
// Given we are creating a space
180+
const view = await renderSpaceRoomView({
181+
createOpts: { preset: Preset.PublicChat },
182+
});
183183

184-
fireEvent.change(screen.getAllByRole("textbox")[0], { target: { value: "Room A" } });
185-
fireEvent.change(screen.getAllByRole("textbox")[1], { target: { value: "Room B" } });
186-
fireEvent.click(screen.getByRole("button"));
184+
// When we clear all the topics
185+
view.getAllByRole("textbox").forEach((input) => fireEvent.change(input, { target: { value: "" } }));
187186

188-
await waitFor(() => {
189-
expect(cli.createRoom).toHaveBeenCalledTimes(2);
190-
});
191-
// After finishing, SpaceSetupFirstRooms should not be rendered
192-
expect(screen.queryByText(/setup_rooms_community_heading/i)).not.toBeInTheDocument();
187+
// Then the button reads "Skip for now"
188+
expect(view.getByRole("button", { name: "Skip for now" })).toBeVisible();
189+
190+
// But when we enter a topic
191+
fireEvent.change(view.getAllByRole("textbox")[0], { target: { value: "Room" } });
192+
193+
// Then the button says "Continue"
194+
expect(view.getByRole("button", { name: "Continue" })).toBeVisible();
193195
});
194196

195197
it("shows error message if room creation fails", async () => {
196-
// Force failure.
198+
// Given we are creating a space
199+
const view = await renderSpaceRoomView({
200+
createOpts: { preset: Preset.PublicChat },
201+
});
202+
203+
// And when we create a room it will fail
197204
cli.createRoom.mockRejectedValue(new Error("fail"));
198205

199-
// Create a room.
200-
fireEvent.change(screen.getAllByRole("textbox")[0], { target: { value: "Room A" } });
201-
fireEvent.click(screen.getByRole("button"));
206+
// When we create the space
207+
fireEvent.change(view.getAllByRole("textbox")[0], { target: { value: "Room A" } });
208+
fireEvent.click(view.getByRole("button", { name: "Continue" }));
202209

210+
// Then we display an error message because it failed
203211
await waitFor(() => {
204212
expect(
205-
screen.getByText((content) =>
206-
content.toLowerCase().includes("failed to create initial space rooms"),
207-
),
213+
view.getByText((content) => content.toLowerCase().includes("failed to create initial space rooms")),
208214
).toBeInTheDocument();
209215
});
210216
});
211217

212218
it("disables button and shows 'Creating rooms' while busy", async () => {
219+
// Given we are creating a space
220+
const view = await renderSpaceRoomView({
221+
createOpts: { preset: Preset.PublicChat },
222+
});
223+
224+
// And creating a room will be slow
213225
cli.createRoom.mockImplementation(
214226
() =>
215-
new Promise((resolve) => {
216-
/* intentionally unresolved to mock work by the server */
227+
new Promise(() => {
228+
// This promise never resolves
217229
}),
218230
);
219231

220-
fireEvent.change(screen.getAllByRole("textbox")[0], { target: { value: "Room A" } });
221-
fireEvent.click(screen.getByRole("button"));
232+
// When we create the space
233+
fireEvent.change(view.getAllByRole("textbox")[0], { target: { value: "Room A" } });
234+
fireEvent.click(view.getByRole("button", { name: "Continue" }));
222235

223-
const button = screen.getByRole("button");
236+
// Then the "Creating rooms..." message is displayed
237+
const button = view.getByRole("button");
224238
expect(button).toBeDisabled();
225239
expect(button).toHaveValue("Creating rooms…"); // Note the ellipsis
226240
});

0 commit comments

Comments
 (0)