Skip to content

Commit ab96a69

Browse files
committed
test(sdk): Add test cases for SlidingSyncRoom::update.
Test when a `SlidingSyncRoom` has received a non-empty update, and then receives an empty-update.
1 parent 68337d5 commit ab96a69

File tree

1 file changed

+36
-7
lines changed
  • crates/matrix-sdk/src/sliding_sync

1 file changed

+36
-7
lines changed

crates/matrix-sdk/src/sliding_sync/room.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ pub enum SlidingSyncRoomState {
4040
/// It contains some information about a specific room, along with a queue of
4141
/// events for the timeline.
4242
///
43-
/// It is OK to clone this type as much as you need: cloning it is cheap.
43+
/// It is OK to clone this type as much as you need: cloning it is cheap, and
44+
/// shallow. All clones of the same value are sharing the same state.
4445
#[derive(Debug, Clone)]
4546
pub struct SlidingSyncRoom {
4647
inner: Arc<SlidingSyncRoomInner>,
@@ -398,9 +399,11 @@ mod tests {
398399
(
399400
$(
400401
$test_name:ident {
401-
$getter:ident () $( . $getter_field:ident )? = $first_value:expr;
402+
$getter:ident () $( . $getter_field:ident )? = $default_value:expr;
402403
receives $room_response:expr;
403-
_ = $second_value:expr;
404+
_ = $init_or_updated_value:expr;
405+
receives nothing;
406+
_ = $no_update_value:expr;
404407
}
405408
)+
406409
) => {
@@ -411,27 +414,35 @@ mod tests {
411414
{
412415
let room = new_room(room_id!("!foo:bar.org"), room_response!({})).await;
413416

414-
assert_eq!(room.$getter() $( . $getter_field )?, $first_value);
417+
assert_eq!(room.$getter() $( . $getter_field )?, $default_value, "default value");
415418
}
416419

417420
// Some value when initializing.
418421
{
419422
let room = new_room(room_id!("!foo:bar.org"), $room_response).await;
420423

421-
assert_eq!(room.$getter() $( . $getter_field )?, $second_value);
424+
assert_eq!(room.$getter() $( . $getter_field )?, $init_or_updated_value, "init value");
422425
}
423426

424427
// Some value when updating.
425428
{
426429

427430
let mut room = new_room(room_id!("!foo:bar.org"), room_response!({})).await;
428431

429-
assert_eq!(room.$getter() $( . $getter_field )?, $first_value);
432+
// Value is set to the default value.
433+
assert_eq!(room.$getter() $( . $getter_field )?, $default_value, "default value (bis)");
430434

431435
room.update($room_response, vec![]);
432436

433-
assert_eq!(room.$getter() $( . $getter_field )?, $second_value);
437+
// Value has been updated.
438+
assert_eq!(room.$getter() $( . $getter_field )?, $init_or_updated_value, "updated value");
439+
440+
room.update(room_response!({}), vec![]);
441+
442+
// Value is kept.
443+
assert_eq!(room.$getter() $( . $getter_field )?, $no_update_value, "not updated value");
434444
}
445+
435446
}
436447
)+
437448
};
@@ -442,42 +453,56 @@ mod tests {
442453
name() = None;
443454
receives room_response!({"name": "gordon"});
444455
_ = Some("gordon".to_string());
456+
receives nothing;
457+
_ = Some("gordon".to_string());
445458
}
446459

447460
test_room_is_dm {
448461
is_dm() = None;
449462
receives room_response!({"is_dm": true});
450463
_ = Some(true);
464+
receives nothing;
465+
_ = Some(true);
451466
}
452467

453468
test_room_is_initial_response {
454469
is_initial_response() = None;
455470
receives room_response!({"initial": true});
456471
_ = Some(true);
472+
receives nothing;
473+
_ = Some(true);
457474
}
458475

459476
test_has_unread_notifications_with_notification_count {
460477
has_unread_notifications() = false;
461478
receives room_response!({"notification_count": 42});
462479
_ = true;
480+
receives nothing;
481+
_ = false;
463482
}
464483

465484
test_has_unread_notifications_with_highlight_count {
466485
has_unread_notifications() = false;
467486
receives room_response!({"highlight_count": 42});
468487
_ = true;
488+
receives nothing;
489+
_ = false;
469490
}
470491

471492
test_unread_notifications_with_notification_count {
472493
unread_notifications().notification_count = None;
473494
receives room_response!({"notification_count": 42});
474495
_ = Some(uint!(42));
496+
receives nothing;
497+
_ = None;
475498
}
476499

477500
test_unread_notifications_with_highlight_count {
478501
unread_notifications().highlight_count = None;
479502
receives room_response!({"highlight_count": 42});
480503
_ = Some(uint!(42));
504+
receives nothing;
505+
_ = None;
481506
}
482507
}
483508

@@ -506,7 +531,9 @@ mod tests {
506531
assert_eq!(room.inner.prev_batch(), None);
507532

508533
room.update(room_response!({"prev_batch": "t111_222_333"}), vec![]);
534+
assert_eq!(room.inner.prev_batch(), Some("t111_222_333".to_string()));
509535

536+
room.update(room_response!({}), vec![]);
510537
assert_eq!(room.inner.prev_batch(), Some("t111_222_333".to_string()));
511538
}
512539
}
@@ -563,7 +590,9 @@ mod tests {
563590
}),
564591
vec![],
565592
);
593+
assert!(!room.required_state().is_empty());
566594

595+
room.update(room_response!({}), vec![]);
567596
assert!(!room.required_state().is_empty());
568597
}
569598
}

0 commit comments

Comments
 (0)