@@ -14,16 +14,28 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import RoomViewStore from "../../../../src/stores/RoomViewStore" ;
18
- import { createMessageContent } from "../../../../src/components/views/rooms/SendMessageComposer" ;
17
+ import Adapter from "enzyme-adapter-react-16" ;
18
+ import { configure , mount } from "enzyme" ;
19
+ import React from "react" ;
20
+ import { act } from "react-dom/test-utils" ;
21
+
22
+ import SendMessageComposer , { createMessageContent } from "../../../../src/components/views/rooms/SendMessageComposer" ;
23
+ import MatrixClientContext from "../../../../src/contexts/MatrixClientContext" ;
19
24
import EditorModel from "../../../../src/editor/model" ;
20
25
import { createPartCreator , createRenderer } from "../../../editor/mock" ;
26
+ import { createTestClient , mkEvent , mkStubRoom } from "../../../test-utils" ;
27
+ import BasicMessageComposer from "../../../../src/components/views/rooms/BasicMessageComposer" ;
28
+ import { MatrixClientPeg } from "../../../../src/MatrixClientPeg" ;
29
+ import { sleep } from "../../../../src/utils/promise" ;
30
+ import SpecPermalinkConstructor from "../../../../src/utils/permalinks/SpecPermalinkConstructor" ;
31
+ import defaultDispatcher from "../../../../src/dispatcher/dispatcher" ;
21
32
22
33
jest . mock ( "../../../../src/stores/RoomViewStore" ) ;
23
34
35
+ configure ( { adapter : new Adapter ( ) } ) ;
36
+
24
37
describe ( '<SendMessageComposer/>' , ( ) => {
25
38
describe ( "createMessageContent" , ( ) => {
26
- RoomViewStore . getQuotingEvent . mockReturnValue ( false ) ;
27
39
const permalinkCreator = jest . fn ( ) ;
28
40
29
41
it ( "sends plaintext messages correctly" , ( ) => {
@@ -78,6 +90,143 @@ describe('<SendMessageComposer/>', () => {
78
90
} ) ;
79
91
} ) ;
80
92
} ) ;
93
+
94
+ describe ( "functions correctly mounted" , ( ) => {
95
+ const mockClient = MatrixClientPeg . matrixClient = createTestClient ( ) ;
96
+ const mockRoom = mkStubRoom ( ) ;
97
+ const mockEvent = mkEvent ( {
98
+ type : "m.room.message" ,
99
+ content : "Replying to this" ,
100
+ event : true ,
101
+ } ) ;
102
+ mockRoom . findEventById = jest . fn ( eventId => {
103
+ return eventId === mockEvent . getId ( ) ? mockEvent : null ;
104
+ } ) ;
105
+
106
+ const spyDispatcher = jest . spyOn ( defaultDispatcher , "dispatch" ) ;
107
+
108
+ beforeEach ( ( ) => {
109
+ localStorage . clear ( ) ;
110
+ spyDispatcher . mockReset ( ) ;
111
+ } ) ;
112
+
113
+ it ( "renders text and placeholder correctly" , ( ) => {
114
+ const wrapper = mount ( < MatrixClientContext . Provider value = { mockClient } >
115
+ < SendMessageComposer
116
+ room = { mockRoom }
117
+ placeholder = "placeholder string"
118
+ permalinkCreator = { new SpecPermalinkConstructor ( ) }
119
+ />
120
+ </ MatrixClientContext . Provider > ) ;
121
+
122
+ expect ( wrapper . find ( '[aria-label="placeholder string"]' ) ) . toHaveLength ( 1 ) ;
123
+
124
+ act ( ( ) => {
125
+ wrapper . find ( BasicMessageComposer ) . instance ( ) . insertText ( "Test Text" ) ;
126
+ wrapper . update ( ) ;
127
+ } ) ;
128
+
129
+ expect ( wrapper . text ( ) ) . toBe ( "Test Text" ) ;
130
+ } ) ;
131
+
132
+ it ( "correctly persists state to and from localStorage" , ( ) => {
133
+ const wrapper = mount ( < MatrixClientContext . Provider value = { mockClient } >
134
+ < SendMessageComposer
135
+ room = { mockRoom }
136
+ placeholder = ""
137
+ permalinkCreator = { new SpecPermalinkConstructor ( ) }
138
+ replyToEvent = { mockEvent }
139
+ />
140
+ </ MatrixClientContext . Provider > ) ;
141
+
142
+ act ( ( ) => {
143
+ wrapper . find ( BasicMessageComposer ) . instance ( ) . insertText ( "Test Text" ) ;
144
+ wrapper . update ( ) ;
145
+ } ) ;
146
+
147
+ const key = wrapper . find ( SendMessageComposer ) . instance ( ) . _editorStateKey ;
148
+
149
+ expect ( wrapper . text ( ) ) . toBe ( "Test Text" ) ;
150
+ expect ( localStorage . getItem ( key ) ) . toBeNull ( ) ;
151
+
152
+ // ensure the right state was persisted to localStorage
153
+ wrapper . unmount ( ) ;
154
+ expect ( JSON . parse ( localStorage . getItem ( key ) ) ) . toStrictEqual ( {
155
+ parts : [ { "type" : "plain" , "text" : "Test Text" } ] ,
156
+ replyEventId : mockEvent . getId ( ) ,
157
+ } ) ;
158
+
159
+ // ensure the correct model is re-loaded
160
+ wrapper . mount ( ) ;
161
+ expect ( wrapper . text ( ) ) . toBe ( "Test Text" ) ;
162
+ expect ( spyDispatcher ) . toHaveBeenCalledWith ( {
163
+ action : "reply_to_event" ,
164
+ event : mockEvent ,
165
+ } ) ;
166
+
167
+ // now try with localStorage wiped out
168
+ wrapper . unmount ( ) ;
169
+ localStorage . removeItem ( key ) ;
170
+ wrapper . mount ( ) ;
171
+ expect ( wrapper . text ( ) ) . toBe ( "" ) ;
172
+ } ) ;
173
+
174
+ it ( "persists state correctly without replyToEvent onbeforeunload" , ( ) => {
175
+ const wrapper = mount ( < MatrixClientContext . Provider value = { mockClient } >
176
+ < SendMessageComposer
177
+ room = { mockRoom }
178
+ placeholder = ""
179
+ permalinkCreator = { new SpecPermalinkConstructor ( ) }
180
+ />
181
+ </ MatrixClientContext . Provider > ) ;
182
+
183
+ act ( ( ) => {
184
+ wrapper . find ( BasicMessageComposer ) . instance ( ) . insertText ( "Hello World" ) ;
185
+ wrapper . update ( ) ;
186
+ } ) ;
187
+
188
+ const key = wrapper . find ( SendMessageComposer ) . instance ( ) . _editorStateKey ;
189
+
190
+ expect ( wrapper . text ( ) ) . toBe ( "Hello World" ) ;
191
+ expect ( localStorage . getItem ( key ) ) . toBeNull ( ) ;
192
+
193
+ // ensure the right state was persisted to localStorage
194
+ window . dispatchEvent ( new Event ( 'beforeunload' ) ) ;
195
+ expect ( JSON . parse ( localStorage . getItem ( key ) ) ) . toStrictEqual ( {
196
+ parts : [ { "type" : "plain" , "text" : "Hello World" } ] ,
197
+ } ) ;
198
+ } ) ;
199
+
200
+ it ( "persists to session history upon sending" , async ( ) => {
201
+ const wrapper = mount ( < MatrixClientContext . Provider value = { mockClient } >
202
+ < SendMessageComposer
203
+ room = { mockRoom }
204
+ placeholder = "placeholder"
205
+ permalinkCreator = { new SpecPermalinkConstructor ( ) }
206
+ replyToEvent = { mockEvent }
207
+ />
208
+ </ MatrixClientContext . Provider > ) ;
209
+
210
+ act ( ( ) => {
211
+ wrapper . find ( BasicMessageComposer ) . instance ( ) . insertText ( "This is a message" ) ;
212
+ wrapper . find ( ".mx_SendMessageComposer" ) . simulate ( "keydown" , { key : "Enter" } ) ;
213
+ wrapper . update ( ) ;
214
+ } ) ;
215
+ await sleep ( 10 ) ; // await the async _sendMessage
216
+ wrapper . update ( ) ;
217
+ expect ( spyDispatcher ) . toHaveBeenCalledWith ( {
218
+ action : "reply_to_event" ,
219
+ event : null ,
220
+ } ) ;
221
+
222
+ expect ( wrapper . text ( ) ) . toBe ( "" ) ;
223
+ const str = sessionStorage . getItem ( `mx_cider_composer_history_${ mockRoom . roomId } [0]` ) ;
224
+ expect ( JSON . parse ( str ) ) . toStrictEqual ( {
225
+ parts : [ { "type" : "plain" , "text" : "This is a message" } ] ,
226
+ replyEventId : mockEvent . getId ( ) ,
227
+ } ) ;
228
+ } ) ;
229
+ } ) ;
81
230
} ) ;
82
231
83
232
0 commit comments