mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Finish migration of UnitTests target from XCTestCase to Swift Testing (#5129)
* Initial plan * Migrate 3 test files from XCTest to Swift Testing - MediaUploadPreviewScreenViewModelTests: @MainActor @Suite struct with init(), BundleFinder class for Bundle(for:), mutating test/setup functions, [self] capture replacing [weak self] in closures - NotificationManagerTests: @MainActor @Suite final class with init()/deinit, expectation/fulfillment(of:) replaced with confirmation(...), test_ prefix stripped - NotificationSettingsScreenViewModelTests: @MainActor @Suite struct with init() throws, non-optional stored properties, test prefix stripped Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate 3 XCTest files to Swift Testing - NotificationSettingsEditScreenViewModelTests: @MainActor @Suite struct with init() throws, mutating test methods - TimelineViewModelTests: @MainActor @Suite final class with init() async throws + deinit - AttributedStringBuilderTests: @Suite struct with init() async throws All XCT assertions replaced with #expect/#require/Issue.record Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate 4 test files from XCTest to Swift Testing - TimelineMediaPreviewViewModelTests: @Suite struct, mutating @Test funcs, testLoadingItem renamed to loadingItem (called internally by other tests) - ServerConfirmationScreenViewModelTests: @Suite final class with init()/deinit - CompletionSuggestionServiceTests: @Suite struct with init() - RoomFlowCoordinatorTests: @Suite final class with deinit Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate 4 test files from XCTest to Swift Testing - VoiceMessageRecorderTests: @Suite struct with init() async throws, added BundleFinder class for Bundle lookup, migrated all assertions - SpaceScreenViewModelTests: @Suite struct, private mutating setupViewModel, all test funcs mutating, XCTestExpectation → confirmation - RoomNotificationSettingsScreenViewModelTests: @Suite struct with init() throws, cancellable tests marked mutating - JoinRoomScreenViewModelTests: @Suite final class with init()/deinit, XCTestExpectation → confirmation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate 6 test files from XCTestCase to Swift Testing Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com> * Fix trailing blank line in RoomPollsHistoryScreenViewModelTests Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com> * Migrate 3 test files from XCTest to Swift Testing - MediaUploadingPreprocessorTests: @Suite final class with init()/deinit, removed executionTimeAllowance, XCTAssertEqual(accuracy:) → abs(Double) - SecurityAndPrivacyScreenViewModelTests: @MainActor @Suite final class, 5 expectation+fulfillment → await confirmation(...) - CreateRoomViewModelTests: @MainActor @Suite final class, 4 expectation+fulfillment → await confirmation(...) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate RoomScreenViewModelTests and RoomDetailsScreenViewModelTests to Swift Testing - Replace XCTest with Testing framework - RoomScreenViewModelTests: final class with init() async throws + deinit - RoomDetailsScreenViewModelTests: struct with init() and mutating funcs - Convert XCT assertions to #expect / Issue.record - Convert XCTestExpectation patterns to confirmation { confirm in } - Strip 'test' prefix from all test function names Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Migrate ComposerToolbarViewModelTests from XCTest to Swift Testing - Replace import XCTest with import Testing - Convert XCTestCase class to @MainActor @Suite final class - Replace setUp()/tearDown() with init()/deinit - Strip 'test' prefix from all 41 test method names and add @Test - Replace XCTAssert* with #expect()/#require() - Replace try XCTUnwrap() with try #require() - Convert expectation+wait patterns to deferFulfillment with PassthroughSubject - Convert isInverted expectation to boolean flag checked after await - Use deferFulfillment on $viewState for state-transition tests Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address comments with Copilot. * Fix the failing tests. * Fixed flaky tests (#5137) resolved flaky tests * Tweaks and fixes. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com> Co-authored-by: Doug <douglase@element.io> Co-authored-by: Mauro <34335419+Velin92@users.noreply.github.com>
This commit is contained in:
@@ -10,10 +10,11 @@ import Combine
|
||||
@testable import ElementX
|
||||
import MatrixRustSDK
|
||||
import MatrixRustSDKMocks
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@Suite
|
||||
@MainActor
|
||||
class SpaceScreenViewModelTests: XCTestCase {
|
||||
struct SpaceScreenViewModelTests {
|
||||
var spaceRoomListProxy: SpaceRoomListProxyMock!
|
||||
var spaceServiceProxy: SpaceServiceProxyMock!
|
||||
let mockSpaceRooms = [SpaceServiceRoom].mockSpaceList
|
||||
@@ -27,23 +28,25 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
func testInitialState() {
|
||||
@Test
|
||||
mutating func initialState() {
|
||||
setupViewModel()
|
||||
|
||||
XCTAssertEqual(context.viewState.paginationState, .idle)
|
||||
XCTAssertTrue(context.viewState.rooms.isEmpty)
|
||||
XCTAssertFalse(spaceRoomListProxy.paginateCalled)
|
||||
#expect(context.viewState.paginationState == .idle)
|
||||
#expect(context.viewState.rooms.isEmpty)
|
||||
#expect(!spaceRoomListProxy.paginateCalled)
|
||||
}
|
||||
|
||||
func testSinglePagination() async throws {
|
||||
@Test
|
||||
mutating func singlePagination() async throws {
|
||||
// Given a space screen view model for a space with a single paginations worth of children.
|
||||
let response = mockSpaceRooms.prefix(3)
|
||||
setupViewModel(paginationResponses: [Array(response)])
|
||||
|
||||
XCTAssertEqual(context.viewState.paginationState, .idle)
|
||||
XCTAssertTrue(context.viewState.rooms.isEmpty)
|
||||
XCTAssertFalse(spaceRoomListProxy.paginateCalled)
|
||||
XCTAssertFalse(response.isEmpty, "There should be some test rooms.")
|
||||
#expect(context.viewState.paginationState == .idle)
|
||||
#expect(context.viewState.rooms.isEmpty)
|
||||
#expect(!spaceRoomListProxy.paginateCalled)
|
||||
#expect(!response.isEmpty, "There should be some test rooms.")
|
||||
|
||||
// When the pagination is triggered.
|
||||
var deferred = deferFulfillment(spaceRoomListProxy.paginationStatePublisher) { $0 == .loading }
|
||||
@@ -51,30 +54,31 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then the screen should show a paginating indicator.
|
||||
XCTAssertEqual(context.viewState.paginationState, .paginating)
|
||||
XCTAssertEqual(spaceRoomListProxy.paginateCallsCount, 1)
|
||||
#expect(context.viewState.paginationState == .paginating)
|
||||
#expect(spaceRoomListProxy.paginateCallsCount == 1)
|
||||
|
||||
// When waiting for the pagination to finish.
|
||||
deferred = deferFulfillment(spaceRoomListProxy.paginationStatePublisher) { $0 == .idle(endReached: true) }
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then no more pagination requests should be made the the space rooms should be populated.
|
||||
XCTAssertEqual(context.viewState.paginationState, .endReached)
|
||||
XCTAssertEqual(spaceRoomListProxy.paginateCallsCount, 1)
|
||||
XCTAssertEqual(context.viewState.rooms.map(\.id), response.map(\.id))
|
||||
#expect(context.viewState.paginationState == .endReached)
|
||||
#expect(spaceRoomListProxy.paginateCallsCount == 1)
|
||||
#expect(context.viewState.rooms.map(\.id) == response.map(\.id))
|
||||
}
|
||||
|
||||
func testMultiplePaginations() async throws {
|
||||
@Test
|
||||
mutating func multiplePaginations() async throws {
|
||||
// Given a space screen view model for a space with two distinct paginations worth of children.
|
||||
let response1 = mockSpaceRooms.prefix(3)
|
||||
let response2 = mockSpaceRooms.suffix(mockSpaceRooms.count - 3)
|
||||
setupViewModel(paginationResponses: [Array(response1), Array(response2)])
|
||||
|
||||
XCTAssertEqual(context.viewState.paginationState, .idle)
|
||||
XCTAssertTrue(context.viewState.rooms.isEmpty)
|
||||
XCTAssertFalse(spaceRoomListProxy.paginateCalled)
|
||||
XCTAssertFalse(response1.isEmpty, "There should be some test rooms.")
|
||||
XCTAssertFalse(response2.isEmpty, "There should be more test rooms.")
|
||||
#expect(context.viewState.paginationState == .idle)
|
||||
#expect(context.viewState.rooms.isEmpty)
|
||||
#expect(!spaceRoomListProxy.paginateCalled)
|
||||
#expect(!response1.isEmpty, "There should be some test rooms.")
|
||||
#expect(!response2.isEmpty, "There should be more test rooms.")
|
||||
|
||||
// When the pagination is triggered.
|
||||
let deferredIsPaginating = deferFulfillment(context.observe(\.viewState.paginationState), transitionValues: [.paginating, .idle, .paginating, .endReached])
|
||||
@@ -88,15 +92,16 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
try await deferredIsPaginating.fulfill()
|
||||
try await deferredState.fulfill()
|
||||
|
||||
XCTAssertEqual(context.viewState.paginationState, .endReached)
|
||||
XCTAssertEqual(spaceRoomListProxy.paginateCallsCount, 2)
|
||||
XCTAssertEqual(context.viewState.rooms.map(\.id), mockSpaceRooms.map(\.id))
|
||||
#expect(context.viewState.paginationState == .endReached)
|
||||
#expect(spaceRoomListProxy.paginateCallsCount == 2)
|
||||
#expect(context.viewState.rooms.map(\.id) == mockSpaceRooms.map(\.id))
|
||||
}
|
||||
|
||||
func testSelectingSpace() async throws {
|
||||
@Test
|
||||
mutating func selectingSpace() async throws {
|
||||
setupViewModel()
|
||||
|
||||
let selectedSpace = try XCTUnwrap(mockSpaceRooms.first { $0.isSpace && $0.state == .joined }, "There should be a space to select.")
|
||||
let selectedSpace = try #require(mockSpaceRooms.first { $0.isSpace && $0.state == .joined }, "There should be a space to select.")
|
||||
let deferred = deferFulfillment(viewModel.actionsPublisher) { _ in true }
|
||||
viewModel.context.send(viewAction: .spaceAction(.select(selectedSpace)))
|
||||
let action = try await deferred.fulfill()
|
||||
@@ -105,14 +110,15 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
case .selectSpace(let spaceRoomListProxy) where spaceRoomListProxy.id == selectedSpace.id:
|
||||
break
|
||||
default:
|
||||
XCTFail("The action should select the space.")
|
||||
Issue.record("The action should select the space.")
|
||||
}
|
||||
}
|
||||
|
||||
func testSelectingUnjoinedSpace() async throws {
|
||||
@Test
|
||||
mutating func selectingUnjoinedSpace() async throws {
|
||||
setupViewModel()
|
||||
|
||||
let selectedSpace = try XCTUnwrap(mockSpaceRooms.first { $0.isSpace && $0.state != .joined }, "There should be a space to select.")
|
||||
let selectedSpace = try #require(mockSpaceRooms.first { $0.isSpace && $0.state != .joined }, "There should be a space to select.")
|
||||
let deferred = deferFulfillment(viewModel.actionsPublisher) { _ in true }
|
||||
viewModel.context.send(viewAction: .spaceAction(.select(selectedSpace)))
|
||||
let action = try await deferred.fulfill()
|
||||
@@ -121,14 +127,15 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
case .selectUnjoinedSpace(let spaceServiceRoom) where spaceServiceRoom.id == selectedSpace.id:
|
||||
break
|
||||
default:
|
||||
XCTFail("The action should select the space.")
|
||||
Issue.record("The action should select the space.")
|
||||
}
|
||||
}
|
||||
|
||||
func testSelectingRoom() async throws {
|
||||
@Test
|
||||
mutating func selectingRoom() async throws {
|
||||
setupViewModel()
|
||||
|
||||
let selectedRoom = try XCTUnwrap(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
let selectedRoom = try #require(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
let deferred = deferFulfillment(viewModel.actionsPublisher) { _ in true }
|
||||
viewModel.context.send(viewAction: .spaceAction(.select(selectedRoom)))
|
||||
let action = try await deferred.fulfill()
|
||||
@@ -137,106 +144,109 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
case .selectRoom(let roomID) where roomID == selectedRoom.id:
|
||||
break
|
||||
default:
|
||||
XCTFail("The action should select the room.")
|
||||
Issue.record("The action should select the room.")
|
||||
}
|
||||
}
|
||||
|
||||
func testJoiningSpace() async throws {
|
||||
@Test
|
||||
mutating func joiningSpace() async throws {
|
||||
setupViewModel()
|
||||
|
||||
let selectedSpace = try XCTUnwrap(mockSpaceRooms.first { $0.isSpace && $0.state != .joined }, "There should be a space to select.")
|
||||
let selectedSpace = try #require(mockSpaceRooms.first { $0.isSpace && $0.state != .joined }, "There should be a space to select.")
|
||||
|
||||
let expectation = XCTestExpectation(description: "Join room")
|
||||
clientProxy.joinRoomViaClosure = { _, _ in
|
||||
expectation.fulfill()
|
||||
return .success(())
|
||||
}
|
||||
let deferredState = deferFulfillment(viewModel.context.observe(\.viewState.joiningRoomIDs), transitionValues: [[selectedSpace.id], []])
|
||||
|
||||
viewModel.context.send(viewAction: .spaceAction(.join(selectedSpace)))
|
||||
|
||||
await fulfillment(of: [expectation])
|
||||
try await deferredState.fulfill()
|
||||
try await confirmation("Join room") { confirm in
|
||||
clientProxy.joinRoomViaClosure = { _, _ in
|
||||
confirm()
|
||||
return .success(())
|
||||
}
|
||||
viewModel.context.send(viewAction: .spaceAction(.join(selectedSpace)))
|
||||
try await deferredState.fulfill()
|
||||
}
|
||||
}
|
||||
|
||||
func testJoiningRoom() async throws {
|
||||
@Test
|
||||
mutating func joiningRoom() async throws {
|
||||
setupViewModel()
|
||||
|
||||
let selectedRoom = try XCTUnwrap(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
let selectedRoom = try #require(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
|
||||
let expectation = XCTestExpectation(description: "Join room")
|
||||
clientProxy.joinRoomViaClosure = { _, _ in
|
||||
expectation.fulfill()
|
||||
return .success(())
|
||||
}
|
||||
let deferredState = deferFulfillment(viewModel.context.observe(\.viewState.joiningRoomIDs), transitionValues: [[selectedRoom.id], []])
|
||||
|
||||
viewModel.context.send(viewAction: .spaceAction(.join(selectedRoom)))
|
||||
|
||||
await fulfillment(of: [expectation])
|
||||
try await deferredState.fulfill()
|
||||
try await confirmation("Join room") { confirm in
|
||||
clientProxy.joinRoomViaClosure = { _, _ in
|
||||
confirm()
|
||||
return .success(())
|
||||
}
|
||||
viewModel.context.send(viewAction: .spaceAction(.join(selectedRoom)))
|
||||
try await deferredState.fulfill()
|
||||
}
|
||||
}
|
||||
|
||||
func testManageRoomsWithoutRemoving() throws {
|
||||
@Test
|
||||
mutating func manageRoomsWithoutRemoving() throws {
|
||||
setupViewModel(initialSpaceRooms: mockSpaceRooms)
|
||||
XCTAssertEqual(context.viewState.editMode, .inactive)
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty)
|
||||
XCTAssertTrue(context.viewState.visibleRooms.contains { $0.isSpace })
|
||||
#expect(context.viewState.editMode == .inactive)
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty)
|
||||
#expect(context.viewState.visibleRooms.contains { $0.isSpace })
|
||||
|
||||
context.send(viewAction: .manageChildren)
|
||||
XCTAssertEqual(context.viewState.editMode, .transient, "Managing rooms should enable edit mode.")
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty, "No rooms should be selected to begin with.")
|
||||
XCTAssertFalse(context.viewState.visibleRooms.contains { $0.isSpace }, "Spaces should be filtered out when managing rooms.")
|
||||
#expect(context.viewState.editMode == .transient, "Managing rooms should enable edit mode.")
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty, "No rooms should be selected to begin with.")
|
||||
#expect(!context.viewState.visibleRooms.contains { $0.isSpace }, "Spaces should be filtered out when managing rooms.")
|
||||
|
||||
let selectedRoom = try XCTUnwrap(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
XCTAssertFalse(context.viewState.isSpaceIDSelected(selectedRoom.id))
|
||||
let selectedRoom = try #require(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
#expect(!context.viewState.isSpaceIDSelected(selectedRoom.id))
|
||||
context.send(viewAction: .spaceAction(.select(selectedRoom)))
|
||||
XCTAssertEqual(context.viewState.editModeSelectedIDs.count, 1, "The selected room should be included.")
|
||||
XCTAssertTrue(context.viewState.isSpaceIDSelected(selectedRoom.id), "The room should be selected.")
|
||||
#expect(context.viewState.editModeSelectedIDs.count == 1, "The selected room should be included.")
|
||||
#expect(context.viewState.isSpaceIDSelected(selectedRoom.id), "The room should be selected.")
|
||||
|
||||
context.send(viewAction: .finishManagingChildren)
|
||||
XCTAssertEqual(context.viewState.editMode, .inactive, "Cancelling should disable edit mode.")
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty, "Cancelling should clear all selected rooms.")
|
||||
XCTAssertTrue(context.viewState.visibleRooms.contains { $0.isSpace }, "Cancelling should restore the hidden spaces.")
|
||||
#expect(context.viewState.editMode == .inactive, "Cancelling should disable edit mode.")
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty, "Cancelling should clear all selected rooms.")
|
||||
#expect(context.viewState.visibleRooms.contains { $0.isSpace }, "Cancelling should restore the hidden spaces.")
|
||||
|
||||
XCTAssertFalse(spaceServiceProxy.removeChildFromCalled, "There should be no attempt to remove children when cancelling.")
|
||||
#expect(!spaceServiceProxy.removeChildFromCalled, "There should be no attempt to remove children when cancelling.")
|
||||
}
|
||||
|
||||
func testManageRoomsRemovingChildren() async throws {
|
||||
@Test
|
||||
mutating func manageRoomsRemovingChildren() async throws {
|
||||
setupViewModel(initialSpaceRooms: mockSpaceRooms)
|
||||
XCTAssertEqual(context.viewState.editMode, .inactive)
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty)
|
||||
XCTAssertTrue(context.viewState.visibleRooms.contains { $0.isSpace })
|
||||
#expect(context.viewState.editMode == .inactive)
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty)
|
||||
#expect(context.viewState.visibleRooms.contains { $0.isSpace })
|
||||
|
||||
context.send(viewAction: .manageChildren)
|
||||
XCTAssertEqual(context.viewState.editMode, .transient, "Managing rooms should enable edit mode.")
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty, "No rooms should be selected to begin with.")
|
||||
XCTAssertFalse(context.viewState.visibleRooms.contains { $0.isSpace }, "Spaces should be filtered out when managing rooms.")
|
||||
#expect(context.viewState.editMode == .transient, "Managing rooms should enable edit mode.")
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty, "No rooms should be selected to begin with.")
|
||||
#expect(!context.viewState.visibleRooms.contains { $0.isSpace }, "Spaces should be filtered out when managing rooms.")
|
||||
|
||||
let firstRoom = try XCTUnwrap(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
let lastRoom = try XCTUnwrap(mockSpaceRooms.last { !$0.isSpace }, "There should be a room to select.")
|
||||
XCTAssertNotEqual(firstRoom.id, lastRoom.id, "There should be more than one room in the list.")
|
||||
let firstRoom = try #require(mockSpaceRooms.first { !$0.isSpace }, "There should be a room to select.")
|
||||
let lastRoom = try #require(mockSpaceRooms.last { !$0.isSpace }, "There should be a room to select.")
|
||||
#expect(firstRoom.id != lastRoom.id, "There should be more than one room in the list.")
|
||||
context.send(viewAction: .spaceAction(.select(firstRoom)))
|
||||
context.send(viewAction: .spaceAction(.select(lastRoom)))
|
||||
XCTAssertEqual(context.viewState.editModeSelectedIDs.count, 2, "The selected rooms should be included.")
|
||||
#expect(context.viewState.editModeSelectedIDs.count == 2, "The selected rooms should be included.")
|
||||
|
||||
context.send(viewAction: .removeSelectedChildren)
|
||||
XCTAssertTrue(context.isPresentingRemoveChildrenConfirmation, "A confirmation prompt should be shown before removing children.")
|
||||
XCTAssertFalse(spaceServiceProxy.removeChildFromCalled, "There should be no attempt to remove children before confirming.")
|
||||
#expect(context.isPresentingRemoveChildrenConfirmation, "A confirmation prompt should be shown before removing children.")
|
||||
#expect(!spaceServiceProxy.removeChildFromCalled, "There should be no attempt to remove children before confirming.")
|
||||
|
||||
let deferred = deferFulfillment(context.observe(\.viewState.editMode)) { $0 == .inactive }
|
||||
context.send(viewAction: .confirmRemoveSelectedChildren)
|
||||
try await deferred.fulfill()
|
||||
XCTAssertFalse(context.isPresentingRemoveChildrenConfirmation, "Confirming should dismiss the confirmation prompt.")
|
||||
XCTAssertEqual(context.viewState.editMode, .inactive, "Confirming should disable edit mode when done.")
|
||||
XCTAssertTrue(context.viewState.editModeSelectedIDs.isEmpty, "Confirming should clear all selected rooms when done.")
|
||||
XCTAssertTrue(context.viewState.visibleRooms.contains { $0.isSpace }, "Confirming should restore the hidden spaces when done.")
|
||||
#expect(!context.isPresentingRemoveChildrenConfirmation, "Confirming should dismiss the confirmation prompt.")
|
||||
#expect(context.viewState.editMode == .inactive, "Confirming should disable edit mode when done.")
|
||||
#expect(context.viewState.editModeSelectedIDs.isEmpty, "Confirming should clear all selected rooms when done.")
|
||||
#expect(context.viewState.visibleRooms.contains { $0.isSpace }, "Confirming should restore the hidden spaces when done.")
|
||||
|
||||
XCTAssertEqual(spaceServiceProxy.removeChildFromCallsCount, 2, "Each selected room should have been removed.")
|
||||
XCTAssertTrue(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
|
||||
#expect(spaceServiceProxy.removeChildFromCallsCount == 2, "Each selected room should have been removed.")
|
||||
#expect(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
|
||||
}
|
||||
|
||||
func testManageRoomsRemovingChildrenWithFailure() async throws {
|
||||
@Test
|
||||
mutating func manageRoomsRemovingChildrenWithFailure() async throws {
|
||||
setupViewModel(initialSpaceRooms: mockSpaceRooms)
|
||||
|
||||
context.send(viewAction: .manageChildren)
|
||||
@@ -245,10 +255,10 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
}
|
||||
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.")
|
||||
#expect(context.viewState.editMode == .transient, "Managing rooms should enable edit mode.")
|
||||
#expect(context.viewState.visibleRooms.count == 3, "There should be 3 rooms to begin with.")
|
||||
#expect(context.viewState.editModeSelectedIDs.count == 3, "All of the visible rooms should be selected.")
|
||||
#expect(context.isPresentingRemoveChildrenConfirmation, "A confirmation prompt should be shown before removing children.")
|
||||
|
||||
let successfulIDs = context.viewState.editModeSelectedIDs.prefix(1)
|
||||
spaceServiceProxy.removeChildFromClosure = { childID, _ in
|
||||
@@ -260,54 +270,55 @@ class SpaceScreenViewModelTests: XCTestCase {
|
||||
}
|
||||
|
||||
let deferred = deferFulfillment(context.observe(\.viewState.visibleRooms.count)) { $0 == 2 }
|
||||
let deferredFailure = deferFailure(context.observe(\.viewState.editMode), timeout: 1) { $0 == .inactive }
|
||||
let deferredFailure = deferFailure(context.observe(\.viewState.editMode), timeout: .seconds(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.")
|
||||
#expect(context.viewState.editMode == .transient, "The screen should remain in edit mode.")
|
||||
#expect(context.viewState.visibleRooms.count == 2, "The removed rooms should no longer be listed for selection.")
|
||||
#expect(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.")
|
||||
#expect(spaceServiceProxy.removeChildFromCallsCount == 2, "Each selected room should have been removed.")
|
||||
#expect(!spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
|
||||
}
|
||||
|
||||
func testLeavingSpace() async throws {
|
||||
@Test
|
||||
mutating func leavingSpace() async throws {
|
||||
setupViewModel()
|
||||
XCTAssertNil(context.leaveSpaceViewModel)
|
||||
#expect(context.leaveSpaceViewModel == nil)
|
||||
|
||||
let deferredHandle = deferFulfillment(context.observe(\.leaveSpaceViewModel)) { $0 != nil }
|
||||
context.send(viewAction: .leaveSpace)
|
||||
try await deferredHandle.fulfill()
|
||||
XCTAssertNotNil(context.leaveSpaceViewModel, "The leave action should show the leave view.")
|
||||
#expect(context.leaveSpaceViewModel != nil, "The leave action should show the leave view.")
|
||||
|
||||
let leaveSpaceViewModel = try XCTUnwrap(context.leaveSpaceViewModel)
|
||||
let handle = try XCTUnwrap(context.leaveSpaceViewModel?.state.leaveHandle)
|
||||
let leaveSpaceViewModel = try #require(context.leaveSpaceViewModel)
|
||||
let handle = try #require(context.leaveSpaceViewModel?.state.leaveHandle)
|
||||
let selectedCount = handle.selectedCount
|
||||
let firstSelectedRoom = try XCTUnwrap(handle.rooms.first { $0.isSelected })
|
||||
XCTAssertGreaterThan(selectedCount, 0, "The leave view should have selected rooms to begin with")
|
||||
let firstSelectedRoom = try #require(handle.rooms.first { $0.isSelected })
|
||||
#expect(selectedCount > 0, "The leave view should have selected rooms to begin with")
|
||||
|
||||
leaveSpaceViewModel.context.send(viewAction: .deselectAll)
|
||||
XCTAssertEqual(handle.selectedCount, 0, "Deselecting all should result in no selected rooms.")
|
||||
#expect(handle.selectedCount == 0, "Deselecting all should result in no selected rooms.")
|
||||
|
||||
leaveSpaceViewModel.context.send(viewAction: .toggleRoom(roomID: firstSelectedRoom.spaceServiceRoom.id))
|
||||
XCTAssertEqual(handle.selectedCount, 1, "Toggling a room should result in 1 selected room")
|
||||
#expect(handle.selectedCount == 1, "Toggling a room should result in 1 selected room")
|
||||
|
||||
// Confirming the leave should leave the selected room and then the space.
|
||||
let deferredAction = deferFulfillment(viewModel.actionsPublisher) { $0.isLeftSpace }
|
||||
leaveSpaceViewModel.context.send(viewAction: .confirmLeaveSpace)
|
||||
try await deferredAction.fulfill()
|
||||
XCTAssertNil(context.leaveSpaceViewModel)
|
||||
XCTAssertTrue(rustLeaveHandle.leaveRoomIdsCalled)
|
||||
XCTAssertEqual(rustLeaveHandle.leaveRoomIdsReceivedRoomIds,
|
||||
[firstSelectedRoom.spaceServiceRoom.id, spaceRoomListProxy.id],
|
||||
"Confirming the leave should first leave the selected room and then the space.")
|
||||
#expect(context.leaveSpaceViewModel == nil)
|
||||
#expect(rustLeaveHandle.leaveRoomIdsCalled)
|
||||
#expect(rustLeaveHandle.leaveRoomIdsReceivedRoomIds ==
|
||||
[firstSelectedRoom.spaceServiceRoom.id, spaceRoomListProxy.id],
|
||||
"Confirming the leave should first leave the selected room and then the space.")
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func setupViewModel(initialSpaceRooms: [SpaceServiceRoom] = [], paginationResponses: [[SpaceServiceRoom]] = []) {
|
||||
private mutating func setupViewModel(initialSpaceRooms: [SpaceServiceRoom] = [], paginationResponses: [[SpaceServiceRoom]] = []) {
|
||||
spaceRoomListProxy = SpaceRoomListProxyMock(.init(spaceServiceRoom: SpaceServiceRoom.mock(isSpace: true),
|
||||
initialSpaceRooms: initialSpaceRooms,
|
||||
paginationStateSubject: paginationStateSubject,
|
||||
|
||||
Reference in New Issue
Block a user