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,96 +7,108 @@
//
@testable import ElementX
import XCTest
import Testing
@MainActor
class ReportRoomScreenViewModelTests: XCTestCase {
var viewModel: ReportRoomScreenViewModelProtocol!
var roomProxy: JoinedRoomProxyMock!
@Suite
struct ReportRoomScreenViewModelTests {
private var viewModel: ReportRoomScreenViewModelProtocol
private var roomProxy: JoinedRoomProxyMock
var context: ReportRoomScreenViewModelType.Context {
private var context: ReportRoomScreenViewModelType.Context {
viewModel.context
}
override func setUp() {
init() {
roomProxy = JoinedRoomProxyMock(.init())
viewModel = ReportRoomScreenViewModel(roomProxy: roomProxy, userIndicatorController: UserIndicatorControllerMock())
}
func testInitialState() {
XCTAssertTrue(context.viewState.bindings.reason.isEmpty)
XCTAssertFalse(context.viewState.bindings.shouldLeaveRoom)
@Test
func initialState() {
#expect(context.viewState.bindings.reason.isEmpty)
#expect(!context.viewState.bindings.shouldLeaveRoom)
}
func testReportSuccess() async throws {
@Test
func reportSuccess() async throws {
let reason = "Spam"
let expectation = XCTestExpectation(description: "Report success")
roomProxy.reportRoomReasonClosure = { reasonArgument in
defer { expectation.fulfill() }
XCTAssertEqual(reasonArgument, reason)
return .success(())
try await confirmation { confirmation in
roomProxy.reportRoomReasonClosure = { reasonArgument in
#expect(reasonArgument == reason)
confirmation()
return .success(())
}
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss(shouldLeaveRoom: false)
}
context.reason = reason
context.send(viewAction: .report)
try await deferred.fulfill()
}
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss(shouldLeaveRoom: false)
}
context.reason = reason
context.send(viewAction: .report)
try await deferred.fulfill()
await fulfillment(of: [expectation])
}
func testReportAndLeaveSuccess() async throws {
@Test
func reportAndLeaveSuccess() async throws {
let reason = "Spam"
let reportExpectation = XCTestExpectation(description: "Report success")
roomProxy.reportRoomReasonClosure = { reasonArgument in
defer { reportExpectation.fulfill() }
XCTAssertEqual(reasonArgument, reason)
return .success(())
try await confirmation(expectedCount: 2) { confirmation in
roomProxy.reportRoomReasonClosure = { reasonArgument in
#expect(reasonArgument == reason)
confirmation()
return .success(())
}
roomProxy.leaveRoomClosure = {
confirmation()
return .success(())
}
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss(shouldLeaveRoom: true)
}
context.reason = reason
context.shouldLeaveRoom = true
context.send(viewAction: .report)
try await deferred.fulfill()
}
let leaveExpectation = XCTestExpectation(description: "Leave success")
roomProxy.leaveRoomClosure = {
defer { leaveExpectation.fulfill() }
return .success(())
}
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss(shouldLeaveRoom: true)
}
context.reason = reason
context.shouldLeaveRoom = true
context.send(viewAction: .report)
await fulfillment(of: [reportExpectation, leaveExpectation])
try await deferred.fulfill()
#expect(roomProxy.reportRoomReasonCalled)
#expect(roomProxy.leaveRoomCalled)
}
func testReportSuccessLeaveFails() async throws {
@Test
func reportSuccessLeaveFails() async throws {
let reason = "Spam"
let reportExpectation = XCTestExpectation(description: "Report success")
roomProxy.reportRoomReasonClosure = { reasonArgument in
defer { reportExpectation.fulfill() }
XCTAssertEqual(reasonArgument, reason)
return .success(())
try await confirmation(expectedCount: 2) { confirmation in
roomProxy.reportRoomReasonClosure = { reasonArgument in
#expect(reasonArgument == reason)
confirmation()
return .success(())
}
roomProxy.leaveRoomClosure = {
confirmation()
return .failure(.eventNotFound)
}
let deferred = deferFulfillment(context.observe(\.viewState.bindings.alert)) { $0 != nil }
context.reason = reason
context.shouldLeaveRoom = true
context.send(viewAction: .report)
try await deferred.fulfill()
}
let leaveExpectation = XCTestExpectation(description: "Leave fails")
roomProxy.leaveRoomClosure = {
defer { leaveExpectation.fulfill() }
return .failure(.eventNotFound)
}
let deferred = deferFulfillment(context.observe(\.viewState.bindings.alert)) { $0 != nil }
context.reason = reason
context.shouldLeaveRoom = true
context.send(viewAction: .report)
await fulfillment(of: [reportExpectation, leaveExpectation])
try await deferred.fulfill()
#expect(roomProxy.reportRoomReasonCalled)
#expect(roomProxy.leaveRoomCalled)
}
}