-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shipping labels] Add Yosemite support for creating package (#14361)
- Loading branch information
Showing
7 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Networking | ||
|
||
public enum WooShippingAction: Action { | ||
/// Creates a custom package or activated a carrier package with provided package details. | ||
/// | ||
case createPackage(siteID: Int64, | ||
customPackage: WooShippingCustomPackage? = nil, | ||
predefinedOption: WooShippingPredefinedOption? = nil, | ||
completion: (Result<WooShippingCreatePackageResponse, PackageCreationError>) -> Void) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
import Networking | ||
import Storage | ||
|
||
/// Implements `WooShippingAction` actions | ||
/// | ||
public final class WooShippingStore: Store { | ||
private let remote: WooShippingRemoteProtocol | ||
|
||
public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) { | ||
self.remote = WooShippingRemote(network: network) | ||
super.init(dispatcher: dispatcher, storageManager: storageManager, network: network) | ||
} | ||
|
||
init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network, remote: WooShippingRemoteProtocol) { | ||
self.remote = remote | ||
super.init(dispatcher: dispatcher, storageManager: storageManager, network: network) | ||
} | ||
|
||
/// Registers for supported Actions. | ||
override public func registerSupportedActions(in dispatcher: Dispatcher) { | ||
dispatcher.register(processor: self, for: WooShippingAction.self) | ||
} | ||
|
||
/// Receives and executes Actions. | ||
override public func onAction(_ action: Action) { | ||
guard let action = action as? WooShippingAction else { | ||
assertionFailure("WooShippingStore received an unsupported action") | ||
return | ||
} | ||
|
||
switch action { | ||
case .createPackage(let siteID, let customPackage, let predefinedOption, let completion): | ||
createPackage(siteID: siteID, customPackage: customPackage, predefinedOption: predefinedOption, completion: completion) | ||
} | ||
} | ||
} | ||
|
||
private extension WooShippingStore { | ||
func createPackage(siteID: Int64, | ||
customPackage: WooShippingCustomPackage? = nil, | ||
predefinedOption: WooShippingPredefinedOption? = nil, | ||
completion: @escaping (Result<WooShippingCreatePackageResponse, PackageCreationError>) -> Void) { | ||
remote.createPackage(siteID: siteID, customPackage: customPackage, predefinedOption: predefinedOption) { result in | ||
switch result { | ||
case .success(let packages): | ||
completion(.success(packages)) | ||
case .failure(let error): | ||
completion(.failure(PackageCreationError(error: error))) | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Yosemite/YosemiteTests/Mocks/Networking/Remote/MockWooShippingRemote.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Networking | ||
import XCTest | ||
|
||
/// Mock for `WooShippingRemote`. | ||
/// | ||
final class MockWooShippingRemote { | ||
|
||
private struct CreatePackageResultKey: Hashable { | ||
let siteID: Int64 | ||
} | ||
|
||
/// The results to return based on the given arguments in `createPackage` | ||
private var createPackageResults = [CreatePackageResultKey: Result<WooShippingCreatePackageResponse, Error>]() | ||
|
||
/// Set the value passed to the `completion` block if `createPackage` is called. | ||
func whenCreatePackage(siteID: Int64, | ||
thenReturn result: Result<WooShippingCreatePackageResponse, Error>) { | ||
let key = CreatePackageResultKey(siteID: siteID) | ||
createPackageResults[key] = result | ||
} | ||
} | ||
|
||
// MARK: - WooShippingRemoteProtocol | ||
extension MockWooShippingRemote: WooShippingRemoteProtocol { | ||
func createPackage(siteID: Int64, | ||
customPackage: Networking.WooShippingCustomPackage?, | ||
predefinedOption: Networking.WooShippingPredefinedOption?, | ||
completion: @escaping (Result<Networking.WooShippingCreatePackageResponse, any Error>) -> Void) { | ||
DispatchQueue.main.async { [weak self] in | ||
guard let self = self else { return } | ||
|
||
let key = CreatePackageResultKey(siteID: siteID) | ||
if let result = self.createPackageResults[key] { | ||
completion(result) | ||
} else { | ||
XCTFail("\(String(describing: self)) Could not find Result for \(key)") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import XCTest | ||
@testable import Yosemite | ||
@testable import Networking | ||
|
||
final class WooShippingStoreTests: XCTestCase { | ||
|
||
/// Mock Dispatcher! | ||
/// | ||
private var dispatcher: Dispatcher! | ||
|
||
/// Mock Storage: InMemory | ||
/// | ||
private var storageManager: MockStorageManager! | ||
|
||
/// Mock Network: Allows us to inject predefined responses! | ||
/// | ||
private var network: MockNetwork! | ||
|
||
/// Testing SiteID | ||
/// | ||
private let sampleSiteID: Int64 = 123 | ||
|
||
override func setUp() { | ||
super.setUp() | ||
dispatcher = Dispatcher() | ||
storageManager = MockStorageManager() | ||
network = MockNetwork() | ||
} | ||
|
||
// MARK: `createPackage` | ||
|
||
func test_createPackage_returns_success_response() throws { | ||
// Given | ||
let remote = MockWooShippingRemote() | ||
let response = WooShippingCreatePackageResponse.fake().copy(customPackages: [WooShippingCustomPackage.fake()]) | ||
remote.whenCreatePackage(siteID: sampleSiteID, thenReturn: .success(response)) | ||
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) | ||
|
||
// When | ||
let result: Result<WooShippingCreatePackageResponse, PackageCreationError> = waitFor { promise in | ||
let action = WooShippingAction.createPackage(siteID: self.sampleSiteID, | ||
customPackage: WooShippingCustomPackage.fake(), | ||
predefinedOption: nil) { result in | ||
promise(result) | ||
} | ||
store.onAction(action) | ||
} | ||
|
||
// Then | ||
XCTAssertTrue(result.isSuccess) | ||
let actualResponse = try result.get() | ||
XCTAssertEqual(actualResponse, response) | ||
} | ||
|
||
func test_createPackage_returns_error_on_failure() throws { | ||
// Given | ||
let remote = MockWooShippingRemote() | ||
let error = DotcomError.unknown(code: "duplicate_custom_package_names_of_existing_packages", | ||
message: "At least one of the new custom packages has the same name as existing packages.") | ||
remote.whenCreatePackage(siteID: sampleSiteID, thenReturn: .failure(error)) | ||
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) | ||
|
||
// When | ||
let result: Result<WooShippingCreatePackageResponse, PackageCreationError> = waitFor { promise in | ||
let action = WooShippingAction.createPackage(siteID: self.sampleSiteID, | ||
customPackage: WooShippingCustomPackage.fake(), | ||
predefinedOption: nil) { result in | ||
promise(result) | ||
} | ||
store.onAction(action) | ||
} | ||
|
||
// Then | ||
XCTAssertTrue(result.isFailure) | ||
XCTAssertEqual(result.failure, .duplicatePackageNames) | ||
} | ||
|
||
} |