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
@@ -7,18 +7,19 @@
//
@testable import ElementX
import XCTest
import Testing
@MainActor
class DeclineAndBlockScreenViewModelTests: XCTestCase {
var viewModel: DeclineAndBlockScreenViewModelProtocol!
var clientProxy: ClientProxyMock!
@Suite
struct DeclineAndBlockScreenViewModelTests {
var viewModel: DeclineAndBlockScreenViewModelProtocol
var clientProxy: ClientProxyMock
var context: DeclineAndBlockScreenViewModelType.Context {
viewModel.context
}
override func setUp() {
init() {
clientProxy = ClientProxyMock(.init())
viewModel = DeclineAndBlockScreenViewModel(userID: "@alice:matrix.org",
roomID: "!room:matrix.org",
@@ -26,39 +27,42 @@ class DeclineAndBlockScreenViewModelTests: XCTestCase {
userIndicatorController: UserIndicatorControllerMock())
}
func testInitialState() {
XCTAssertFalse(context.viewState.isDeclineDisabled)
XCTAssertFalse(context.shouldReport)
XCTAssertTrue(context.shouldBlockUser)
@Test
func initialState() {
#expect(!context.viewState.isDeclineDisabled)
#expect(!context.shouldReport)
#expect(context.shouldBlockUser)
}
func testDeclineDisabled() {
@Test
mutating func declineDisabled() {
context.shouldBlockUser = false
XCTAssertTrue(context.viewState.isDeclineDisabled)
XCTAssertFalse(context.shouldReport)
XCTAssertFalse(context.shouldBlockUser)
#expect(context.viewState.isDeclineDisabled)
#expect(!context.shouldReport)
#expect(!context.shouldBlockUser)
context.shouldReport = true
// Should report set to `true` always requires a non empty reason
XCTAssertTrue(context.viewState.isDeclineDisabled)
#expect(context.viewState.isDeclineDisabled)
context.reportReason = "Test reason"
XCTAssertFalse(context.viewState.isDeclineDisabled)
#expect(!context.viewState.isDeclineDisabled)
}
func testDeclineBlockAndReport() async throws {
@Test
mutating func declineBlockAndReport() async throws {
let reason = "Test reason"
clientProxy.roomForIdentifierClosure = { id in
XCTAssertEqual(id, "!room:matrix.org")
#expect(id == "!room:matrix.org")
let roomProxyMock = InvitedRoomProxyMock(.init(id: id))
roomProxyMock.rejectInvitationReturnValue = .success(())
return .invited(InvitedRoomProxyMock(.init(id: id)))
}
clientProxy.reportRoomForIdentifierReasonClosure = { id, reasonValue in
XCTAssertEqual(id, "!room:matrix.org")
XCTAssertEqual(reasonValue, reason)
#expect(id == "!room:matrix.org")
#expect(reasonValue == reason)
return .success(())
}
clientProxy.ignoreUserClosure = { userId in
XCTAssertEqual(userId, "@alice:matrix.org")
#expect(userId == "@alice:matrix.org")
return .success(())
}
@@ -70,8 +74,8 @@ class DeclineAndBlockScreenViewModelTests: XCTestCase {
}
context.send(viewAction: .decline)
try await deferredAction.fulfill()
XCTAssertTrue(clientProxy.roomForIdentifierCalled)
XCTAssertTrue(clientProxy.reportRoomForIdentifierReasonCalled)
XCTAssertTrue(clientProxy.ignoreUserCalled)
#expect(clientProxy.roomForIdentifierCalled)
#expect(clientProxy.reportRoomForIdentifierReasonCalled)
#expect(clientProxy.ignoreUserCalled)
}
}