diff --git a/Yosemite/Yosemite/Actions/WooShippingAction.swift b/Yosemite/Yosemite/Actions/WooShippingAction.swift index 8f157b9adc4..2331aa9393c 100644 --- a/Yosemite/Yosemite/Actions/WooShippingAction.swift +++ b/Yosemite/Yosemite/Actions/WooShippingAction.swift @@ -16,4 +16,9 @@ public enum WooShippingAction: Action { destinationAddress: ShippingLabelAddress, packages: [ShippingLabelPackageSelected], completion: (Result<[ShippingLabelCarriersAndRates], Error>) -> Void) + + /// Fetch list of packages. + /// + case loadPackages(siteID: Int64, + completion: (Result) -> Void) } diff --git a/Yosemite/Yosemite/Model/Model.swift b/Yosemite/Yosemite/Model/Model.swift index e89888d8803..51f7b0bb289 100644 --- a/Yosemite/Yosemite/Model/Model.swift +++ b/Yosemite/Yosemite/Model/Model.swift @@ -179,7 +179,9 @@ public typealias TopEarnerStatsItem = Networking.TopEarnerStatsItem public typealias User = Networking.User public typealias WooAPIVersion = Networking.WooAPIVersion public typealias WooShippingCustomPackage = Networking.WooShippingCustomPackage +public typealias WooShippingPredefinedPackage = Networking.WooShippingPredefinedPackage public typealias WooShippingCreatePackageResponse = Networking.WooShippingCreatePackageResponse +public typealias WooShippingPackagesResponse = Networking.WooShippingPackagesResponse public typealias WPComPlan = Networking.WPComPlan public typealias WPComSitePlan = Networking.WPComSitePlan public typealias LoadSiteCurrentPlanError = Networking.LoadSiteCurrentPlanError diff --git a/Yosemite/Yosemite/Stores/WooShippingStore.swift b/Yosemite/Yosemite/Stores/WooShippingStore.swift index a6706e9e8b8..811dddb4e9e 100644 --- a/Yosemite/Yosemite/Stores/WooShippingStore.swift +++ b/Yosemite/Yosemite/Stores/WooShippingStore.swift @@ -39,6 +39,8 @@ public final class WooShippingStore: Store { destinationAddress: destinationAddress, packages: packages, completion: completion) + case .loadPackages(let siteID, completion: let completion): + loadPackages(siteID: siteID, completion: completion) } } } @@ -71,4 +73,9 @@ private extension WooShippingStore { packages: packages, completion: completion) } + + func loadPackages(siteID: Int64, + completion: @escaping (Result) -> Void) { + remote.loadPackages(siteID: siteID, completion: completion) + } } diff --git a/Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift b/Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift index ec7e8426162..37851784331 100644 --- a/Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift +++ b/Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift @@ -130,6 +130,50 @@ final class WooShippingStoreTests: XCTestCase { let error = try XCTUnwrap(result.failure) XCTAssertEqual(error as? NetworkError, expectedError) } + + // MARK: `loadPackages` + + func test_loadPackages_returns_success_response_with_rates() throws { + // Given + let remote = MockWooShippingRemote() + let response = WooShippingPackagesResponse.fake().copy(customPackages: [WooShippingCustomPackage.fake()]) + remote.whenLoadPackages(siteID: sampleSiteID, thenReturn: .success(response)) + let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) + + // When + let result: Result = waitFor { promise in + let action = WooShippingAction.loadPackages(siteID: self.sampleSiteID) { result in + promise(result) + } + store.onAction(action) + } + + // Then + XCTAssertTrue(result.isSuccess) + let actualResponse = try result.get() + XCTAssertEqual(actualResponse, response) + } + + func test_loadPackages_returns_error_on_failure() throws { + // Given + let remote = MockWooShippingRemote() + let expectedError = NetworkError.notFound() + remote.whenLoadPackages(siteID: sampleSiteID, thenReturn: .failure(expectedError)) + let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) + + // When + let result: Result = waitFor { promise in + let action = WooShippingAction.loadPackages(siteID: self.sampleSiteID) { result in + promise(result) + } + store.onAction(action) + } + + // Then + XCTAssertTrue(result.isFailure) + let error = try XCTUnwrap(result.failure) + XCTAssertEqual(error as? NetworkError, expectedError) + } } private extension WooShippingStoreTests {