mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +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:
@@ -7,20 +7,21 @@
|
||||
//
|
||||
|
||||
@testable import ElementX
|
||||
import XCTest
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
class AppLockScreenViewModelTests: XCTestCase {
|
||||
var appSettings: AppSettings!
|
||||
var appLockService: AppLockService!
|
||||
var keychainController: KeychainControllerMock!
|
||||
var viewModel: AppLockScreenViewModelProtocol!
|
||||
@Suite
|
||||
final class AppLockScreenViewModelTests {
|
||||
var appSettings: AppSettings
|
||||
var appLockService: AppLockService
|
||||
var keychainController: KeychainControllerMock
|
||||
var viewModel: AppLockScreenViewModelProtocol
|
||||
|
||||
var context: AppLockScreenViewModelType.Context {
|
||||
viewModel.context
|
||||
}
|
||||
|
||||
override func setUp() {
|
||||
init() {
|
||||
AppSettings.resetAllSettings()
|
||||
appSettings = AppSettings()
|
||||
keychainController = KeychainControllerMock()
|
||||
@@ -28,11 +29,12 @@ class AppLockScreenViewModelTests: XCTestCase {
|
||||
viewModel = AppLockScreenViewModel(appLockService: appLockService)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
deinit {
|
||||
AppSettings.resetAllSettings()
|
||||
}
|
||||
|
||||
func testUnlock() async throws {
|
||||
@Test
|
||||
func unlock() async throws {
|
||||
// Given a valid PIN code.
|
||||
let pinCode = "2023"
|
||||
keychainController.pinCodeReturnValue = pinCode
|
||||
@@ -44,18 +46,19 @@ class AppLockScreenViewModelTests: XCTestCase {
|
||||
let result = try await deferred.fulfill()
|
||||
|
||||
// The app should become unlocked.
|
||||
XCTAssertEqual(result, .appUnlocked)
|
||||
#expect(result == .appUnlocked)
|
||||
}
|
||||
|
||||
func testForgotPIN() async throws {
|
||||
@Test
|
||||
func forgotPIN() async throws {
|
||||
// Given a fresh launch of the app.
|
||||
XCTAssertNil(context.alertInfo, "No alert should be shown initially.")
|
||||
#expect(context.alertInfo == nil, "No alert should be shown initially.")
|
||||
|
||||
// When the user has forgotten their PIN.
|
||||
context.send(viewAction: .forgotPIN)
|
||||
|
||||
// Then an alert should be shown before logging out.
|
||||
XCTAssertEqual(context.alertInfo?.id, .confirmResetPIN, "An alert should be shown before logging out.")
|
||||
#expect(context.alertInfo?.id == .confirmResetPIN, "An alert should be shown before logging out.")
|
||||
|
||||
// When confirming the logout.
|
||||
let deferred = deferFulfillment(viewModel.actions) { $0 == .forceLogout }
|
||||
@@ -65,14 +68,15 @@ class AppLockScreenViewModelTests: XCTestCase {
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
|
||||
func testUnlockFailure() async throws {
|
||||
@Test
|
||||
func unlockFailure() async throws {
|
||||
// Given an invalid PIN code.
|
||||
let pinCode = "2024"
|
||||
keychainController.pinCodeReturnValue = "2023"
|
||||
keychainController.containsPINCodeBiometricStateReturnValue = false
|
||||
XCTAssertEqual(context.viewState.numberOfPINAttempts, 0, "The shouldn't be any attempts yet.")
|
||||
XCTAssertFalse(context.viewState.isSubtitleWarning, "No warning should be shown yet.")
|
||||
XCTAssertNil(context.alertInfo, "No alert should be shown yet.")
|
||||
#expect(context.viewState.numberOfPINAttempts == 0, "The shouldn't be any attempts yet.")
|
||||
#expect(!context.viewState.isSubtitleWarning, "No warning should be shown yet.")
|
||||
#expect(context.alertInfo == nil, "No alert should be shown yet.")
|
||||
|
||||
// When entering it on the lock screen.
|
||||
var deferred = deferFulfillment(context.$viewState) { $0.numberOfPINAttempts == 1 }
|
||||
@@ -81,9 +85,9 @@ class AppLockScreenViewModelTests: XCTestCase {
|
||||
context.send(viewAction: .clearPINCode) // Simulate the animation completion
|
||||
|
||||
// Then a failed attempt should be shown.
|
||||
XCTAssertEqual(context.viewState.numberOfPINAttempts, 1, "A failed attempt should have been recorded.")
|
||||
XCTAssertTrue(context.viewState.isSubtitleWarning, "A warning should now be shown.")
|
||||
XCTAssertNil(context.alertInfo, "No alert should be shown yet.")
|
||||
#expect(context.viewState.numberOfPINAttempts == 1, "A failed attempt should have been recorded.")
|
||||
#expect(context.viewState.isSubtitleWarning, "A warning should now be shown.")
|
||||
#expect(context.alertInfo == nil, "No alert should be shown yet.")
|
||||
|
||||
// When entering twice more
|
||||
deferred = deferFulfillment(context.$viewState) { $0.numberOfPINAttempts == 2 }
|
||||
@@ -96,28 +100,28 @@ class AppLockScreenViewModelTests: XCTestCase {
|
||||
context.send(viewAction: .clearPINCode) // Simulate the animation completion
|
||||
|
||||
// Then an alert should be shown
|
||||
XCTAssertEqual(context.viewState.numberOfPINAttempts, 3, "All the attempts should have been recorded.")
|
||||
XCTAssertTrue(context.viewState.isSubtitleWarning, "The warning should still be shown.")
|
||||
XCTAssertEqual(context.alertInfo?.id, .forcedLogout, "An alert should now be shown.")
|
||||
#expect(context.viewState.numberOfPINAttempts == 3, "All the attempts should have been recorded.")
|
||||
#expect(context.viewState.isSubtitleWarning, "The warning should still be shown.")
|
||||
#expect(context.alertInfo?.id == .forcedLogout, "An alert should now be shown.")
|
||||
}
|
||||
|
||||
func testForceQuitRequiresLogout() async throws {
|
||||
@Test
|
||||
func forceQuitRequiresLogout() async throws {
|
||||
// Given an app with a PIN set where the user attempted to unlock 3 times.
|
||||
keychainController.pinCodeReturnValue = "2023"
|
||||
keychainController.containsPINCodeBiometricStateReturnValue = false
|
||||
appSettings.appLockNumberOfPINAttempts = 2
|
||||
XCTAssertNil(context.alertInfo)
|
||||
#expect(context.alertInfo == nil)
|
||||
let deferred = deferFulfillment(context.$viewState) { $0.numberOfPINAttempts == 3 }
|
||||
viewModel.context.pinCode = "0000"
|
||||
try await deferred.fulfill()
|
||||
XCTAssertEqual(appSettings.appLockNumberOfPINAttempts, 3, "The app should have 3 failed attempts before the force quit.")
|
||||
XCTAssertEqual(context.alertInfo?.id, .forcedLogout, "The app should be showing the alert before the force quit.")
|
||||
#expect(appSettings.appLockNumberOfPINAttempts == 3, "The app should have 3 failed attempts before the force quit.")
|
||||
#expect(context.alertInfo?.id == .forcedLogout, "The app should be showing the alert before the force quit.")
|
||||
|
||||
// When force quitting the app and relaunching.
|
||||
viewModel = nil
|
||||
let freshViewModel = AppLockScreenViewModel(appLockService: appLockService)
|
||||
|
||||
// Then the alert should remain in place
|
||||
XCTAssertEqual(freshViewModel.context.alertInfo?.id, .forcedLogout, "The new view model from the fresh launch should also show the alert")
|
||||
#expect(freshViewModel.context.alertInfo?.id == .forcedLogout, "The new view model from the fresh launch should also show the alert")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user