Skip to content

Commit fb820c8

Browse files
committed
Add unit tests for the changes in ProductSearchUICommand.
1 parent e39e421 commit fb820c8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

WooCommerce/WooCommerceTests/ViewRelated/Search/Product/ProductSearchUICommandTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ import Yosemite
55

66
final class ProductSearchUICommandTests: XCTestCase {
77
private let sampleSiteID: Int64 = 134
8+
private var analyticsProvider: MockAnalyticsProvider!
9+
private var analytics: WooAnalytics!
10+
11+
override func setUp() {
12+
super.setUp()
13+
analyticsProvider = MockAnalyticsProvider()
14+
analytics = WooAnalytics(analyticsProvider: analyticsProvider)
15+
}
16+
17+
override func tearDown() {
18+
analytics = nil
19+
analyticsProvider = nil
20+
super.tearDown()
21+
}
822

923
// MARK: - `createHeaderView`
1024

@@ -112,4 +126,49 @@ final class ProductSearchUICommandTests: XCTestCase {
112126
}
113127
XCTAssertEqual(invocationCount, 2)
114128
}
129+
130+
func test_synchronizeModels_does_not_dispatch_search_action_when_keyword_is_empty() {
131+
// Given
132+
let stores = MockStoresManager(sessionManager: .testingInstance)
133+
let command = ProductSearchUICommand(siteID: sampleSiteID, isSearchProductsBySKUEnabled: true)
134+
135+
var invocationCount = 0
136+
stores.whenReceivingAction(ofType: ProductAction.self) { action in
137+
guard case let .searchProducts(_, _, _, _, _, _, _, _, _, _, onCompletion) = action else {
138+
return XCTFail("Unexpected action: \(action)")
139+
}
140+
invocationCount += 1
141+
onCompletion(.success(()))
142+
}
143+
144+
// When
145+
waitFor { promise in
146+
command.synchronizeModels(siteID: self.sampleSiteID, keyword: "", pageNumber: 1, pageSize: 10) { _ in
147+
promise(())
148+
}
149+
}
150+
151+
// Then
152+
XCTAssertEqual(invocationCount, 0)
153+
}
154+
155+
// MARK: - Analytics
156+
157+
func test_productListSearched_is_tracked_when_synchronizing_models() throws {
158+
// Given
159+
let command = ProductSearchUICommand(siteID: sampleSiteID, analytics: analytics, isSearchProductsBySKUEnabled: true)
160+
161+
// When
162+
waitFor { promise in
163+
command.synchronizeModels(siteID: self.sampleSiteID, keyword: "coffee", pageNumber: 1, pageSize: 10) { _ in
164+
promise(())
165+
}
166+
}
167+
168+
// Then
169+
let event = try XCTUnwrap(analyticsProvider.receivedEvents.first)
170+
XCTAssertEqual(event, "product_list_searched")
171+
let eventProperties = try XCTUnwrap(analyticsProvider.receivedProperties.first)
172+
XCTAssertEqual(eventProperties["filter"] as? String, "all")
173+
}
115174
}

0 commit comments

Comments
 (0)