Skip to content

Commit 5ad89b3

Browse files
committed
Add test cases for ProductsRemote.searchProductsBySKU.
1 parent 58045f3 commit 5ad89b3

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
025CA2C4238EBC4300B05C81 /* ProductShippingClassRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025CA2C3238EBC4300B05C81 /* ProductShippingClassRemote.swift */; };
3131
025CA2C6238F4F3500B05C81 /* ProductShippingClassRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025CA2C5238F4F3500B05C81 /* ProductShippingClassRemoteTests.swift */; };
3232
025CA2C8238F4FF400B05C81 /* product-shipping-classes-load-all.json in Resources */ = {isa = PBXBuildFile; fileRef = 025CA2C7238F4FF400B05C81 /* product-shipping-classes-load-all.json */; };
33+
0261F5A928D4641500B7AC72 /* products-sku-search.json in Resources */ = {isa = PBXBuildFile; fileRef = 0261F5A828D4641500B7AC72 /* products-sku-search.json */; };
3334
02698CF624C17FC1005337C4 /* product-alternative-types.json in Resources */ = {isa = PBXBuildFile; fileRef = 02698CF524C17FC1005337C4 /* product-alternative-types.json */; };
3435
02698CF824C183A5005337C4 /* ProductVariationListMapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02698CF724C183A5005337C4 /* ProductVariationListMapperTests.swift */; };
3536
02698CFA24C188E9005337C4 /* product-variations-load-all-alternative-types.json in Resources */ = {isa = PBXBuildFile; fileRef = 02698CF924C188E8005337C4 /* product-variations-load-all-alternative-types.json */; };
@@ -721,6 +722,7 @@
721722
025CA2C3238EBC4300B05C81 /* ProductShippingClassRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductShippingClassRemote.swift; sourceTree = "<group>"; };
722723
025CA2C5238F4F3500B05C81 /* ProductShippingClassRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductShippingClassRemoteTests.swift; sourceTree = "<group>"; };
723724
025CA2C7238F4FF400B05C81 /* product-shipping-classes-load-all.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-shipping-classes-load-all.json"; sourceTree = "<group>"; };
725+
0261F5A828D4641500B7AC72 /* products-sku-search.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "products-sku-search.json"; sourceTree = "<group>"; };
724726
02698CF524C17FC1005337C4 /* product-alternative-types.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-alternative-types.json"; sourceTree = "<group>"; };
725727
02698CF724C183A5005337C4 /* ProductVariationListMapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductVariationListMapperTests.swift; sourceTree = "<group>"; };
726728
02698CF924C188E8005337C4 /* product-variations-load-all-alternative-types.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "product-variations-load-all-alternative-types.json"; sourceTree = "<group>"; };
@@ -1984,6 +1986,7 @@
19841986
451274A525276C82009911FF /* product-variation.json */,
19851987
CE0A0F1E223998A00075ED8D /* products-load-all.json */,
19861988
0282DD90233A120A006A5FDB /* products-search-photo.json */,
1989+
0261F5A828D4641500B7AC72 /* products-sku-search.json */,
19871990
45D685F923D0C3CF005F87D0 /* product-search-sku.json */,
19881991
4599FC5B24A6276F0056157A /* product-tags-all.json */,
19891992
45AF57A824AB42CD0088E2F7 /* product-tags-extra.json */,
@@ -2500,6 +2503,7 @@
25002503
D865CE5F278CA183002C8520 /* stripe-payment-intent-requires-action.json in Resources */,
25012504
CCF48B2C2628AE160034EA83 /* shipping-label-account-settings.json in Resources */,
25022505
31A451D927863A2E00FE81AA /* stripe-account-live-test.json in Resources */,
2506+
0261F5A928D4641500B7AC72 /* products-sku-search.json in Resources */,
25032507
09885C8027C3FFD200910A62 /* product-variations-bulk-update.json in Resources */,
25042508
31054734262E36AB00C5C02B /* wcpay-payment-intent-error.json in Resources */,
25052509
02AF07EE27493AE700B2D81E /* media-upload-to-wordpress-site.json in Resources */,

Networking/Networking/Remote/ProductsRemote.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ public final class ProductsRemote: Remote, ProductsRemoteProtocol {
243243
enqueue(request, mapper: mapper, completion: completion)
244244
}
245245

246+
/// Retrieves all of the `Product`s that match the SKU. Partial SKU search is supported for WooCommerce version 6.6+, otherwise full SKU match is performed.
247+
/// - Parameters:
248+
/// - siteID: Site for which we'll fetch remote products
249+
/// - keyword: Search string that should be matched by the SKU (partial or full depending on the WC version).
250+
/// - pageNumber: Number of page that should be retrieved.
251+
/// - pageSize: Number of products to be retrieved per page.
252+
/// - completion: Closure to be executed upon completion.
246253
public func searchProductsBySKU(for siteID: Int64,
247254
keyword: String,
248255
pageNumber: Int,

Networking/NetworkingTests/Remote/ProductsRemoteTests.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ final class ProductsRemoteTests: XCTestCase {
395395

396396
/// Verifies that searchProducts properly relays Networking Layer errors.
397397
///
398-
func test_searchProducts_properly_relays_netwoking_errors() {
398+
func test_searchProducts_properly_relays_networking_errors() {
399399
// Given
400400
let remote = ProductsRemote(network: network)
401401

@@ -413,6 +413,47 @@ final class ProductsRemoteTests: XCTestCase {
413413
XCTAssertTrue(result.isFailure)
414414
}
415415

416+
// MARK: - Search Products by SKU
417+
418+
func test_searchProductsBySKU_properly_returns_parsed_products() throws {
419+
// Given
420+
let remote = ProductsRemote(network: network)
421+
network.simulateResponse(requestUrlSuffix: "products", filename: "products-sku-search")
422+
423+
// When
424+
let result: Result<[Product], Error> = waitFor { promise in
425+
remote.searchProductsBySKU(for: self.sampleSiteID,
426+
keyword: "choco",
427+
pageNumber: 0,
428+
pageSize: 100) { result in
429+
promise(result)
430+
}
431+
}
432+
433+
// Then
434+
XCTAssertTrue(result.isSuccess)
435+
let products = try result.get()
436+
XCTAssertEqual(products.count, 1)
437+
}
438+
439+
func test_searchProductsBySKU_properly_relays_networking_errors() {
440+
// Given
441+
let remote = ProductsRemote(network: network)
442+
443+
// When
444+
let result: Result<[Product], Error> = waitFor { promise in
445+
remote.searchProductsBySKU(for: self.sampleSiteID,
446+
keyword: String(),
447+
pageNumber: 0,
448+
pageSize: 100) { result in
449+
promise(result)
450+
}
451+
}
452+
453+
// Then
454+
XCTAssertTrue(result.isFailure)
455+
}
456+
416457

417458
// MARK: - Search Product SKU tests
418459

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"data": [
3+
{
4+
"id": 2783,
5+
"name": "Chocolate bars",
6+
"slug": "variable-chocolate-bar",
7+
"permalink": "",
8+
"date_created": "2020-06-12T22:36:02",
9+
"date_created_gmt": "2020-06-12T14:36:02",
10+
"date_modified": "2021-02-23T10:46:54",
11+
"date_modified_gmt": "2021-02-23T02:46:54",
12+
"type": "variation",
13+
"status": "publish",
14+
"featured": false,
15+
"catalog_visibility": "visible",
16+
"description": "<p>Tasty!!</p>\n",
17+
"short_description": "",
18+
"sku": "chocobars",
19+
"price": "22",
20+
"regular_price": "999",
21+
"sale_price": "22",
22+
"date_on_sale_from": null,
23+
"date_on_sale_from_gmt": null,
24+
"date_on_sale_to": null,
25+
"date_on_sale_to_gmt": null,
26+
"on_sale": true,
27+
"purchasable": true,
28+
"total_sales": "0",
29+
"virtual": false,
30+
"downloadable": false,
31+
"downloads": [],
32+
"download_limit": -1,
33+
"download_expiry": -1,
34+
"external_url": "",
35+
"button_text": "",
36+
"tax_status": "taxable",
37+
"tax_class": "reduced-rate",
38+
"manage_stock": "parent",
39+
"stock_quantity": 7774,
40+
"backorders": "no",
41+
"backorders_allowed": false,
42+
"backordered": false,
43+
"low_stock_amount": null,
44+
"sold_individually": false,
45+
"weight": "67",
46+
"dimensions": {
47+
"length": "6",
48+
"width": "",
49+
"height": "1"
50+
},
51+
"shipping_required": true,
52+
"shipping_taxable": true,
53+
"shipping_class": "10-day-shipping",
54+
"shipping_class_id": 96987521,
55+
"reviews_allowed": false,
56+
"average_rating": "0.00",
57+
"rating_count": 0,
58+
"upsell_ids": [],
59+
"cross_sell_ids": [],
60+
"parent_id": 846,
61+
"purchase_note": "",
62+
"categories": [],
63+
"tags": [],
64+
"images": [],
65+
"attributes": [
66+
{
67+
"id": 0,
68+
"name": "Darkness",
69+
"option": "87%"
70+
}
71+
],
72+
"default_attributes": [],
73+
"variations": [],
74+
"grouped_products": [],
75+
"menu_order": 6,
76+
"related_ids": [],
77+
"meta_data": [],
78+
"stock_status": "instock",
79+
"has_options": false,
80+
}
81+
]
82+
}

0 commit comments

Comments
 (0)