@@ -16,49 +16,21 @@ limitations under the License.
1616
1717import "@testing-library/jest-dom" ;
1818import React from "react" ;
19- import { render , screen , waitFor } from "@testing-library/react" ;
20- import { WysiwygProps } from "@matrix-org/matrix-wysiwyg" ;
19+ import { fireEvent , render , screen , waitFor } from "@testing-library/react" ;
2120
2221import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext" ;
2322import RoomContext from "../../../../../src/contexts/RoomContext" ;
2423import defaultDispatcher from "../../../../../src/dispatcher/dispatcher" ;
2524import { Action } from "../../../../../src/dispatcher/actions" ;
2625import { IRoomState } from "../../../../../src/components/structures/RoomView" ;
27- import { createTestClient , getRoomContext , mkEvent , mkStubRoom } from "../../../../test-utils" ;
26+ import { createTestClient , flushPromises , getRoomContext , mkEvent , mkStubRoom } from "../../../../test-utils" ;
2827import { EditWysiwygComposer }
2928 from "../../../../../src/components/views/rooms/wysiwyg_composer" ;
3029import EditorStateTransfer from "../../../../../src/utils/EditorStateTransfer" ;
3130
32- const mockClear = jest . fn ( ) ;
33-
34- let initialContent : string ;
35- const defaultContent = '<b>html</b>' ;
36- let mockContent = defaultContent ;
37-
38- // The wysiwyg fetch wasm bytes and a specific workaround is needed to make it works in a node (jest) environnement
39- // See https://github.com/matrix-org/matrix-wysiwyg/blob/main/platforms/web/test.setup.ts
40- jest . mock ( "@matrix-org/matrix-wysiwyg" , ( ) => ( {
41- useWysiwyg : ( props : WysiwygProps ) => {
42- initialContent = props . initialContent ;
43- return {
44- ref : { current : null } ,
45- content : mockContent ,
46- isWysiwygReady : true ,
47- wysiwyg : { clear : mockClear } ,
48- actionStates : {
49- bold : 'enabled' ,
50- italic : 'enabled' ,
51- underline : 'enabled' ,
52- strikeThrough : 'enabled' ,
53- } ,
54- } ;
55- } ,
56- } ) ) ;
57-
5831describe ( 'EditWysiwygComposer' , ( ) => {
5932 afterEach ( ( ) => {
6033 jest . resetAllMocks ( ) ;
61- mockContent = defaultContent ;
6234 } ) ;
6335
6436 const mockClient = createTestClient ( ) ;
@@ -70,7 +42,7 @@ describe('EditWysiwygComposer', () => {
7042 "msgtype" : "m.text" ,
7143 "body" : "Replying to this" ,
7244 "format" : "org.matrix.custom.html" ,
73- "formatted_body" : " Replying <b>to</b> this new content" ,
45+ "formatted_body" : ' Replying <b>to</b> this new content' ,
7446 } ,
7547 event : true ,
7648 } ) ;
@@ -96,10 +68,12 @@ describe('EditWysiwygComposer', () => {
9668 describe ( 'Initialize with content' , ( ) => {
9769 it ( 'Should initialize useWysiwyg with html content' , async ( ) => {
9870 // When
99- customRender ( true ) ;
71+ customRender ( false , editorStateTransfer ) ;
72+ await waitFor ( ( ) => expect ( screen . getByRole ( 'textbox' ) ) . toHaveAttribute ( 'contentEditable' , "true" ) ) ;
10073
10174 // Then
102- expect ( initialContent ) . toBe ( mockEvent . getContent ( ) [ 'formatted_body' ] ) ;
75+ await waitFor ( ( ) =>
76+ expect ( screen . getByRole ( 'textbox' ) ) . toContainHTML ( mockEvent . getContent ( ) [ 'formatted_body' ] ) ) ;
10377 } ) ;
10478
10579 it ( 'Should initialize useWysiwyg with plain text content' , async ( ) => {
@@ -115,24 +89,29 @@ describe('EditWysiwygComposer', () => {
11589 event : true ,
11690 } ) ;
11791 const editorStateTransfer = new EditorStateTransfer ( mockEvent ) ;
118-
119- customRender ( true , editorStateTransfer ) ;
92+ customRender ( false , editorStateTransfer ) ;
93+ await waitFor ( ( ) => expect ( screen . getByRole ( 'textbox' ) ) . toHaveAttribute ( 'contentEditable' , "true" ) ) ;
12094
12195 // Then
122- expect ( initialContent ) . toBe ( mockEvent . getContent ( ) . body ) ;
96+ await waitFor ( ( ) =>
97+ expect ( screen . getByRole ( 'textbox' ) ) . toContainHTML ( mockEvent . getContent ( ) [ 'body' ] ) ) ;
12398 } ) ;
12499 } ) ;
125100
126101 describe ( 'Edit and save actions' , ( ) => {
102+ beforeEach ( async ( ) => {
103+ customRender ( ) ;
104+ await waitFor ( ( ) => expect ( screen . getByRole ( 'textbox' ) ) . toHaveAttribute ( 'contentEditable' , "true" ) ) ;
105+ } ) ;
106+
127107 const spyDispatcher = jest . spyOn ( defaultDispatcher , "dispatch" ) ;
128108 afterEach ( ( ) => {
129109 spyDispatcher . mockRestore ( ) ;
130110 } ) ;
131111
132112 it ( 'Should cancel edit on cancel button click' , async ( ) => {
133113 // When
134- customRender ( true ) ;
135- ( await screen . findByText ( 'Cancel' ) ) . click ( ) ;
114+ screen . getByText ( 'Cancel' ) . click ( ) ;
136115
137116 // Then
138117 expect ( spyDispatcher ) . toBeCalledWith ( {
@@ -149,27 +128,22 @@ describe('EditWysiwygComposer', () => {
149128 it ( 'Should send message on save button click' , async ( ) => {
150129 // When
151130 const spyDispatcher = jest . spyOn ( defaultDispatcher , "dispatch" ) ;
152-
153- const renderer = customRender ( true ) ;
154-
155- mockContent = 'my new content' ;
156- renderer . rerender ( < MatrixClientContext . Provider value = { mockClient } >
157- < RoomContext . Provider value = { defaultRoomContext } >
158- < EditWysiwygComposer editorStateTransfer = { editorStateTransfer } />
159- </ RoomContext . Provider >
160- </ MatrixClientContext . Provider > ) ;
161-
162- ( await screen . findByText ( 'Save' ) ) . click ( ) ;
131+ fireEvent . input ( screen . getByRole ( 'textbox' ) , {
132+ data : 'foo bar' ,
133+ inputType : 'insertText' ,
134+ } ) ;
135+ await waitFor ( ( ) => expect ( screen . getByText ( 'Save' ) ) . not . toHaveAttribute ( 'disabled' ) ) ;
163136
164137 // Then
138+ screen . getByText ( 'Save' ) . click ( ) ;
165139 const expectedContent = {
166- "body" : ` * ${ mockContent } ` ,
140+ "body" : ` * foo bar ` ,
167141 "format" : "org.matrix.custom.html" ,
168- "formatted_body" : ` * ${ mockContent } ` ,
142+ "formatted_body" : ` * foo bar ` ,
169143 "m.new_content" : {
170- "body" : mockContent ,
144+ "body" : "foo bar" ,
171145 "format" : "org.matrix.custom.html" ,
172- "formatted_body" : mockContent ,
146+ "formatted_body" : "foo bar" ,
173147 "msgtype" : "m.text" ,
174148 } ,
175149 "m.relates_to" : {
@@ -217,7 +191,7 @@ describe('EditWysiwygComposer', () => {
217191 } ) ;
218192
219193 // Wait for event dispatch to happen
220- await new Promise ( ( r ) => setTimeout ( r , 200 ) ) ;
194+ await flushPromises ( ) ;
221195
222196 // Then we don't get it because we are disabled
223197 expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveFocus ( ) ;
0 commit comments