@@ -14,15 +14,18 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
+ import "@testing-library/jest-dom" ;
17
18
import React from "react" ;
18
- import { act , render , screen } from "@testing-library/react" ;
19
+ import { act , render , screen , waitFor } from "@testing-library/react" ;
19
20
20
- import { IRoomState } from "../../../../../src/components/structures/RoomView " ;
21
+ import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext " ;
21
22
import RoomContext , { TimelineRenderingType } from "../../../../../src/contexts/RoomContext" ;
23
+ import defaultDispatcher from "../../../../../src/dispatcher/dispatcher" ;
24
+ import { Action } from "../../../../../src/dispatcher/actions" ;
25
+ import { IRoomState } from "../../../../../src/components/structures/RoomView" ;
22
26
import { Layout } from "../../../../../src/settings/enums/Layout" ;
23
- import { createTestClient , mkEvent , mkStubRoom } from "../../../../test-utils" ;
24
- import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext" ;
25
27
import { WysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer/WysiwygComposer" ;
28
+ import { createTestClient , mkEvent , mkStubRoom } from "../../../../test-utils" ;
26
29
27
30
// The wysiwyg fetch wasm bytes and a specific workaround is needed to make it works in a node (jest) environnement
28
31
// See https://github.com/matrix-org/matrix-wysiwyg/blob/main/platforms/web/test.setup.ts
@@ -92,7 +95,7 @@ describe('WysiwygComposer', () => {
92
95
} ;
93
96
94
97
let sendMessage : ( ) => void ;
95
- const customRender = ( onChange = ( content : string ) => void 0 , disabled = false ) => {
98
+ const customRender = ( onChange = ( _content : string ) => void 0 , disabled = false ) => {
96
99
return render (
97
100
< MatrixClientContext . Provider value = { mockClient } >
98
101
< RoomContext . Provider value = { defaultRoomContext } >
@@ -140,5 +143,58 @@ describe('WysiwygComposer', () => {
140
143
expect ( mockClient . sendMessage ) . toBeCalledWith ( 'myfakeroom' , null , expectedContent ) ;
141
144
expect ( screen . getByRole ( 'textbox' ) ) . toHaveFocus ( ) ;
142
145
} ) ;
146
+
147
+ it ( 'Should focus when receiving an Action.FocusSendMessageComposer action' , async ( ) => {
148
+ // Given we don't have focus
149
+ customRender ( ( ) => { } , false ) ;
150
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveFocus ( ) ;
151
+
152
+ // When we send the right action
153
+ defaultDispatcher . dispatch ( {
154
+ action : Action . FocusSendMessageComposer ,
155
+ context : null ,
156
+ } ) ;
157
+
158
+ // Then the component gets the focus
159
+ await waitFor ( ( ) => expect ( screen . getByRole ( 'textbox' ) ) . toHaveFocus ( ) ) ;
160
+ } ) ;
161
+
162
+ it ( 'Should focus when receiving a reply_to_event action' , async ( ) => {
163
+ // Given we don't have focus
164
+ customRender ( ( ) => { } , false ) ;
165
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveFocus ( ) ;
166
+
167
+ // When we send the right action
168
+ defaultDispatcher . dispatch ( {
169
+ action : "reply_to_event" ,
170
+ context : null ,
171
+ } ) ;
172
+
173
+ // Then the component gets the focus
174
+ await waitFor ( ( ) => expect ( screen . getByRole ( 'textbox' ) ) . toHaveFocus ( ) ) ;
175
+ } ) ;
176
+
177
+ it ( 'Should not focus when disabled' , async ( ) => {
178
+ // Given we don't have focus and we are disabled
179
+ customRender ( ( ) => { } , true ) ;
180
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveFocus ( ) ;
181
+
182
+ // When we send an action that would cause us to get focus
183
+ defaultDispatcher . dispatch ( {
184
+ action : Action . FocusSendMessageComposer ,
185
+ context : null ,
186
+ } ) ;
187
+ // (Send a second event to exercise the clearTimeout logic)
188
+ defaultDispatcher . dispatch ( {
189
+ action : Action . FocusSendMessageComposer ,
190
+ context : null ,
191
+ } ) ;
192
+
193
+ // Wait for event dispatch to happen
194
+ await new Promise ( ( r ) => setTimeout ( r , 200 ) ) ;
195
+
196
+ // Then we don't get it because we are disabled
197
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveFocus ( ) ;
198
+ } ) ;
143
199
} ) ;
144
200
0 commit comments