|
| 1 | +import XCTest |
| 2 | +import Yosemite |
| 3 | +@testable import WooCommerce |
| 4 | + |
| 5 | +final class AccountCreationFormViewModelTests: XCTestCase { |
| 6 | + private var stores: MockStoresManager! |
| 7 | + private var analyticsProvider: MockAnalyticsProvider! |
| 8 | + private var analytics: WooAnalytics! |
| 9 | + private var viewModel: AccountCreationFormViewModel! |
| 10 | + |
| 11 | + override func setUp() { |
| 12 | + super.setUp() |
| 13 | + stores = MockStoresManager(sessionManager: SessionManager.makeForTesting()) |
| 14 | + analyticsProvider = MockAnalyticsProvider() |
| 15 | + analytics = WooAnalytics(analyticsProvider: analyticsProvider) |
| 16 | + viewModel = .init(debounceDuration: 0, stores: stores, analytics: analytics) |
| 17 | + } |
| 18 | + |
| 19 | + override func tearDown() { |
| 20 | + viewModel = nil |
| 21 | + analytics = nil |
| 22 | + analyticsProvider = nil |
| 23 | + stores = nil |
| 24 | + super.tearDown() |
| 25 | + } |
| 26 | + |
| 27 | + // MARK: - `isEmailValid` |
| 28 | + |
| 29 | + func test_isEmailValid_is_false_after_entering_invalid_email() { |
| 30 | + // When |
| 31 | + viewModel.email = "notanemail@woocom" |
| 32 | + |
| 33 | + // Then |
| 34 | + waitUntil { |
| 35 | + self.viewModel.isEmailValid == false |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + func test_isEmailValid_is_true_after_entering_valid_email() { |
| 40 | + // When |
| 41 | + viewModel .email = "[email protected]" |
| 42 | + |
| 43 | + // Then |
| 44 | + waitUntil { |
| 45 | + self.viewModel.isEmailValid == true |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // MARK: - `isPasswordValid` |
| 50 | + |
| 51 | + func test_isPasswordValid_is_false_after_entering_password_less_than_minimum_length() { |
| 52 | + // When |
| 53 | + viewModel.password = "minim" |
| 54 | + |
| 55 | + // Then |
| 56 | + waitUntil { |
| 57 | + self.viewModel.isPasswordValid == false |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + func test_isPasswordValid_is_true_after_entering_password_of_minimum_length() { |
| 62 | + // When |
| 63 | + viewModel.password = "minimu" |
| 64 | + |
| 65 | + // Then |
| 66 | + waitUntil { |
| 67 | + self.viewModel.isPasswordValid == true |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: - `createAccount` |
| 72 | + |
| 73 | + func test_createAccount_success_sets_state_to_authenticated() async { |
| 74 | + // Given |
| 75 | + mockAccountCreationSuccess(result: .init(authToken: "token", username: "username")) |
| 76 | + XCTAssertFalse(stores.isAuthenticated) |
| 77 | + |
| 78 | + // When |
| 79 | + let result = await viewModel.createAccount() |
| 80 | + |
| 81 | + // Then |
| 82 | + XCTAssertTrue(result.isSuccess) |
| 83 | + XCTAssertTrue(stores.isAuthenticated) |
| 84 | + } |
| 85 | + |
| 86 | + func test_createAccount_password_failure_sets_passwordErrorMessage() async { |
| 87 | + // Given |
| 88 | + mockAccountCreationFailure(error: .invalidPassword(message: "too complex to guess")) |
| 89 | + XCTAssertFalse(stores.isAuthenticated) |
| 90 | + XCTAssertNil(viewModel.passwordErrorMessage) |
| 91 | + |
| 92 | + // When |
| 93 | + let result = await viewModel.createAccount() |
| 94 | + |
| 95 | + // Then |
| 96 | + XCTAssertTrue(result.isFailure) |
| 97 | + XCTAssertFalse(stores.isAuthenticated) |
| 98 | + XCTAssertEqual(viewModel.passwordErrorMessage, "too complex to guess") |
| 99 | + } |
| 100 | + |
| 101 | + func test_createAccount_invalidEmail_failure_sets_emailErrorMessage() async { |
| 102 | + // Given |
| 103 | + mockAccountCreationFailure(error: .invalidEmail) |
| 104 | + XCTAssertNil(viewModel.emailErrorMessage) |
| 105 | + |
| 106 | + // When |
| 107 | + let result = await viewModel.createAccount() |
| 108 | + |
| 109 | + // Then |
| 110 | + XCTAssertTrue(result.isFailure) |
| 111 | + XCTAssertNotNil(viewModel.emailErrorMessage) |
| 112 | + } |
| 113 | + |
| 114 | + func test_passwordErrorMessage_is_cleared_after_changing_password_input() async { |
| 115 | + // Given |
| 116 | + mockAccountCreationFailure(error: .invalidPassword(message: "too complex to guess")) |
| 117 | + |
| 118 | + // When |
| 119 | + let _ = await viewModel.createAccount() |
| 120 | + viewModel.password = "simple password" |
| 121 | + |
| 122 | + // Then |
| 123 | + waitUntil { |
| 124 | + self.viewModel.passwordErrorMessage == nil |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + func test_emailErrorMessage_is_cleared_after_changing_email_input() async { |
| 129 | + // Given |
| 130 | + mockAccountCreationFailure(error: .emailExists) |
| 131 | + |
| 132 | + // When |
| 133 | + let _ = await viewModel.createAccount() |
| 134 | + viewModel .email = "[email protected]" |
| 135 | + |
| 136 | + // Then |
| 137 | + waitUntil { |
| 138 | + self.viewModel.emailErrorMessage == nil |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // MARK: - analytics |
| 143 | + |
| 144 | + func test_createAccount_success_tracks_expected_events() async { |
| 145 | + // Given |
| 146 | + mockAccountCreationSuccess(result: .init(authToken: "", username: "")) |
| 147 | + |
| 148 | + // When |
| 149 | + let _ = await viewModel.createAccount() |
| 150 | + |
| 151 | + // Then |
| 152 | + XCTAssertEqual(analyticsProvider.receivedEvents, ["signup_submitted", "signup_success"]) |
| 153 | + } |
| 154 | + |
| 155 | + func test_createAccount_failure_tracks_expected_events() async { |
| 156 | + // Given |
| 157 | + mockAccountCreationFailure(error: .emailExists) |
| 158 | + |
| 159 | + // When |
| 160 | + let _ = await viewModel.createAccount() |
| 161 | + |
| 162 | + // Then |
| 163 | + XCTAssertEqual(analyticsProvider.receivedEvents, ["signup_submitted", "signup_failed"]) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +private extension AccountCreationFormViewModelTests { |
| 168 | + func mockAccountCreationSuccess(result: CreateAccountResult) { |
| 169 | + stores.whenReceivingAction(ofType: AccountCreationAction.self) { action in |
| 170 | + switch action { |
| 171 | + case let .createAccount(_, _, completion): |
| 172 | + completion(.success(result)) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + stores.whenReceivingAction(ofType: AccountAction.self) { action in |
| 177 | + switch action { |
| 178 | + case let .synchronizeAccount(completion): |
| 179 | + completion(.success(.fake())) |
| 180 | + case let .synchronizeAccountSettings(_, completion): |
| 181 | + completion(.success(.fake())) |
| 182 | + case let .synchronizeSites(_, completion): |
| 183 | + completion(.success(true)) |
| 184 | + default: |
| 185 | + break |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + func mockAccountCreationFailure(error: CreateAccountError) { |
| 191 | + stores.whenReceivingAction(ofType: AccountCreationAction.self) { action in |
| 192 | + guard case let .createAccount(_, _, completion) = action else { |
| 193 | + return |
| 194 | + } |
| 195 | + completion(.failure(error)) |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments