@@ -14,28 +14,16 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- // eslint-disable-next-line deprecate/import
18- import { mount } from "enzyme" ;
19- import { MatrixClient } from "matrix-js-sdk/src/matrix" ;
20- import { sleep } from "matrix-js-sdk/src/utils" ;
21- import React from "react" ;
22- import { act } from "react-dom/test-utils" ;
17+ import { waitFor } from "@testing-library/react" ;
18+ import { renderHook , act } from "@testing-library/react-hooks/dom" ;
19+ import { IRoomDirectoryOptions , MatrixClient } from "matrix-js-sdk/src/matrix" ;
2320
2421import { usePublicRoomDirectory } from "../../src/hooks/usePublicRoomDirectory" ;
2522import { MatrixClientPeg } from "../../src/MatrixClientPeg" ;
2623import { stubClient } from "../test-utils/test-utils" ;
2724
28- function PublicRoomComponent ( { onClick } : { onClick ( hook : ReturnType < typeof usePublicRoomDirectory > ) : void } ) {
29- const roomDirectory = usePublicRoomDirectory ( ) ;
30-
31- const { ready, loading, publicRooms } = roomDirectory ;
32-
33- return (
34- < div onClick = { ( ) => onClick ( roomDirectory ) } >
35- { ( ! ready || loading ) && `ready: ${ ready } , loading: ${ loading } ` }
36- { publicRooms [ 0 ] && `Name: ${ publicRooms [ 0 ] . name } ` }
37- </ div >
38- ) ;
25+ function render ( ) {
26+ return renderHook ( ( ) => usePublicRoomDirectory ( ) ) ;
3927}
4028
4129describe ( "usePublicRoomDirectory" , ( ) => {
@@ -47,65 +35,62 @@ describe("usePublicRoomDirectory", () => {
4735
4836 MatrixClientPeg . getHomeserverName = ( ) => "matrix.org" ;
4937 cli . getThirdpartyProtocols = ( ) => Promise . resolve ( { } ) ;
50- cli . publicRooms = ( { filter : { generic_search_term : query } } ) =>
51- Promise . resolve ( {
52- chunk : [
53- {
54- room_id : "hello world!" ,
55- name : query ,
56- world_readable : true ,
57- guest_can_join : true ,
58- num_joined_members : 1 ,
59- } ,
60- ] ,
38+ cli . publicRooms = ( { filter } : IRoomDirectoryOptions ) => {
39+ const chunk = filter ?. generic_search_term
40+ ? [
41+ {
42+ room_id : "hello world!" ,
43+ name : filter . generic_search_term ,
44+ world_readable : true ,
45+ guest_can_join : true ,
46+ num_joined_members : 1 ,
47+ } ,
48+ ]
49+ : [ ] ;
50+ return Promise . resolve ( {
51+ chunk,
6152 total_room_count_estimate : 1 ,
6253 } ) ;
54+ } ;
6355 } ) ;
6456
6557 it ( "should display public rooms when searching" , async ( ) => {
6658 const query = "ROOM NAME" ;
59+ const { result } = render ( ) ;
60+
61+ expect ( result . current . ready ) . toBe ( false ) ;
62+ expect ( result . current . loading ) . toBe ( false ) ;
6763
68- const wrapper = mount (
69- < PublicRoomComponent
70- onClick = { ( hook ) => {
71- hook . search ( {
72- limit : 1 ,
73- query,
74- } ) ;
75- } }
76- /> ,
77- ) ;
78-
79- expect ( wrapper . text ( ) ) . toBe ( "ready: false, loading: false" ) ;
80-
81- await act ( async ( ) => {
82- await sleep ( 1 ) ;
83- wrapper . simulate ( "click" ) ;
84- return act ( ( ) => sleep ( 1 ) ) ;
64+ act ( ( ) => {
65+ result . current . search ( {
66+ limit : 1 ,
67+ query,
68+ } ) ;
69+ } ) ;
70+
71+ await waitFor ( ( ) => {
72+ expect ( result . current . ready ) . toBe ( true ) ;
8573 } ) ;
8674
87- expect ( wrapper . text ( ) ) . toContain ( query ) ;
75+ expect ( result . current . publicRooms [ 0 ] . name ) . toBe ( query ) ;
8876 } ) ;
8977
9078 it ( "should work with empty queries" , async ( ) => {
91- const wrapper = mount (
92- < PublicRoomComponent
93- onClick = { ( hook ) => {
94- hook . search ( {
95- limit : 1 ,
96- query : "" ,
97- } ) ;
98- } }
99- /> ,
100- ) ;
101-
102- await act ( async ( ) => {
103- await sleep ( 1 ) ;
104- wrapper . simulate ( "click" ) ;
105- return act ( ( ) => sleep ( 1 ) ) ;
79+ const query = "ROOM NAME" ;
80+ const { result } = render ( ) ;
81+
82+ act ( ( ) => {
83+ result . current . search ( {
84+ limit : 1 ,
85+ query,
86+ } ) ;
87+ } ) ;
88+
89+ await waitFor ( ( ) => {
90+ expect ( result . current . ready ) . toBe ( true ) ;
10691 } ) ;
10792
108- expect ( wrapper . text ( ) ) . toBe ( "" ) ;
93+ expect ( result . current . publicRooms [ 0 ] . name ) . toEqual ( query ) ;
10994 } ) ;
11095
11196 it ( "should recover from a server exception" , async ( ) => {
@@ -114,22 +99,19 @@ describe("usePublicRoomDirectory", () => {
11499 } ;
115100 const query = "ROOM NAME" ;
116101
117- const wrapper = mount (
118- < PublicRoomComponent
119- onClick = { ( hook ) => {
120- hook . search ( {
121- limit : 1 ,
122- query,
123- } ) ;
124- } }
125- /> ,
126- ) ;
127- await act ( async ( ) => {
128- await sleep ( 1 ) ;
129- wrapper . simulate ( "click" ) ;
130- return act ( ( ) => sleep ( 1 ) ) ;
102+ const { result } = render ( ) ;
103+
104+ act ( ( ) => {
105+ result . current . search ( {
106+ limit : 1 ,
107+ query,
108+ } ) ;
109+ } ) ;
110+
111+ await waitFor ( ( ) => {
112+ expect ( result . current . ready ) . toBe ( true ) ;
131113 } ) ;
132114
133- expect ( wrapper . text ( ) ) . toBe ( "" ) ;
115+ expect ( result . current . publicRooms ) . toEqual ( [ ] ) ;
134116 } ) ;
135117} ) ;
0 commit comments