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 c92e847ed7
commit 173b39a07f
118 changed files with 4630 additions and 4129 deletions

View File

@@ -7,23 +7,25 @@
//
@testable import ElementX
import XCTest
import Testing
@MainActor
class ManageRoomMemberSheetViewModelTests: XCTestCase {
@Suite
struct ManageRoomMemberSheetViewModelTests {
private var viewModel: ManageRoomMemberSheetViewModel!
private var context: ManageRoomMemberSheetViewModel.Context! {
viewModel.context
}
func testKick() async throws {
@Test
mutating func kick() async throws {
let testReason = "Kick Test"
let roomProxy = JoinedRoomProxyMock(.init(members: [RoomMemberProxyMock.mockAdmin, RoomMemberProxyMock.mockAlice]))
let expectation = XCTestExpectation(description: "Kick member")
var kickCalled = false
roomProxy.kickUserReasonClosure = { userID, reason in
defer { expectation.fulfill() }
XCTAssertEqual(userID, RoomMemberProxyMock.mockAlice.userID)
XCTAssertEqual(reason, testReason)
kickCalled = true
#expect(userID == RoomMemberProxyMock.mockAlice.userID)
#expect(reason == testReason)
return .success(())
}
@@ -43,18 +45,19 @@ class ManageRoomMemberSheetViewModelTests: XCTestCase {
context.alertInfo?.textFields?[0].text.wrappedValue = testReason
context.alertInfo?.secondaryButton?.action?()
await fulfillment(of: [expectation])
try await deferredAction.fulfill()
#expect(kickCalled)
}
func testBan() async throws {
@Test
mutating func ban() async throws {
let testReason = "Ban Test"
let roomProxy = JoinedRoomProxyMock(.init(members: [RoomMemberProxyMock.mockAdmin, RoomMemberProxyMock.mockAlice]))
let expectation = XCTestExpectation(description: "Ban member")
var banCalled = false
roomProxy.banUserReasonClosure = { userID, reason in
defer { expectation.fulfill() }
XCTAssertEqual(userID, RoomMemberProxyMock.mockAlice.userID)
XCTAssertEqual(reason, testReason)
banCalled = true
#expect(userID == RoomMemberProxyMock.mockAlice.userID)
#expect(reason == testReason)
return .success(())
}
@@ -74,11 +77,12 @@ class ManageRoomMemberSheetViewModelTests: XCTestCase {
}
context.alertInfo?.textFields?[0].text.wrappedValue = testReason
context.alertInfo?.secondaryButton?.action?()
await fulfillment(of: [expectation])
try await deferredAction.fulfill()
#expect(banCalled)
}
func testDisplayDetails() async throws {
@Test
mutating func displayDetails() async throws {
let roomProxy = JoinedRoomProxyMock(.init(members: [RoomMemberProxyMock.mockAdmin, RoomMemberProxyMock.mockAlice]))
viewModel = ManageRoomMemberSheetViewModel(memberDetails: .memberDetails(roomMember: .init(withProxy: RoomMemberProxyMock.mockAlice)),
permissions: .init(canKick: true, canBan: true, ownPowerLevel: RoomMemberProxyMock.mockAdmin.powerLevel),
@@ -92,6 +96,6 @@ class ManageRoomMemberSheetViewModelTests: XCTestCase {
}
context.send(viewAction: .displayDetails)
try await deferredAction.fulfill()
XCTAssertNil(context.alertInfo)
#expect(context.alertInfo == nil)
}
}