Skip to content

Commit

Permalink
Add Woo Shipping action to print a shipping label
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelmcr committed Nov 27, 2024
1 parent e2d54bc commit f6eba59
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Yosemite/Yosemite/Actions/WooShippingAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ public enum WooShippingAction: Action {
pollingDelay: TimeInterval = 1.0,
pollingMaximumRetries: Int64 = 3,
completion: (Result<ShippingLabel, Error>) -> Void)

/// Generates a shipping label document for printing.
///
case printLabel(siteID: Int64,
labelIDs: [Int64],
paperSize: ShippingLabelPaperSize,
completion: (Result<ShippingLabelPrintData, Error>) -> Void)
}
9 changes: 9 additions & 0 deletions Yosemite/Yosemite/Stores/WooShippingStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public final class WooShippingStore: Store {
pollingDelay: pollingDelay,
pollingMaximumRetries: pollingMaximumRetries,
completion: completion)
case let .printLabel(siteID, labelIDs, paperSize, completion):
printLabel(siteID: siteID, labelIDs: labelIDs, paperSize: paperSize, completion: completion)
}
}
}
Expand Down Expand Up @@ -146,6 +148,13 @@ private extension WooShippingStore {
}
}
}

func printLabel(siteID: Int64,
labelIDs: [Int64],
paperSize: ShippingLabelPaperSize,
completion: @escaping (Result<ShippingLabelPrintData, Error>) -> Void) {
remote.printLabel(siteID: siteID, labelIDs: labelIDs, paperSize: paperSize, completion: completion)
}
}

// MARK: Helpers
Expand Down
50 changes: 50 additions & 0 deletions Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,56 @@ final class WooShippingStoreTests: XCTestCase {
XCTAssertTrue(remote.purchaseShippingLabelCalled)
XCTAssertGreaterThan(remote.checkLabelStatusCallsCount, 1)
}

func test_printLabel_returns_print_data_on_success() throws {
// Given
let expectedPrintData = ShippingLabelPrintData.fake()
let remote = MockWooShippingRemote()
remote.whenPrintLabel(siteID: sampleSiteID, thenReturn: .success(expectedPrintData))
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

// When
let printData: ShippingLabelPrintData = waitFor { promise in
let action = WooShippingAction.printLabel(siteID: self.sampleSiteID,
labelIDs: [123],
paperSize: .letter) { result in
guard let printData = try? result.get() else {
XCTFail("Error printing shipping label: \(String(describing: result.failure))")
return
}
promise(printData)
}
store.onAction(action)
}

// Then
XCTAssertEqual(printData, expectedPrintData)
}

func test_printLabel_returns_error_on_failure() throws {
// Given
let expectedError = NetworkError.timeout()
let remote = MockWooShippingRemote()
remote.whenPrintLabel(siteID: sampleSiteID, thenReturn: .failure(expectedError))
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

// When
let error: Error = waitFor { promise in
let action = WooShippingAction.printLabel(siteID: self.sampleSiteID,
labelIDs: [123],
paperSize: .letter) { result in
guard let printData = result.failure else {
XCTFail("Unexpected result when printing shipping label: \(result)")
return
}
promise(printData)
}
store.onAction(action)
}

// Then
XCTAssertEqual(error as? NetworkError, expectedError)
}
}

private extension WooShippingStoreTests {
Expand Down

0 comments on commit f6eba59

Please sign in to comment.