Skip to content

Commit 1ef532a

Browse files
committed
Update unit tests for account mismatch view model
1 parent 9be1c8a commit 1ef532a

File tree

3 files changed

+40
-54
lines changed

3 files changed

+40
-54
lines changed

WooCommerce/Classes/Authentication/Navigation Exceptions/WrongAccountErrorViewModel.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ final class WrongAccountErrorViewModel: ULAccountMismatchViewModel {
119119
// Fetches site info if we're not sure whether the site is self-hosted.
120120
if siteXMLRPC.isEmpty {
121121
fetchSiteInfo()
122+
} else {
123+
isSelfHostedSite = true
122124
}
123125
}
124126

@@ -344,8 +346,8 @@ private extension WrongAccountErrorViewModel {
344346
+ "Presented when logging in with a store address that does not match the account entered")
345347

346348
static let primaryButtonTitle = NSLocalizedString("Connect to the site",
347-
comment: "Action button to handle connecting the logged-in account to the site."
348-
+ "Presented when logging in with a self-hosted site that does not match the account entered")
349+
comment: "Action button to handle connecting the logged-in account to a given site."
350+
+ "Presented when logging in with a store address that does not match the account entered")
349351

350352
static let yourSite = NSLocalizedString("your site",
351353
comment: "Placeholder for site url, if the url is unknown."

WooCommerce/WooCommerceTests/Authentication/ULAccountMismatchViewControllerTests.swift

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,6 @@ final class ULAccountMismatchViewControllerTests: XCTestCase {
186186
XCTAssertTrue(viewModel.logOutButtonTapped)
187187
}
188188

189-
func test_viewcontroller_assigns_visibility_provided_by_viewmodel_to_primary_button() throws {
190-
// Given
191-
let viewModel = MismatchViewModel()
192-
let viewController = ULAccountMismatchViewController(viewModel: viewModel)
193-
194-
// When
195-
_ = try XCTUnwrap(viewController.view)
196-
let primaryButton = viewController.getPrimaryActionButton()
197-
var isHidden: Bool?
198-
viewModel.isPrimaryButtonHidden
199-
.sink { isHidden = $0 }
200-
.store(in: &subscriptions)
201-
viewModel.primaryButtonHidden = true
202-
203-
// Then
204-
XCTAssertEqual(primaryButton.isHidden, isHidden)
205-
}
206-
207189
func test_viewcontroller_assigns_visibility_provided_by_viewmodel_to_secondary_button() throws {
208190
// Given
209191
let viewModel = MismatchViewModel()
@@ -217,24 +199,6 @@ final class ULAccountMismatchViewControllerTests: XCTestCase {
217199
XCTAssertEqual(secondaryButton.isHidden, viewModel.isSecondaryButtonHidden)
218200
}
219201

220-
func test_viewcontroller_assigns_loading_state_provided_by_viewmodel_to_activity_indicator() throws {
221-
// Given
222-
let viewModel = MismatchViewModel()
223-
let viewController = ULAccountMismatchViewController(viewModel: viewModel)
224-
225-
// When
226-
_ = try XCTUnwrap(viewController.view)
227-
let activityIndicator = viewController.getActivityIndicator()
228-
var isLoading: Bool?
229-
viewModel.isShowingActivityIndicator
230-
.sink { isLoading = $0 }
231-
.store(in: &subscriptions)
232-
viewModel.showingActivityIndicator = true
233-
234-
// Then
235-
XCTAssertEqual(activityIndicator.isAnimating, isLoading)
236-
}
237-
238202
func test_viewcontroller_does_not_have_right_bar_button_item_when_rightBarButtonItemTitle_is_nil_in_viewmodel() throws {
239203
// Given
240204
let viewModel = MismatchViewModel()
@@ -282,9 +246,6 @@ final class ULAccountMismatchViewControllerTests: XCTestCase {
282246

283247

284248
private final class MismatchViewModel: ULAccountMismatchViewModel {
285-
var isPrimaryButtonHidden: AnyPublisher<Bool, Never> {
286-
Just(primaryButtonHidden).eraseToAnyPublisher()
287-
}
288249

289250
var isPrimaryButtonLoading: AnyPublisher<Bool, Never> {
290251
Just(primaryButtonLoading).eraseToAnyPublisher()
@@ -294,10 +255,6 @@ private final class MismatchViewModel: ULAccountMismatchViewModel {
294255

295256
let isSecondaryButtonHidden: Bool = false
296257

297-
var isShowingActivityIndicator: AnyPublisher<Bool, Never> {
298-
Just(showingActivityIndicator).eraseToAnyPublisher()
299-
}
300-
301258
let userEmail: String = "[email protected]"
302259

303260
var userName: String = "username"
@@ -323,8 +280,6 @@ private final class MismatchViewModel: ULAccountMismatchViewModel {
323280
var secondaryButtonTapped: Bool = false
324281
var logOutButtonTapped: Bool = false
325282
var auxiliaryButtonTapped: Bool = false
326-
var showingActivityIndicator: Bool = false
327-
var primaryButtonHidden: Bool = false
328283
var primaryButtonLoading: Bool = false
329284
var rightBarButtonItemTapped = false
330285

WooCommerce/WooCommerceTests/Authentication/WrongAccountErrorViewModelTests.swift

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import XCTest
22
import Yosemite
33
import WordPressAuthenticator
4+
import WordPressUI
45
@testable import WooCommerce
56

67
final class WrongAccountErrorViewModelTests: XCTestCase {
@@ -163,7 +164,7 @@ final class WrongAccountErrorViewModelTests: XCTestCase {
163164
XCTAssertTrue(navigationController.topViewController is AuthenticatedWebViewController)
164165
}
165166

166-
func test_fetchSiteInfo_is_triggered_if_credentials_are_not_present() throws {
167+
func test_fetchSiteInfo_is_triggered_if_credentials_are_not_present() {
167168
// Given
168169

169170
let viewModel = WrongAccountErrorViewModel(siteURL: Expectations.url,
@@ -247,7 +248,7 @@ final class WrongAccountErrorViewModelTests: XCTestCase {
247248
XCTAssertEqual(properties["is_selfhosted_site"] as? Bool, false)
248249
}
249250

250-
func test_primary_button_tap_is_tracked() throws {
251+
func test_primary_button_tap_is_tracked() {
251252
// Given
252253
let analyticsProvider = MockAnalyticsProvider()
253254
let analytics = WooAnalytics(analyticsProvider: analyticsProvider)
@@ -266,21 +267,49 @@ final class WrongAccountErrorViewModelTests: XCTestCase {
266267
XCTAssertNotNil(analyticsProvider.receivedEvents.first(where: { $0 == "login_jetpack_connect_button_tapped" }))
267268
}
268269

269-
func test_primary_button_tap_triggers_site_credential_login_if_credentials_are_not_present() throws {
270+
func test_primary_button_tap_triggers_site_credential_login_if_credentials_are_not_present_and_fetched_site_info_returns_self_hosted_site() {
270271
// Given
272+
let siteInfo = WordPressComSiteInfo(remote: ["isWordPressDotCom": false])
273+
MockAuthenticator.setMockSiteInfo(siteInfo)
271274
let viewModel = WrongAccountErrorViewModel(siteURL: Expectations.url,
272275
showsConnectedStores: false,
273276
siteCredentials: nil,
274277
authenticatorType: MockAuthenticator.self,
275278
onJetpackSetupCompletion: { _, _ in })
279+
let viewController = UIViewController()
276280

277281
// When
278-
viewModel.didTapPrimaryButton(in: UIViewController())
282+
viewModel.viewDidLoad(viewController)
283+
viewModel.didTapPrimaryButton(in: viewController)
279284

280285
// Then
281286
XCTAssertTrue(MockAuthenticator.siteCredentialLoginTriggered)
282287
}
283288

289+
func test_primary_button_tap_presents_fancy_alert_if_credentials_are_not_present_and_fetched_site_info_returns_wpcom_site() {
290+
// Given
291+
let siteInfo = WordPressComSiteInfo(remote: ["isWordPressDotCom": true])
292+
MockAuthenticator.setMockSiteInfo(siteInfo)
293+
let viewModel = WrongAccountErrorViewModel(siteURL: Expectations.url,
294+
showsConnectedStores: false,
295+
siteCredentials: nil,
296+
authenticatorType: MockAuthenticator.self,
297+
onJetpackSetupCompletion: { _, _ in })
298+
let viewController = ULAccountMismatchViewController(viewModel: viewModel)
299+
300+
// When
301+
viewModel.viewDidLoad(viewController)
302+
viewModel.didTapPrimaryButton(in: viewController)
303+
304+
// Then
305+
XCTAssertFalse(MockAuthenticator.siteCredentialLoginTriggered)
306+
307+
waitUntil { // waiting for a bit since the presentation involves animation
308+
viewController.presentedViewController != nil
309+
}
310+
XCTAssertTrue(viewController.presentedViewController is FancyAlertViewController)
311+
}
312+
284313
func test_failure_to_fetch_connection_url_is_tracked() throws {
285314
// Given
286315
let stores = MockStoresManager(sessionManager: .makeForTesting())
@@ -325,9 +354,9 @@ private extension WrongAccountErrorViewModelTests {
325354
static let url = "https://woocommerce.com"
326355
static let image = UIImage.productErrorImage
327356

328-
static let primaryButtonTitle = NSLocalizedString("Connect Jetpack",
329-
comment: "Action button to handle Jetpack connection."
330-
+ "Presented when logging in with a self-hosted site that does not match the account entered")
357+
static let primaryButtonTitle = NSLocalizedString("Connect to the site",
358+
comment: "Action button to handle connecting the logged-in account to a given site."
359+
+ "Presented when logging in with a store address that does not match the account entered")
331360

332361
static let secondaryButtonTitle = NSLocalizedString("See Connected Stores",
333362
comment: "Action button linking to a list of connected stores."

0 commit comments

Comments
 (0)