@@ -19,7 +19,7 @@ limitations under the License.
19
19
*/
20
20
21
21
import { mocked } from "jest-mock" ;
22
- import { M_POLL_KIND_DISCLOSED , M_POLL_RESPONSE , M_POLL_START , PollStartEvent } from "matrix-events-sdk" ;
22
+ import { M_POLL_KIND_DISCLOSED , M_POLL_RESPONSE , M_POLL_START , Optional , PollStartEvent } from "matrix-events-sdk" ;
23
23
24
24
import * as utils from "../test-utils/test-utils" ;
25
25
import { emitPromise } from "../test-utils/test-utils" ;
@@ -188,7 +188,41 @@ describe("Room", function () {
188
188
participantUserIds : [ "@bob:example.org" ] ,
189
189
} ) ;
190
190
result . threadEvent = mkThreadResponse ( rootEvent , { ts : tsThread } ) ;
191
- thread . liveTimeline . addEvent ( result . threadEvent , true ) ;
191
+ thread . liveTimeline . addEvent ( result . threadEvent , { toStartOfTimeline : true } ) ;
192
+ }
193
+
194
+ return result ;
195
+ } ;
196
+
197
+ const addRoomThreads = (
198
+ room : Room ,
199
+ thread1EventTs : Optional < number > ,
200
+ thread2EventTs : Optional < number > ,
201
+ ) : { thread1 ?: Thread ; thread2 ?: Thread } => {
202
+ const result : { thread1 ?: Thread ; thread2 ?: Thread } = { } ;
203
+
204
+ if ( thread1EventTs !== null ) {
205
+ const { rootEvent : thread1RootEvent , thread : thread1 } = mkThread ( {
206
+ room,
207
+ client : new TestClient ( ) . client ,
208
+ authorId : "@bob:example.org" ,
209
+ participantUserIds : [ "@bob:example.org" ] ,
210
+ } ) ;
211
+ const thread1Event = mkThreadResponse ( thread1RootEvent , { ts : thread1EventTs } ) ;
212
+ thread1 . liveTimeline . addEvent ( thread1Event , { toStartOfTimeline : true } ) ;
213
+ result . thread1 = thread1 ;
214
+ }
215
+
216
+ if ( thread2EventTs !== null ) {
217
+ const { rootEvent : thread2RootEvent , thread : thread2 } = mkThread ( {
218
+ room,
219
+ client : new TestClient ( ) . client ,
220
+ authorId : "@bob:example.org" ,
221
+ participantUserIds : [ "@bob:example.org" ] ,
222
+ } ) ;
223
+ const thread2Event = mkThreadResponse ( thread2RootEvent , { ts : thread2EventTs } ) ;
224
+ thread2 . liveTimeline . addEvent ( thread2Event , { toStartOfTimeline : true } ) ;
225
+ result . thread2 = thread2 ;
192
226
}
193
227
194
228
return result ;
@@ -3556,4 +3590,35 @@ describe("Room", function () {
3556
3590
} ) ;
3557
3591
} ) ;
3558
3592
} ) ;
3593
+
3594
+ describe ( "getLastThread" , ( ) => {
3595
+ it ( "when there is no thread, it should return undefined" , ( ) => {
3596
+ expect ( room . getLastThread ( ) ) . toBeUndefined ( ) ;
3597
+ } ) ;
3598
+
3599
+ it ( "when there is only one thread, it should return this one" , ( ) => {
3600
+ const { thread1 } = addRoomThreads ( room , 23 , null ) ;
3601
+ expect ( room . getLastThread ( ) ) . toBe ( thread1 ) ;
3602
+ } ) ;
3603
+
3604
+ it ( "when there are tho threads, it should return the one with the recent event I" , ( ) => {
3605
+ const { thread2 } = addRoomThreads ( room , 23 , 42 ) ;
3606
+ expect ( room . getLastThread ( ) ) . toBe ( thread2 ) ;
3607
+ } ) ;
3608
+
3609
+ it ( "when there are tho threads, it should return the one with the recent event II" , ( ) => {
3610
+ const { thread1 } = addRoomThreads ( room , 42 , 23 ) ;
3611
+ expect ( room . getLastThread ( ) ) . toBe ( thread1 ) ;
3612
+ } ) ;
3613
+
3614
+ it ( "when there is a thread with the last event ts undefined, it should return the thread with the defined event ts" , ( ) => {
3615
+ const { thread2 } = addRoomThreads ( room , undefined , 23 ) ;
3616
+ expect ( room . getLastThread ( ) ) . toBe ( thread2 ) ;
3617
+ } ) ;
3618
+
3619
+ it ( "when the last event ts of all threads is undefined, it should return the last added thread" , ( ) => {
3620
+ const { thread2 } = addRoomThreads ( room , undefined , undefined ) ;
3621
+ expect ( room . getLastThread ( ) ) . toBe ( thread2 ) ;
3622
+ } ) ;
3623
+ } ) ;
3559
3624
} ) ;
0 commit comments