Skip to content

Commit

Permalink
[Shipping labels] Add Yosemite support for getting list of packages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bozidarsevo authored Nov 19, 2024
2 parents beff276 + 20d03dc commit e3571f9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Yosemite/Yosemite/Actions/WooShippingAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<WooShippingPackagesResponse, Error>) -> Void)
}
2 changes: 2 additions & 0 deletions Yosemite/Yosemite/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Yosemite/Yosemite/Stores/WooShippingStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -71,4 +73,9 @@ private extension WooShippingStore {
packages: packages,
completion: completion)
}

func loadPackages(siteID: Int64,
completion: @escaping (Result<WooShippingPackagesResponse, Error>) -> Void) {
remote.loadPackages(siteID: siteID, completion: completion)
}
}
44 changes: 44 additions & 0 deletions Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<WooShippingPackagesResponse, Error> = 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<WooShippingPackagesResponse, Error> = 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 {
Expand Down

0 comments on commit e3571f9

Please sign in to comment.