mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Swift Testing for Unit Tests PART 1 (#5119)
* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment more tests migration Cleaned the code manually to establish some good patterns more code improvements some more code improvements removed empty tests update project * more pr suggestions and cleanups * removed the TestSetup pattern * fixing claude not reusing tests * pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
This commit is contained in:
@@ -8,21 +8,35 @@
|
||||
|
||||
import Combine
|
||||
@testable import ElementX
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
class SpaceAddRoomsScreenViewModelTests: XCTestCase {
|
||||
var spaceRoomListProxy: SpaceRoomListProxyMock!
|
||||
var spaceServiceProxy: SpaceServiceProxyMock!
|
||||
@Suite
|
||||
struct SpaceAddRoomsScreenViewModelTests {
|
||||
var spaceRoomListProxy: SpaceRoomListProxyMock
|
||||
var spaceServiceProxy: SpaceServiceProxyMock
|
||||
var viewModel: SpaceAddRoomsScreenViewModelProtocol
|
||||
|
||||
var viewModel: SpaceAddRoomsScreenViewModelProtocol!
|
||||
var context: SpaceAddRoomsScreenViewModelType.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
func testAddingChildRoom() async throws {
|
||||
setupViewModel()
|
||||
init() {
|
||||
let summaryProvider = RoomSummaryProviderMock(.init(state: .loaded(.mockRooms)))
|
||||
spaceRoomListProxy = SpaceRoomListProxyMock(.init(spaceServiceRoom: SpaceServiceRoom.mock(isSpace: true)))
|
||||
|
||||
let clientProxy = ClientProxyMock(.init())
|
||||
clientProxy.recentlyVisitedRoomsFilterReturnValue = .init(repeating: JoinedRoomProxyMock(.init()), count: 5)
|
||||
spaceServiceProxy = clientProxy.underlyingSpaceService as? SpaceServiceProxyMock ?? SpaceServiceProxyMock(.init())
|
||||
|
||||
viewModel = SpaceAddRoomsScreenViewModel(spaceRoomListProxy: spaceRoomListProxy,
|
||||
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
|
||||
roomSummaryProvider: summaryProvider,
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}
|
||||
|
||||
@Test
|
||||
func addingChildRoom() async throws {
|
||||
var deferred = deferFulfillment(context.observe(\.viewState.roomsSection),
|
||||
message: "The screen should start with some suggestions.") { section in
|
||||
section.type == .suggestions && !section.rooms.isEmpty
|
||||
@@ -37,23 +51,22 @@ class SpaceAddRoomsScreenViewModelTests: XCTestCase {
|
||||
context.send(viewAction: .searchQueryChanged)
|
||||
try await deferred.fulfill()
|
||||
|
||||
let room = try XCTUnwrap(context.viewState.roomsSection.rooms.first)
|
||||
let room = try #require(context.viewState.roomsSection.rooms.first, "Expected a room in the section")
|
||||
context.send(viewAction: .toggleRoom(room))
|
||||
XCTAssertTrue(context.viewState.selectedRooms.contains(room), "The selected room should be shown.")
|
||||
#expect(context.viewState.selectedRooms.contains(room), "The selected room should be shown.")
|
||||
|
||||
let deferredAction = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
context.send(viewAction: .save)
|
||||
|
||||
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.")
|
||||
#expect(spaceServiceProxy.addChildToCalled, "The room should have been added to the space.")
|
||||
#expect(spaceRoomListProxy.resetCalled, "The room list should be reset to pick up the changes.")
|
||||
}
|
||||
|
||||
func testFailureWithMultipleRoomsSelected() async throws {
|
||||
@Test
|
||||
func failureWithMultipleRoomsSelected() 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
|
||||
@@ -65,7 +78,7 @@ class SpaceAddRoomsScreenViewModelTests: XCTestCase {
|
||||
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.")
|
||||
#expect(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)
|
||||
@@ -85,24 +98,10 @@ class SpaceAddRoomsScreenViewModelTests: XCTestCase {
|
||||
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 { successfulIDs.contains($0.id) },
|
||||
"The added rooms should no longer show as selected.")
|
||||
XCTAssertFalse(context.viewState.roomsSection.rooms.contains { 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: SpaceServiceRoom.mock(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)),
|
||||
roomSummaryProvider: summaryProvider,
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
#expect(spaceServiceProxy.addChildToCallsCount == 3, "The remaining calls to the service should stop after a failure.")
|
||||
#expect(!context.viewState.selectedRooms.contains { successfulIDs.contains($0.id) },
|
||||
"The added rooms should no longer show as selected.")
|
||||
#expect(!context.viewState.roomsSection.rooms.contains { successfulIDs.contains($0.id) },
|
||||
"The added rooms should no longer be listed for selection.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user