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:
Mauro
2026-02-19 16:20:47 +01:00
committed by GitHub
parent fc03e05697
commit 7982aef855
118 changed files with 4630 additions and 4129 deletions
+25 -20
View File
@@ -7,19 +7,20 @@
//
@testable import ElementX
import XCTest
import Testing
@MainActor
class StartChatScreenViewModelTests: XCTestCase {
var viewModel: StartChatScreenViewModelProtocol!
var clientProxy: ClientProxyMock!
var userDiscoveryService: UserDiscoveryServiceMock!
@Suite
struct StartChatScreenViewModelTests {
private var viewModel: StartChatScreenViewModelProtocol!
private var clientProxy: ClientProxyMock!
private var userDiscoveryService: UserDiscoveryServiceMock!
var context: StartChatScreenViewModel.Context {
private var context: StartChatScreenViewModel.Context {
viewModel.context
}
override func setUpWithError() throws {
init() {
clientProxy = .init(.init(userID: ""))
userDiscoveryService = UserDiscoveryServiceMock()
userDiscoveryService.searchProfilesWithReturnValue = .success([])
@@ -31,21 +32,23 @@ class StartChatScreenViewModelTests: XCTestCase {
appSettings: ServiceLocator.shared.settings)
}
func testQueryShowingNoResults() async {
@Test
mutating func queryShowingNoResults() async {
await search(query: "A")
XCTAssertEqual(context.viewState.usersSection.type, .suggestions)
#expect(context.viewState.usersSection.type == .suggestions)
await search(query: "AA")
XCTAssertEqual(context.viewState.usersSection.type, .suggestions)
XCTAssertFalse(userDiscoveryService.searchProfilesWithCalled)
#expect(context.viewState.usersSection.type == .suggestions)
#expect(!userDiscoveryService.searchProfilesWithCalled)
await search(query: "AAA")
assertSearchResults(toBe: 0)
XCTAssertTrue(userDiscoveryService.searchProfilesWithCalled)
#expect(userDiscoveryService.searchProfilesWithCalled)
}
func testJoinRoomByAddress() async throws {
@Test
func joinRoomByAddress() async throws {
clientProxy.resolveRoomAliasReturnValue = .success(.init(roomId: "id", servers: []))
let deferredViewState = deferFulfillment(viewModel.context.$viewState) { viewState in
@@ -61,7 +64,8 @@ class StartChatScreenViewModelTests: XCTestCase {
try await deferredAction.fulfill()
}
func testJoinRoomByAddressFailsBecauseInvalid() async throws {
@Test
func joinRoomByAddressFailsBecauseInvalid() async throws {
let deferred = deferFulfillment(viewModel.context.$viewState) { viewState in
viewState.joinByAddressState == .invalidAddress
}
@@ -70,7 +74,8 @@ class StartChatScreenViewModelTests: XCTestCase {
try await deferred.fulfill()
}
func testJoinRoomByAddressFailsBecauseNotFound() async throws {
@Test
func joinRoomByAddressFailsBecauseNotFound() async throws {
clientProxy.resolveRoomAliasReturnValue = .failure(.failedResolvingRoomAlias)
let deferred = deferFulfillment(viewModel.context.$viewState) { viewState in
@@ -84,14 +89,14 @@ class StartChatScreenViewModelTests: XCTestCase {
// MARK: - Private
private func assertSearchResults(toBe count: Int) {
XCTAssertTrue(count >= 0)
XCTAssertEqual(context.viewState.usersSection.type, .searchResult)
XCTAssertEqual(context.viewState.usersSection.users.count, count)
XCTAssertEqual(context.viewState.hasEmptySearchResults, count == 0)
#expect(count >= 0)
#expect(context.viewState.usersSection.type == .searchResult)
#expect(context.viewState.usersSection.users.count == count)
#expect(context.viewState.hasEmptySearchResults == (count == 0))
}
@discardableResult
private func search(query: String) async -> StartChatScreenViewState? {
private mutating func search(query: String) async -> StartChatScreenViewState? {
viewModel.context.searchQuery = query
return await context.$viewState.nextValue
}