Skip to content

Commit 5a423f6

Browse files
Refactor unit tests to use firstIndex to find events.
1 parent c37260d commit 5a423f6

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

WooCommerce/WooCommerceTests/AppCoordinatorTests.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,12 @@ final class AppCoordinatorTests: XCTestCase {
340340
pushNotesManager.sendLocalNotificationResponse(response)
341341

342342
// Then
343-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
344-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
343+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
344+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
345+
346+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
345347
XCTAssertEqual(actionPropertyValue, "contact_support")
346-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
348+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
347349
XCTAssertEqual(typePropertyValue, "site_address_error")
348350
}
349351

@@ -364,10 +366,12 @@ final class AppCoordinatorTests: XCTestCase {
364366
pushNotesManager.sendLocalNotificationResponse(response)
365367

366368
// Then
367-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
368-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
369+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
370+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
371+
372+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
369373
XCTAssertEqual(actionPropertyValue, "login_with_wpcom")
370-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
374+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
371375
XCTAssertEqual(typePropertyValue, "site_address_error")
372376
}
373377

@@ -388,10 +392,12 @@ final class AppCoordinatorTests: XCTestCase {
388392
pushNotesManager.sendLocalNotificationResponse(response)
389393

390394
// Then
391-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationTapped.rawValue])
392-
let actionPropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["action"] as? String)
395+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationTapped.rawValue}))
396+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
397+
398+
let actionPropertyValue = try XCTUnwrap(eventProperties["action"] as? String)
393399
XCTAssertEqual(actionPropertyValue, "default")
394-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
400+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
395401
XCTAssertEqual(typePropertyValue, "site_address_error")
396402
}
397403

@@ -412,8 +418,10 @@ final class AppCoordinatorTests: XCTestCase {
412418
pushNotesManager.sendLocalNotificationResponse(response)
413419

414420
// Then
415-
XCTAssertEqual(analytics.receivedEvents, [WooAnalyticsStat.loginLocalNotificationDismissed.rawValue])
416-
let typePropertyValue = try XCTUnwrap(analytics.receivedProperties.first?["type"] as? String)
421+
let indexOfEvent = try XCTUnwrap(analytics.receivedEvents.firstIndex(where: { $0 == WooAnalyticsStat.loginLocalNotificationDismissed.rawValue}))
422+
let eventProperties = try XCTUnwrap(analytics.receivedProperties[indexOfEvent])
423+
424+
let typePropertyValue = try XCTUnwrap(eventProperties["type"] as? String)
417425
XCTAssertEqual(typePropertyValue, "site_address_error")
418426
}
419427
}

0 commit comments

Comments
 (0)