Skip to content

Commit

Permalink
fix(ui): Set RoomListLoadingState initial value
Browse files Browse the repository at this point in the history
fix(ui): Set `RoomListLoadingState` initial value
  • Loading branch information
Hywan authored Sep 18, 2023
2 parents 8032c39 + 1cf5f3a commit af402b3
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 78 deletions.
8 changes: 7 additions & 1 deletion crates/matrix-sdk-ui/src/room_list_service/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ impl RoomList {
.await
.ok_or_else(|| Error::UnknownList(sliding_sync_list_name.to_owned()))?;

let loading_state = SharedObservable::new(RoomListLoadingState::NotLoaded);
let loading_state =
SharedObservable::new(match sliding_sync_list.maximum_number_of_rooms() {
Some(maximum_number_of_rooms) => RoomListLoadingState::Loaded {
maximum_number_of_rooms: Some(maximum_number_of_rooms),
},
None => RoomListLoadingState::NotLoaded,
});

Ok(Self {
sliding_sync_list: sliding_sync_list.clone(),
Expand Down
203 changes: 126 additions & 77 deletions crates/matrix-sdk-ui/tests/integration/room_list_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,108 +1311,157 @@ async fn test_sync_resumes_from_terminated() -> Result<(), Error> {

#[async_test]
async fn test_loading_states() -> Result<(), Error> {
let (_, server, room_list) = new_room_list_service().await?;
// Test with an empty client, so no cache.
let (client, server) = {
let (client, server, room_list) = new_room_list_service().await?;

let sync = room_list.sync();
pin_mut!(sync);
let sync = room_list.sync();
pin_mut!(sync);

let all_rooms = room_list.all_rooms().await?;
let mut all_rooms_loading_state = all_rooms.loading_state();
let all_rooms = room_list.all_rooms().await?;
let mut all_rooms_loading_state = all_rooms.loading_state();

// The loading is not loaded.
assert_matches!(all_rooms_loading_state.get(), RoomListLoadingState::NotLoaded);
assert_pending!(all_rooms_loading_state);
// The loading is not loaded.
assert_matches!(all_rooms_loading_state.get(), RoomListLoadingState::NotLoaded);
assert_pending!(all_rooms_loading_state);

sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = Init => SettingUp,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 19]],
sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = Init => SettingUp,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 19]],
},
},
},
},
respond with = {
"pos": "0",
"lists": {
ALL_ROOMS: {
"count": 10,
respond with = {
"pos": "0",
"lists": {
ALL_ROOMS: {
"count": 10,
},
},
"rooms": {},
},
"rooms": {},
},
};
};

// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;
// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;

// There is a loading state update, it's loaded now!
assert_next_matches!(
all_rooms_loading_state,
RoomListLoadingState::Loaded { maximum_number_of_rooms } => {
assert_eq!(maximum_number_of_rooms, Some(10));
}
);
// There is a loading state update, it's loaded now!
assert_next_matches!(
all_rooms_loading_state,
RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(10) }
);

sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = SettingUp => Running,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 9]],
sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = SettingUp => Running,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 9]],
},
},
},
},
respond with = {
"pos": "1",
"lists": {
ALL_ROOMS: {
"count": 12, // 2 more rooms
respond with = {
"pos": "1",
"lists": {
ALL_ROOMS: {
"count": 12, // 2 more rooms
},
},
"rooms": {},
},
"rooms": {},
},
};
};

// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;
// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;

// There is a loading state update because the number of rooms has been updated.
assert_next_matches!(
all_rooms_loading_state,
RoomListLoadingState::Loaded { maximum_number_of_rooms } => {
assert_eq!(maximum_number_of_rooms, Some(12));
}
);
// There is a loading state update because the number of rooms has been updated.
assert_next_matches!(
all_rooms_loading_state,
RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) }
);

sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = Running => Running,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 11]],
sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = Running => Running,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 11]],
},
},
},
},
respond with = {
"pos": "2",
"lists": {
ALL_ROOMS: {
"count": 12, // no more rooms
respond with = {
"pos": "2",
"lists": {
ALL_ROOMS: {
"count": 12, // no more rooms
},
},
"rooms": {},
},
"rooms": {},
},
};

// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;

// No loading state update.
assert_pending!(all_rooms_loading_state);

(client, server)
};

// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;
// Now, let's try with a cache!
{
let room_list = RoomListService::new(client).await?;

let all_rooms = room_list.all_rooms().await?;
let mut all_rooms_loading_state = all_rooms.loading_state();

let sync = room_list.sync();
pin_mut!(sync);

// The loading state is loaded! Indeed, there is data loaded from the cache.
assert_matches!(
all_rooms_loading_state.get(),
RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(12) }
);
assert_pending!(all_rooms_loading_state);

sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
states = Init => SettingUp,
assert request >= {
"lists": {
ALL_ROOMS: {
"ranges": [[0, 19]],
},
},
},
respond with = {
"pos": "0",
"lists": {
ALL_ROOMS: {
"count": 13, // 1 more room
},
},
"rooms": {},
},
};

// Wait on Tokio to run all the tasks. Necessary only when testing.
yield_now().await;

// No loading state update.
assert_pending!(all_rooms_loading_state);
// The loading state has been updated.
assert_next_matches!(
all_rooms_loading_state,
RoomListLoadingState::Loaded { maximum_number_of_rooms: Some(13) }
);
}

Ok(())
}
Expand Down

0 comments on commit af402b3

Please sign in to comment.