Make sure the selected/available rooms are updated when adding space children fails part way through. (#4997)

* Make sure the available rooms are updated if adding space children fails part way through.

* Additionally handle the case where removing rooms fails part way through.
This commit is contained in:
Doug
2026-01-23 17:36:26 +00:00
committed by GitHub
parent a3683ea996
commit 3b231339cd
6 changed files with 125 additions and 12 deletions
@@ -14,6 +14,7 @@ import XCTest
@MainActor
class SpaceAddRoomsScreenViewModelTests: XCTestCase {
var spaceRoomListProxy: SpaceRoomListProxyMock!
var spaceServiceProxy: SpaceServiceProxyMock!
var viewModel: SpaceAddRoomsScreenViewModelProtocol!
var context: SpaceAddRoomsScreenViewModelType.Context { viewModel.context }
@@ -44,15 +45,59 @@ class SpaceAddRoomsScreenViewModelTests: XCTestCase {
try await deferredAction.fulfill()
XCTAssertTrue(spaceServiceProxy.addChildToCalled, "The room should have been added to the space.")
XCTAssertTrue(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
}
func testFailureWithMultipleRoomsSelected() async throws {
// Given a view model with 4 selected rooms.
setupViewModel()
var deferred = deferFulfillment(context.observe(\.viewState.roomsSection),
message: "There should be 4 search results.") { section in
section.type == .searchResults && section.rooms.count == 4
}
context.searchQuery = "f"
context.send(viewAction: .searchQueryChanged)
try await deferred.fulfill()
for room in context.viewState.roomsSection.rooms {
context.send(viewAction: .toggleRoom(room))
}
XCTAssertEqual(context.viewState.selectedRooms.count, 4, "All of the rooms should be selected.")
// When there's a failure half way through saving.
let successfulIDs = context.viewState.roomsSection.rooms.map(\.id).prefix(2)
spaceServiceProxy.addChildToClosure = { childID, _ in
if successfulIDs.contains(childID) {
.success(())
} else {
.failure(.sdkError(SpaceServiceProxyMockError.generic))
}
}
deferred = deferFulfillment(context.observe(\.viewState.roomsSection),
message: "The search results should update.") { section in
section.type == .searchResults && section.rooms.count == 2
}
context.send(viewAction: .save)
try await deferred.fulfill()
// Then the screen should be updated to only show the rooms that still need to be added.
XCTAssertEqual(spaceServiceProxy.addChildToCallsCount, 3, "The remaining calls to the service should stop after a failure.")
XCTAssertFalse(context.viewState.selectedRooms.contains(where: { successfulIDs.contains($0.id) }),
"The added rooms should no longer show as selected.")
XCTAssertFalse(context.viewState.roomsSection.rooms.contains(where: { successfulIDs.contains($0.id) }),
"The added rooms should no longer be listed for selection.")
}
func setupViewModel() {
let summaryProvider = RoomSummaryProviderMock(.init(state: .loaded(.mockRooms)))
spaceRoomListProxy = SpaceRoomListProxyMock(.init(spaceServiceRoom: SpaceServiceRoomMock(.init(isSpace: true))))
let clientProxy = ClientProxyMock(.init())
clientProxy.recentlyVisitedRoomsFilterReturnValue = .init(repeating: JoinedRoomProxyMock(.init()), count: 5)
spaceServiceProxy = clientProxy.underlyingSpaceService as? SpaceServiceProxyMock
viewModel = SpaceAddRoomsScreenViewModel(spaceRoomListProxy: spaceRoomListProxy,
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
@@ -237,6 +237,43 @@ class SpaceScreenViewModelTests: XCTestCase {
XCTAssertTrue(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
}
func testManageRoomsRemovingChildrenWithFailure() async throws {
setupViewModel(initialSpaceRooms: mockSpaceRooms)
context.send(viewAction: .manageChildren)
for room in context.viewState.visibleRooms {
context.send(viewAction: .spaceAction(.select(room)))
}
context.send(viewAction: .removeSelectedChildren)
XCTAssertEqual(context.viewState.editMode, .transient, "Managing rooms should enable edit mode.")
XCTAssertEqual(context.viewState.visibleRooms.count, 3, "There should be 3 rooms to begin with.")
XCTAssertEqual(context.viewState.editModeSelectedIDs.count, 3, "All of the visible rooms should be selected.")
XCTAssertTrue(context.isPresentingRemoveChildrenConfirmation, "A confirmation prompt should be shown before removing children.")
let successfulIDs = context.viewState.editModeSelectedIDs.prefix(1)
spaceServiceProxy.removeChildFromClosure = { childID, _ in
if successfulIDs.contains(childID) {
.success(())
} else {
.failure(.sdkError(SpaceServiceProxyMockError.generic))
}
}
let deferred = deferFulfillment(context.observe(\.viewState.visibleRooms.count)) { $0 == 2 }
let deferredFailure = deferFailure(context.observe(\.viewState.editMode), timeout: 1) { $0 == .inactive }
context.send(viewAction: .confirmRemoveSelectedChildren)
try await deferred.fulfill()
try await deferredFailure.fulfill()
XCTAssertEqual(context.viewState.editMode, .transient, "The screen should remain in edit mode.")
XCTAssertEqual(context.viewState.visibleRooms.count, 2, "The removed rooms should no longer be listed for selection.")
XCTAssertEqual(context.viewState.editModeSelectedIDs.count, 2, "The removed rooms should no longer be selected.")
XCTAssertEqual(spaceServiceProxy.removeChildFromCallsCount, 2, "Each selected room should have been removed.")
XCTAssertFalse(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
}
func testLeavingSpace() async throws {
setupViewModel()
XCTAssertNil(context.leaveSpaceViewModel)