mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:31:27 +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,47 +8,53 @@
|
||||
|
||||
import Combine
|
||||
@testable import ElementX
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
class UserDetailsEditScreenViewModelTests: XCTestCase {
|
||||
var viewModel: UserDetailsEditScreenViewModel!
|
||||
@Suite
|
||||
struct UserDetailsEditScreenViewModelTests {
|
||||
private var viewModel: UserDetailsEditScreenViewModel!
|
||||
private var userIndicatorController: UserIndicatorControllerMock!
|
||||
|
||||
var userIndicatorController: UserIndicatorControllerMock!
|
||||
|
||||
var context: UserDetailsEditScreenViewModelType.Context {
|
||||
private var context: UserDetailsEditScreenViewModelType.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
func testCannotSaveOnLanding() {
|
||||
setupViewModel()
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
init() {
|
||||
userIndicatorController = UserIndicatorControllerMock.default
|
||||
viewModel = .init(userSession: UserSessionMock(.init()),
|
||||
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
|
||||
userIndicatorController: userIndicatorController)
|
||||
}
|
||||
|
||||
func testNameDidChange() {
|
||||
setupViewModel()
|
||||
@Test
|
||||
func cannotSaveOnLanding() {
|
||||
#expect(!context.viewState.canSave)
|
||||
}
|
||||
|
||||
@Test
|
||||
func nameDidChange() {
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.nameDidChange)
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
#expect(context.viewState.nameDidChange)
|
||||
#expect(context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testEmptyNameCannotBeSaved() {
|
||||
setupViewModel()
|
||||
@Test
|
||||
func emptyNameCannotBeSaved() {
|
||||
context.name = ""
|
||||
XCTAssertFalse(context.viewState.canSave)
|
||||
#expect(!context.viewState.canSave)
|
||||
}
|
||||
|
||||
func testAvatarPickerShowsSheet() {
|
||||
setupViewModel()
|
||||
@Test
|
||||
func avatarPickerShowsSheet() {
|
||||
context.name = "name"
|
||||
XCTAssertFalse(context.showMediaSheet)
|
||||
#expect(!context.showMediaSheet)
|
||||
context.send(viewAction: .presentMediaSource)
|
||||
XCTAssertTrue(context.showMediaSheet)
|
||||
#expect(context.showMediaSheet)
|
||||
}
|
||||
|
||||
func testSave() async throws {
|
||||
setupViewModel()
|
||||
|
||||
@Test
|
||||
func save() async throws {
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
|
||||
context.name = "name"
|
||||
@@ -57,43 +63,33 @@ class UserDetailsEditScreenViewModelTests: XCTestCase {
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndDiscard() async throws {
|
||||
setupViewModel()
|
||||
@Test
|
||||
func cancelWithChangesAndDiscard() async throws {
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
#expect(context.viewState.canSave)
|
||||
#expect(context.alertInfo == nil)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
#expect(context.alertInfo != nil)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
context.alertInfo?.secondaryButton?.action?() // Discard
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testCancelWithChangesAndSave() async throws {
|
||||
setupViewModel()
|
||||
@Test
|
||||
func cancelWithChangesAndSave() async throws {
|
||||
context.name = "name"
|
||||
XCTAssertTrue(context.viewState.canSave)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
#expect(context.viewState.canSave)
|
||||
#expect(context.alertInfo == nil)
|
||||
|
||||
context.send(viewAction: .cancel)
|
||||
|
||||
XCTAssertNotNil(context.alertInfo)
|
||||
#expect(context.alertInfo != nil)
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
|
||||
context.alertInfo?.primaryButton.action?() // Save
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func setupViewModel() {
|
||||
userIndicatorController = UserIndicatorControllerMock.default
|
||||
|
||||
viewModel = .init(userSession: UserSessionMock(.init()),
|
||||
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: ServiceLocator.shared.settings),
|
||||
userIndicatorController: userIndicatorController)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user