Skip to content

Commit addb2ca

Browse files
committed
Tests for getRoomUpgradeHistory
1 parent 6cf6a0c commit addb2ca

File tree

1 file changed

+122
-40
lines changed

1 file changed

+122
-40
lines changed

Diff for: spec/unit/matrix-client.spec.ts

+122-40
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ describe("MatrixClient", function () {
21982198
});
21992199
});
22002200

2201-
describe("getVisibleRooms", () => {
2201+
describe("room lists and history", () => {
22022202
function roomCreateEvent(newRoomId: string, predecessorRoomId: string): MatrixEvent {
22032203
return new MatrixEvent({
22042204
content: {
@@ -2234,45 +2234,127 @@ describe("MatrixClient", function () {
22342234
});
22352235
}
22362236

2237-
it("Returns an empty list if there are no rooms", () => {
2238-
client.store = new StubStore();
2239-
client.store.getRooms = () => [];
2240-
const rooms = client.getVisibleRooms();
2241-
expect(rooms).toHaveLength(0);
2242-
});
2243-
2244-
it("Returns all non-replaced rooms", () => {
2245-
const room1 = new Room("room1", client, "@carol:alexandria.example.com");
2246-
const room2 = new Room("room2", client, "@daryl:alexandria.example.com");
2247-
client.store = new StubStore();
2248-
client.store.getRooms = () => [room1, room2];
2249-
const rooms = client.getVisibleRooms();
2250-
expect(rooms).toContain(room1);
2251-
expect(rooms).toContain(room2);
2252-
expect(rooms).toHaveLength(2);
2253-
});
2254-
2255-
it("Does not return replaced rooms", () => {
2256-
// Given 4 rooms, 2 of which have been replaced
2257-
const room1 = new Room("room1", client, "@carol:alexandria.example.com");
2258-
const replacedRoom1 = new Room("replacedRoom1", client, "@carol:alexandria.example.com");
2259-
const replacedRoom2 = new Room("replacedRoom2", client, "@carol:alexandria.example.com");
2260-
const room2 = new Room("room2", client, "@daryl:alexandria.example.com");
2261-
client.store = new StubStore();
2262-
client.store.getRooms = () => [room1, replacedRoom1, replacedRoom2, room2];
2263-
room1.addLiveEvents([roomCreateEvent(room1.roomId, replacedRoom1.roomId)], {});
2264-
room2.addLiveEvents([roomCreateEvent(room2.roomId, replacedRoom2.roomId)], {});
2265-
replacedRoom1.addLiveEvents([tombstoneEvent(room1.roomId, replacedRoom1.roomId)], {});
2266-
replacedRoom2.addLiveEvents([tombstoneEvent(room2.roomId, replacedRoom2.roomId)], {});
2267-
2268-
// When we ask for the visible rooms
2269-
const rooms = client.getVisibleRooms();
2270-
2271-
// Then we only get the ones that have not been replaced
2272-
expect(rooms).not.toContain(replacedRoom1);
2273-
expect(rooms).not.toContain(replacedRoom2);
2274-
expect(rooms).toContain(room1);
2275-
expect(rooms).toContain(room2);
2237+
describe("getVisibleRooms", () => {
2238+
it("Returns an empty list if there are no rooms", () => {
2239+
client.store = new StubStore();
2240+
client.store.getRooms = () => [];
2241+
const rooms = client.getVisibleRooms();
2242+
expect(rooms).toHaveLength(0);
2243+
});
2244+
2245+
it("Returns all non-replaced rooms", () => {
2246+
const room1 = new Room("room1", client, "@carol:alexandria.example.com");
2247+
const room2 = new Room("room2", client, "@daryl:alexandria.example.com");
2248+
client.store = new StubStore();
2249+
client.store.getRooms = () => [room1, room2];
2250+
const rooms = client.getVisibleRooms();
2251+
expect(rooms).toContain(room1);
2252+
expect(rooms).toContain(room2);
2253+
expect(rooms).toHaveLength(2);
2254+
});
2255+
2256+
it("Does not return replaced rooms", () => {
2257+
// Given 4 rooms, 2 of which have been replaced
2258+
const room1 = new Room("room1", client, "@carol:alexandria.example.com");
2259+
const replacedRoom1 = new Room("replacedRoom1", client, "@carol:alexandria.example.com");
2260+
const replacedRoom2 = new Room("replacedRoom2", client, "@carol:alexandria.example.com");
2261+
const room2 = new Room("room2", client, "@daryl:alexandria.example.com");
2262+
client.store = new StubStore();
2263+
client.store.getRooms = () => [room1, replacedRoom1, replacedRoom2, room2];
2264+
room1.addLiveEvents([roomCreateEvent(room1.roomId, replacedRoom1.roomId)], {});
2265+
room2.addLiveEvents([roomCreateEvent(room2.roomId, replacedRoom2.roomId)], {});
2266+
replacedRoom1.addLiveEvents([tombstoneEvent(room1.roomId, replacedRoom1.roomId)], {});
2267+
replacedRoom2.addLiveEvents([tombstoneEvent(room2.roomId, replacedRoom2.roomId)], {});
2268+
2269+
// When we ask for the visible rooms
2270+
const rooms = client.getVisibleRooms();
2271+
2272+
// Then we only get the ones that have not been replaced
2273+
expect(rooms).not.toContain(replacedRoom1);
2274+
expect(rooms).not.toContain(replacedRoom2);
2275+
expect(rooms).toContain(room1);
2276+
expect(rooms).toContain(room2);
2277+
});
2278+
});
2279+
2280+
describe("getRoomUpgradeHistory", () => {
2281+
function createRoomHistory(): [Room, Room, Room, Room] {
2282+
const room1 = new Room("room1", client, "@carol:alexandria.example.com");
2283+
const room2 = new Room("room2", client, "@daryl:alexandria.example.com");
2284+
const room3 = new Room("room3", client, "@rick:helicopter.example.com");
2285+
const room4 = new Room("room4", client, "@michonne:hawthorne.example.com");
2286+
2287+
room1.addLiveEvents([tombstoneEvent(room2.roomId, room1.roomId)], {});
2288+
room2.addLiveEvents([roomCreateEvent(room2.roomId, room1.roomId)]);
2289+
2290+
room2.addLiveEvents([tombstoneEvent(room3.roomId, room2.roomId)], {});
2291+
room3.addLiveEvents([roomCreateEvent(room3.roomId, room2.roomId)]);
2292+
2293+
room3.addLiveEvents([tombstoneEvent(room4.roomId, room3.roomId)], {});
2294+
room4.addLiveEvents([roomCreateEvent(room4.roomId, room3.roomId)]);
2295+
2296+
mocked(store.getRoom).mockImplementation((roomId: string) => {
2297+
switch (roomId) {
2298+
case "room1":
2299+
return room1;
2300+
case "room2":
2301+
return room2;
2302+
case "room3":
2303+
return room3;
2304+
case "room4":
2305+
return room4;
2306+
default:
2307+
return null;
2308+
}
2309+
});
2310+
2311+
return [room1, room2, room3, room4];
2312+
}
2313+
2314+
it("Returns an empty list if room does not exist", () => {
2315+
const history = client.getRoomUpgradeHistory("roomthatdoesnotexist");
2316+
expect(history).toHaveLength(0);
2317+
});
2318+
2319+
it("Returns just this room if there is no predecessor", () => {
2320+
const mainRoom = new Room("mainRoom", client, "@carol:alexandria.example.com");
2321+
mocked(store.getRoom).mockReturnValue(mainRoom);
2322+
const history = client.getRoomUpgradeHistory(mainRoom.roomId);
2323+
expect(history).toEqual([mainRoom]);
2324+
});
2325+
2326+
it("Returns the predecessors of this room", () => {
2327+
const [room1, room2, room3, room4] = createRoomHistory();
2328+
const history = client.getRoomUpgradeHistory(room4.roomId);
2329+
expect(history.map((room) => room.roomId)).toEqual([
2330+
room1.roomId,
2331+
room2.roomId,
2332+
room3.roomId,
2333+
room4.roomId,
2334+
]);
2335+
});
2336+
2337+
it("Returns the subsequent rooms", () => {
2338+
const [room1, room2, room3, room4] = createRoomHistory();
2339+
const history = client.getRoomUpgradeHistory(room1.roomId);
2340+
expect(history.map((room) => room.roomId)).toEqual([
2341+
room1.roomId,
2342+
room2.roomId,
2343+
room3.roomId,
2344+
room4.roomId,
2345+
]);
2346+
});
2347+
2348+
it("Returns the predecessors and subsequent rooms", () => {
2349+
const [room1, room2, room3, room4] = createRoomHistory();
2350+
const history = client.getRoomUpgradeHistory(room3.roomId);
2351+
expect(history.map((room) => room.roomId)).toEqual([
2352+
room1.roomId,
2353+
room2.roomId,
2354+
room3.roomId,
2355+
room4.roomId,
2356+
]);
2357+
});
22762358
});
22772359
});
22782360
});

0 commit comments

Comments
 (0)