Skip to content

Commit 3a26eb2

Browse files
committed
Add test cases for POSSystemStatusService.loadWooCommercePluginAndPOSFeatureSwitch.
1 parent b11e98a commit 3a26eb2

File tree

5 files changed

+264
-2
lines changed

5 files changed

+264
-2
lines changed

Modules/Sources/Yosemite/PointOfSale/Eligibility/POSSystemStatusService.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
2929
remote = SystemStatusRemote(network: network)
3030
}
3131

32+
/// Test-friendly initializer that accepts a network implementation.
33+
init(network: Network) {
34+
remote = SystemStatusRemote(network: network)
35+
}
36+
3237
public func loadWooCommercePluginAndPOSFeatureSwitch(siteID: Int64) async throws -> POSPluginAndFeatureInfo {
3338
let mapper = SingleItemMapper<POSPluginEligibilitySystemStatus>(siteID: siteID)
3439
let systemStatus: POSPluginEligibilitySystemStatus = try await remote.loadSystemStatus(
@@ -43,7 +48,7 @@ public final class POSSystemStatusService: POSSystemStatusServiceProtocol {
4348
}
4449

4550
// Extracts POS feature value from settings response.
46-
let featureValue = systemStatus.settings.enabledFeatures.contains(SiteSettingsFeature.pointOfSale.rawValue) ? true : nil
51+
let featureValue = systemStatus.settings.enabledFeatures?.contains(SiteSettingsFeature.pointOfSale.rawValue) == true ? true : nil
4752
return POSPluginAndFeatureInfo(wcPlugin: wcPlugin, featureValue: featureValue)
4853
}
4954
}
@@ -73,7 +78,9 @@ private struct POSPluginEligibilitySystemStatus: Decodable {
7378
}
7479

7580
private struct POSEligibilitySystemStatusSettings: Decodable {
76-
let enabledFeatures: [String]
81+
// As `settings.enable_features` was introduced in WC version 9.9.0, this field is optional.
82+
// Ref: https://github.com/woocommerce/woocommerce/pull/57168
83+
let enabledFeatures: [String]?
7784

7885
enum CodingKeys: String, CodingKey {
7986
case enabledFeatures = "enabled_features"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"data": {
3+
"active_plugins":[
4+
{
5+
"plugin": "woocommerce/woocommerce.php",
6+
"name": "WooCommerce",
7+
"version": "10.0.0-dev",
8+
"version_latest": "10.0.0-dev",
9+
"url": "https://woocommerce.com/",
10+
"author_name": "Automattic",
11+
"author_url": "https://woocommerce.com",
12+
"network_activated": false
13+
}
14+
],
15+
"settings": {
16+
"api_enabled": false,
17+
"force_ssl": false,
18+
"currency": "USD",
19+
"currency_symbol": "&#36;",
20+
"currency_position": "left",
21+
"thousand_separator": ",",
22+
"decimal_separator": ".",
23+
"number_of_decimals": 2,
24+
"geolocation_enabled": false,
25+
"taxonomies": {
26+
"external": "external",
27+
"grouped": "grouped",
28+
"simple": "simple",
29+
"variable": "variable"
30+
},
31+
"product_visibility_terms": {
32+
"exclude-from-catalog": "exclude-from-catalog",
33+
"exclude-from-search": "exclude-from-search",
34+
"featured": "featured",
35+
"outofstock": "outofstock",
36+
"rated-1": "rated-1",
37+
"rated-2": "rated-2",
38+
"rated-3": "rated-3",
39+
"rated-4": "rated-4",
40+
"rated-5": "rated-5"
41+
},
42+
"woocommerce_com_connected": "no",
43+
"enforce_approved_download_dirs": true,
44+
"order_datastore": "Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\OrdersTableDataStore",
45+
"HPOS_enabled": true,
46+
"HPOS_sync_enabled": false,
47+
"enabled_features": [
48+
"analytics",
49+
"marketplace",
50+
"order_attribution",
51+
"site_visibility_badge",
52+
"remote_logging",
53+
"email_improvements",
54+
"custom_order_tables"
55+
]
56+
}
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"data": {
3+
"active_plugins":[
4+
{
5+
"plugin": "woocommerce/woocommerce.php",
6+
"name": "WooCommerce",
7+
"version": "10.0.0-dev",
8+
"version_latest": "10.0.0-dev",
9+
"url": "https://woocommerce.com/",
10+
"author_name": "Automattic",
11+
"author_url": "https://woocommerce.com",
12+
"network_activated": false
13+
}
14+
],
15+
"settings": {
16+
"api_enabled": false,
17+
"force_ssl": false,
18+
"currency": "USD",
19+
"currency_symbol": "&#36;",
20+
"currency_position": "left",
21+
"thousand_separator": ",",
22+
"decimal_separator": ".",
23+
"number_of_decimals": 2,
24+
"geolocation_enabled": false,
25+
"taxonomies": {
26+
"external": "external",
27+
"grouped": "grouped",
28+
"simple": "simple",
29+
"variable": "variable"
30+
},
31+
"product_visibility_terms": {
32+
"exclude-from-catalog": "exclude-from-catalog",
33+
"exclude-from-search": "exclude-from-search",
34+
"featured": "featured",
35+
"outofstock": "outofstock",
36+
"rated-1": "rated-1",
37+
"rated-2": "rated-2",
38+
"rated-3": "rated-3",
39+
"rated-4": "rated-4",
40+
"rated-5": "rated-5"
41+
},
42+
"woocommerce_com_connected": "no",
43+
"enforce_approved_download_dirs": true,
44+
"order_datastore": "Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\OrdersTableDataStore",
45+
"HPOS_enabled": true,
46+
"HPOS_sync_enabled": false,
47+
"enabled_features": [
48+
"analytics",
49+
"marketplace",
50+
"order_attribution",
51+
"site_visibility_badge",
52+
"remote_logging",
53+
"email_improvements",
54+
"point_of_sale",
55+
"custom_order_tables"
56+
]
57+
}
58+
}
59+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"data": {
3+
"active_plugins":[
4+
],
5+
"settings": {
6+
"api_enabled": false,
7+
"force_ssl": false,
8+
"currency": "USD",
9+
"currency_symbol": "&#36;",
10+
"currency_position": "left",
11+
"thousand_separator": ",",
12+
"decimal_separator": ".",
13+
"number_of_decimals": 2,
14+
"geolocation_enabled": false,
15+
"taxonomies": {
16+
"external": "external",
17+
"grouped": "grouped",
18+
"simple": "simple",
19+
"variable": "variable"
20+
},
21+
"product_visibility_terms": {
22+
"exclude-from-catalog": "exclude-from-catalog",
23+
"exclude-from-search": "exclude-from-search",
24+
"featured": "featured",
25+
"outofstock": "outofstock",
26+
"rated-1": "rated-1",
27+
"rated-2": "rated-2",
28+
"rated-3": "rated-3",
29+
"rated-4": "rated-4",
30+
"rated-5": "rated-5"
31+
},
32+
"woocommerce_com_connected": "no",
33+
"enforce_approved_download_dirs": true,
34+
"order_datastore": "Automattic\\WooCommerce\\Internal\\DataStores\\Orders\\OrdersTableDataStore",
35+
"HPOS_enabled": true,
36+
"HPOS_sync_enabled": false,
37+
"enabled_features": [
38+
"analytics",
39+
"marketplace",
40+
"order_attribution",
41+
"site_visibility_badge",
42+
"remote_logging",
43+
"email_improvements",
44+
"point_of_sale",
45+
"custom_order_tables"
46+
]
47+
}
48+
}
49+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Foundation
2+
import Testing
3+
import TestKit
4+
@testable import Networking
5+
@testable import Yosemite
6+
7+
@MainActor
8+
struct POSSystemStatusServiceTests {
9+
private let network = MockNetwork()
10+
private let sampleSiteID: Int64 = 134
11+
private let sut: POSSystemStatusService
12+
13+
init() async throws {
14+
network.removeAllSimulatedResponses()
15+
sut = POSSystemStatusService(network: network)
16+
}
17+
18+
// MARK: - loadWooCommercePluginAndPOSFeatureSwitch Tests
19+
20+
@Test func loadWooCommercePluginAndPOSFeatureSwitch_returns_plugin_and_nil_feature_when_settings_response_does_not_include_enabled_featuers() async throws {
21+
// Given
22+
network.simulateResponse(requestUrlSuffix: "system_status", filename: "systemStatus")
23+
24+
// When
25+
let result = try await sut.loadWooCommercePluginAndPOSFeatureSwitch(siteID: sampleSiteID)
26+
27+
// Then
28+
let plugin = try #require(result.wcPlugin)
29+
#expect(plugin.plugin == "woocommerce/woocommerce.php")
30+
#expect(plugin.name == "WooCommerce")
31+
#expect(plugin.version == "5.8.0")
32+
#expect(plugin.active == true)
33+
#expect(result.featureValue == nil)
34+
}
35+
36+
@Test func loadWooCommercePluginAndPOSFeatureSwitch_returns_plugin_and_enabled_feature_when_feature_is_enabled() async throws {
37+
// Given
38+
network.simulateResponse(requestUrlSuffix: "system_status", filename: "system-status-wc-plugin-and-pos-feature-enabled")
39+
40+
// When
41+
let result = try await sut.loadWooCommercePluginAndPOSFeatureSwitch(siteID: sampleSiteID)
42+
43+
// Then
44+
let plugin = try #require(result.wcPlugin)
45+
#expect(plugin.plugin == "woocommerce/woocommerce.php")
46+
#expect(plugin.name == "WooCommerce")
47+
#expect(plugin.version == "10.0.0-dev")
48+
#expect(plugin.active == true)
49+
#expect(result.featureValue == true)
50+
}
51+
52+
@Test func loadWooCommercePluginAndPOSFeatureSwitch_returns_plugin_and_nil_feature_when_feature_is_disabled() async throws {
53+
// Given
54+
network.simulateResponse(requestUrlSuffix: "system_status", filename: "system-status-wc-plugin-and-pos-feature-disabled")
55+
56+
// When
57+
let result = try await sut.loadWooCommercePluginAndPOSFeatureSwitch(siteID: sampleSiteID)
58+
59+
// Then
60+
let plugin = try #require(result.wcPlugin)
61+
#expect(plugin.plugin == "woocommerce/woocommerce.php")
62+
#expect(plugin.name == "WooCommerce")
63+
#expect(plugin.version == "10.0.0-dev")
64+
#expect(plugin.active == true)
65+
#expect(result.featureValue == nil)
66+
}
67+
68+
@Test func loadWooCommercePluginAndPOSFeatureSwitch_returns_nil_plugin_and_nil_feature_when_woocommerce_plugin_not_found() async throws {
69+
// Given
70+
network.simulateResponse(requestUrlSuffix: "system_status", filename: "system-status-wc-plugin-missing")
71+
72+
// When
73+
let result = try await sut.loadWooCommercePluginAndPOSFeatureSwitch(siteID: sampleSiteID)
74+
75+
// Then
76+
#expect(result.wcPlugin == nil)
77+
#expect(result.featureValue == nil)
78+
}
79+
80+
@Test func loadWooCommercePluginAndPOSFeatureSwitch_throws_error_on_network_failure() async throws {
81+
// Given
82+
network.simulateError(requestUrlSuffix: "system_status", error: NetworkError.notFound())
83+
84+
// When & Then
85+
await #expect(throws: NetworkError.self) {
86+
try await sut.loadWooCommercePluginAndPOSFeatureSwitch(siteID: sampleSiteID)
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)