Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit f0c5159

Browse files
estellecommentmcalinghee
authored andcommitted
Refactor tests
1 parent 2c1d424 commit f0c5159

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

test/components/views/rooms/RoomListHeader-test.tsx

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ const checkIsDisabled = menuItem => {
101101
expect(menuItem.props()['aria-disabled']).toBeTruthy();
102102
};
103103

104+
const checkMenuLabels = (items, labelArray) => {
105+
expect(items).toHaveLength(labelArray.length);
106+
107+
const checkLabel = (item, label) => {
108+
expect(item.find(".mx_IconizedContextMenu_label").text()).toBe(label);
109+
};
110+
111+
labelArray.forEach((label, index) => {
112+
console.log('index', index, 'label', label);
113+
checkLabel(items.at(index), label);
114+
});
115+
};
116+
104117
describe("RoomListHeader", () => {
105118
let client: MatrixClient;
106119

@@ -137,14 +150,15 @@ describe("RoomListHeader", () => {
137150

138151
const menu = wrapper.find(".mx_IconizedContextMenu");
139152
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
140-
expect(items).toHaveLength(6);
141-
expect(items.at(0).text()).toBe("Space home");
142-
expect(items.at(1).text()).toBe("Manage & explore rooms");
143-
expect(items.at(2).text()).toBe("Preferences");
144-
expect(items.at(3).text()).toBe("Settings");
145-
expect(items.at(4).text()).toBe("Room");
146-
// Look for label within the item, to ignore the text of the "Beta" pill if present.
147-
expect(items.at(5).find(".mx_IconizedContextMenu_label").text()).toBe("Space");
153+
154+
checkMenuLabels(items, [
155+
"Space home",
156+
"Manage & explore rooms",
157+
"Preferences",
158+
"Settings",
159+
"Room",
160+
"Space",
161+
]);
148162
});
149163

150164
it("renders a plus menu for spaces", async () => {
@@ -154,12 +168,12 @@ describe("RoomListHeader", () => {
154168
const menu = wrapper.find(".mx_IconizedContextMenu");
155169
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
156170

157-
expect(items).toHaveLength(4);
158-
expect(items.at(0).text()).toBe("New room");
159-
expect(items.at(1).text()).toBe("Explore rooms");
160-
expect(items.at(2).text()).toBe("Add existing room");
161-
// Look for label within the item, to ignore the text of the "Beta" pill if present.
162-
expect(items.at(3).find(".mx_IconizedContextMenu_label").text()).toBe("Add space");
171+
checkMenuLabels(items, [
172+
"New room",
173+
"Explore rooms",
174+
"Add existing room",
175+
"Add space",
176+
]);
163177
});
164178

165179
it("closes menu if space changes from under it", async () => {
@@ -193,12 +207,14 @@ describe("RoomListHeader", () => {
193207

194208
const menu = wrapper.find(".mx_IconizedContextMenu");
195209
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
196-
expect(items).toHaveLength(5);
197-
expect(items.at(0).text()).toBe("Space home");
198-
expect(items.at(1).text()).toBe("Manage & explore rooms");
199-
expect(items.at(2).text()).toBe("Preferences");
200-
expect(items.at(3).text()).toBe("Settings");
201-
expect(items.at(4).text()).toBe("Room");
210+
checkMenuLabels(items, [
211+
"Space home",
212+
"Manage & explore rooms",
213+
"Preferences",
214+
"Settings",
215+
"Room",
216+
// no add space
217+
]);
202218
});
203219

204220
it('does not render Add Room when user does not have permission to add rooms', async () => {
@@ -210,29 +226,17 @@ describe("RoomListHeader", () => {
210226

211227
const menu = wrapper.find(".mx_IconizedContextMenu");
212228
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
213-
expect(items).toHaveLength(5);
214-
expect(items.at(0).text()).toBe("Space home");
215-
expect(items.at(1).text()).toBe("Explore rooms");
216-
expect(items.at(2).text()).toBe("Preferences");
217-
expect(items.at(3).text()).toBe("Settings");
218-
// Look for label within the item, to ignore the text of the "Beta" pill if present.
219-
expect(items.at(4).find(".mx_IconizedContextMenu_label").text()).toBe("Space");
229+
checkMenuLabels(items, [
230+
"Space home",
231+
"Explore rooms", // not Manage & explore rooms
232+
"Preferences",
233+
"Settings",
234+
// no add room
235+
"Space",
236+
]);
220237
});
221238
});
222239

223-
const checkMenuLabels = (items, labelArray) => {
224-
expect(items).toHaveLength(labelArray.length);
225-
226-
const checkLabel = (item, label) => {
227-
expect(item.find(".mx_IconizedContextMenu_label").text()).toBe(label);
228-
};
229-
230-
labelArray.forEach((label, index) => {
231-
console.log('index', index, 'label', label);
232-
checkLabel(items.at(index), label);
233-
});
234-
};
235-
236240
describe('Plus menu', () => {
237241
it('does not render Add Space when user does not have permission to add spaces', async () => {
238242
// User does not have permission to add spaces, anywhere
@@ -248,10 +252,11 @@ describe("RoomListHeader", () => {
248252
"New room",
249253
"Explore rooms",
250254
"Add existing room",
255+
// no Add space
251256
]);
252257
});
253258

254-
it('does not render Add Room when user does not have permission to add rooms', async () => {
259+
it('disables Add Room when user does not have permission to add rooms', async () => {
255260
// User does not have permission to add rooms
256261
blockUIComponent(UIComponent.CreateRooms);
257262

@@ -267,12 +272,15 @@ describe("RoomListHeader", () => {
267272
"Add existing room",
268273
"Add space",
269274
]);
275+
276+
// "Add existing room" is disabled
277+
checkIsDisabled(items.at(2));
270278
});
271279
});
272280
});
273281

274282
describe('adding children to space', () => {
275-
it('if user cannot add children to space, MainMenu buttons are hidden', async () => {
283+
it('if user cannot add children to space, MainMenu adding buttons are hidden', async () => {
276284
const testSpace = setupSpace(client);
277285
mocked(testSpace.currentState.maySendStateEvent).mockImplementation(
278286
(stateEventType, userId) => stateEventType !== EventType.SpaceChild);
@@ -281,14 +289,17 @@ describe("RoomListHeader", () => {
281289

282290
const menu = wrapper.find(".mx_IconizedContextMenu");
283291
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
284-
expect(items).toHaveLength(4);
285-
expect(items.at(0).text()).toBe("Space home");
286-
expect(items.at(1).text()).toBe("Explore rooms");
287-
expect(items.at(2).text()).toBe("Preferences");
288-
expect(items.at(3).text()).toBe("Settings");
292+
checkMenuLabels(items, [
293+
"Space home",
294+
"Explore rooms", // not Manage & explore rooms
295+
"Preferences",
296+
"Settings",
297+
// no add room
298+
// no add space
299+
]);
289300
});
290301

291-
it('if user cannot add children to space, PlusMenu buttons are disabled', async () => {
302+
it('if user cannot add children to space, PlusMenu add buttons are disabled', async () => {
292303
const testSpace = setupSpace(client);
293304
mocked(testSpace.currentState.maySendStateEvent).mockImplementation(
294305
(stateEventType, userId) => stateEventType !== EventType.SpaceChild);
@@ -297,12 +308,17 @@ describe("RoomListHeader", () => {
297308

298309
const menu = wrapper.find(".mx_IconizedContextMenu");
299310
const items = menu.find(".mx_IconizedContextMenu_item").hostNodes();
300-
expect(items).toHaveLength(4);
301-
expect(items.at(0).text()).toBe("New room");
302-
expect(items.at(1).text()).toBe("Explore rooms");
303-
expect(items.at(2).text()).toBe("Add existing room");
311+
312+
checkMenuLabels(items, [
313+
"New room",
314+
"Explore rooms",
315+
"Add existing room",
316+
"Add space",
317+
]);
318+
319+
// "Add existing room" is disabled
304320
checkIsDisabled(items.at(2));
305-
expect(items.at(3).find(".mx_IconizedContextMenu_label").text()).toBe("Add space");
321+
// "Add space" is disabled
306322
checkIsDisabled(items.at(3));
307323
});
308324
});

0 commit comments

Comments
 (0)