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

Commit 153a3a7

Browse files
committed
Add hook test
1 parent 676c65b commit 153a3a7

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// eslint-disable-next-line deprecate/import
18+
import { mount } from "enzyme";
19+
import { sleep } from "matrix-js-sdk/src/utils";
20+
import React from "react";
21+
import { act } from "react-dom/test-utils";
22+
import { mocked } from "jest-mock";
23+
import { SlidingSync } from "matrix-js-sdk/src/sliding-sync";
24+
import { Room } from "matrix-js-sdk/src/matrix";
25+
26+
import { useSlidingSyncRoomSearch } from "../../src/hooks/useSlidingSyncRoomSearch";
27+
import { MockEventEmitter, stubClient } from "../test-utils";
28+
import { SlidingSyncManager } from "../../src/SlidingSyncManager";
29+
30+
// hooks must be inside a React component else you get:
31+
// "Invalid hook call. Hooks can only be called inside of the body of a function component."
32+
function RoomSearchComponent({ onClick }) {
33+
const roomSearch = useSlidingSyncRoomSearch();
34+
return <div onClick={() => onClick(roomSearch)} />;
35+
}
36+
37+
describe("useSlidingSyncRoomSearch", () => {
38+
it("should display rooms when searching", async () => {
39+
const client = stubClient();
40+
const roomA = new Room("!a:localhost", client, client.getUserId());
41+
const roomB = new Room("!b:localhost", client, client.getUserId());
42+
const slidingSync = mocked(
43+
new MockEventEmitter({
44+
getListData: jest.fn(),
45+
}) as unknown as SlidingSync,
46+
);
47+
jest.spyOn(SlidingSyncManager.instance, "ensureListRegistered").mockResolvedValue({
48+
ranges: [[0, 9]],
49+
});
50+
SlidingSyncManager.instance.slidingSync = slidingSync;
51+
mocked(slidingSync.getListData).mockReturnValue({
52+
joinedCount: 2,
53+
roomIndexToRoomId: {
54+
0: roomA.roomId,
55+
1: roomB.roomId,
56+
},
57+
});
58+
mocked(client.getRoom).mockImplementation((roomId) => {
59+
switch (roomId) {
60+
case roomA.roomId:
61+
return roomA;
62+
case roomB.roomId:
63+
return roomB;
64+
default:
65+
return null;
66+
}
67+
});
68+
69+
// first check that everything is empty and then do the search
70+
let executeHook = (roomSearch) => {
71+
expect(roomSearch.loading).toBe(false);
72+
expect(roomSearch.rooms).toEqual([]);
73+
roomSearch.search({
74+
limit: 10,
75+
query: "foo",
76+
});
77+
};
78+
const wrapper = mount(
79+
<RoomSearchComponent
80+
onClick={(roomSearch) => {
81+
executeHook(roomSearch);
82+
}}
83+
/>,
84+
);
85+
86+
// run the query
87+
await act(async () => {
88+
await sleep(1);
89+
wrapper.simulate("click");
90+
return act(() => sleep(1));
91+
});
92+
// now we expect there to be rooms
93+
executeHook = (roomSearch) => {
94+
expect(roomSearch.loading).toBe(false);
95+
expect(roomSearch.rooms).toEqual([roomA, roomB]);
96+
};
97+
98+
// run the query
99+
await act(async () => {
100+
await sleep(1);
101+
wrapper.simulate("click");
102+
return act(() => sleep(1));
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)