@@ -531,17 +531,22 @@ final class WooShippingStoreTests: XCTestCase {
531531
532532 // MARK: `purchaseShippingLabel`
533533
534- func test_purchaseShippingLabel_returns_shipping_label_on_success ( ) throws {
534+ func test_purchaseShippingLabel_returns_shipping_label_on_success_and_persists_label_in_storage ( ) throws {
535535 // Given
536- let expectedLabel = ShippingLabel . fake ( ) . copy ( shippingLabelID: 13579 )
536+ let expectedLabel = ShippingLabel . fake ( ) . copy ( siteID : sampleSiteID , orderID : sampleOrderID , shippingLabelID: 13579 , shipmentID : " 0 " )
537537 let labelStatusResponse = ShippingLabelStatusPollingResponse . purchased ( expectedLabel)
538538 let remote = MockWooShippingRemote ( )
539539 remote. whenPurchaseShippingLabel ( siteID: sampleSiteID, thenReturn: . success( [ ShippingLabelPurchase . fake ( ) . copy ( shippingLabelID: 13579 ) ] ) )
540540 remote. whenCheckLabelStatus ( siteID: sampleSiteID, thenReturn: . success( labelStatusResponse) )
541+
542+ let order = insertOrder ( siteID: sampleSiteID, orderID: sampleOrderID)
543+ let shipment = insertShipment ( siteID: sampleSiteID, orderID: sampleOrderID, index: " 0 " )
544+ shipment. order = order
545+
541546 let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
542547
543548 // When
544- let result : Result < ShippingLabel , Error > = waitFor { promise in
549+ let result : Result < ShippingLabel , Error > = waitFor ( timeout : 10 ) { promise in
545550 let action = WooShippingAction . purchaseShippingLabel ( siteID: self . sampleSiteID,
546551 orderID: self . sampleOrderID,
547552 originAddress: . fake( ) ,
@@ -558,6 +563,15 @@ final class WooShippingStoreTests: XCTestCase {
558563 XCTAssertTrue ( result. isSuccess)
559564 let actualLabel = try XCTUnwrap ( result. get ( ) )
560565 XCTAssertEqual ( actualLabel, expectedLabel)
566+
567+ // label is persisted
568+ let storedLabels = storageManager. viewStorage. loadAllShippingLabels ( siteID: sampleSiteID, orderID: sampleOrderID)
569+ XCTAssertEqual ( storedLabels. count, 1 )
570+ XCTAssertEqual ( storedLabels. first? . shippingLabelID, expectedLabel. shippingLabelID)
571+
572+ let storedShipments = storageManager. viewStorage. loadAllShipments ( siteID: sampleSiteID, orderID: sampleOrderID)
573+ XCTAssertEqual ( storedShipments. count, 1 )
574+ XCTAssertEqual ( storedShipments. first? . shippingLabel? . shippingLabelID, expectedLabel. shippingLabelID)
561575 }
562576
563577 func test_purchaseShippingLabel_returns_error_on_purchaseShippingLabel_request_failure( ) throws {
@@ -1037,11 +1051,13 @@ final class WooShippingStoreTests: XCTestCase {
10371051
10381052 // MARK: `updateShipment`
10391053
1040- func test_updateShipment_returns_success_response ( ) throws {
1054+ func test_updateShipment_returns_success_response_and_persists_shipments ( ) throws {
10411055 // Given
10421056 let remote = MockWooShippingRemote ( )
10431057 let expected = [ " 0 " : [ WooShippingShipmentItem . fake ( ) ] ]
10441058 remote. whenUpdatingShipment ( siteID: sampleSiteID, thenReturn: . success( expected) )
1059+
1060+ insertOrder ( siteID: sampleSiteID, orderID: sampleOrderID)
10451061 let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
10461062
10471063 // When
@@ -1057,6 +1073,10 @@ final class WooShippingStoreTests: XCTestCase {
10571073 // Then
10581074 let actual = try XCTUnwrap ( result. get ( ) )
10591075 XCTAssertEqual ( actual, expected)
1076+
1077+ let storedShipments = storageManager. viewStorage. loadAllShipments ( siteID: sampleSiteID, orderID: sampleOrderID)
1078+ XCTAssertEqual ( storedShipments. count, 1 )
1079+ XCTAssertEqual ( storedShipments. first? . index, " 0 " )
10601080 }
10611081
10621082 func test_updateShipment_returns_error_on_failure( ) throws {
@@ -1088,16 +1108,18 @@ final class WooShippingStoreTests: XCTestCase {
10881108 let sampleOrderID : Int64 = 134
10891109 let remote = MockWooShippingRemote ( )
10901110 let expectedRefund = Yosemite . ShippingLabelRefund ( dateRequested: Date ( ) , status: . pending)
1091- let shippingLabel = MockShippingLabel . emptyLabel ( ) . copy ( siteID: sampleSiteID, orderID: sampleOrderID, shippingLabelID: 123 )
1111+ let shippingLabel = MockShippingLabel . emptyLabel ( ) . copy ( siteID: sampleSiteID, orderID: sampleOrderID, shippingLabelID: 123 , shipmentID : " 0 " )
10921112
10931113 remote. whenRefundingShippingLabel ( siteID: shippingLabel. siteID,
10941114 orderID: shippingLabel. orderID,
10951115 shippingLabelID: shippingLabel. shippingLabelID,
10961116 thenReturn: . success( expectedRefund) )
10971117 let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
10981118
1119+ let shipment = insertShipment ( siteID: sampleSiteID, orderID: sampleOrderID, index: " 0 " )
10991120 // Inserts a shipping label without a refund.
1100- insertShippingLabel ( shippingLabel)
1121+ let storedLabel = insertShippingLabel ( shippingLabel)
1122+ shipment. shippingLabel = storedLabel
11011123
11021124 XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabel . self) , 1 )
11031125 XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabelRefund . self) , 0 )
@@ -1121,6 +1143,10 @@ final class WooShippingStoreTests: XCTestCase {
11211143
11221144 XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabel . self) , 1 )
11231145 XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabelRefund . self) , 1 )
1146+
1147+ let storedShipments = viewStorage. loadAllShipments ( siteID: sampleSiteID, orderID: sampleOrderID)
1148+ XCTAssertEqual ( storedShipments. first? . shippingLabel? . shippingLabelID, shippingLabel. shippingLabelID)
1149+ XCTAssertNotNil ( storedShipments. first? . shippingLabel? . refund)
11241150 }
11251151
11261152 func test_refundShippingLabel_returns_error_on_failure( ) throws {
@@ -1390,15 +1416,28 @@ private extension WooShippingStoreTests {
13901416 groupId: " " ) ] ) ] )
13911417 }
13921418
1393- func insertShippingLabel( _ readOnlyShippingLabel: Yosemite . ShippingLabel ) {
1419+ @discardableResult
1420+ func insertShippingLabel( _ readOnlyShippingLabel: Yosemite . ShippingLabel ) -> StorageShippingLabel {
13941421 let shippingLabel = viewStorage. insertNewObject ( ofType: StorageShippingLabel . self)
13951422 shippingLabel. update ( with: readOnlyShippingLabel)
1423+ return shippingLabel
13961424 }
13971425
1398- func insertOrder( siteID: Int64 , orderID: Int64 ) {
1426+ @discardableResult
1427+ func insertOrder( siteID: Int64 , orderID: Int64 ) -> StorageOrder {
13991428 let order = viewStorage. insertNewObject ( ofType: StorageOrder . self)
14001429 order. siteID = siteID
14011430 order. orderID = orderID
14021431 order. statusKey = " "
1432+ return order
1433+ }
1434+
1435+ @discardableResult
1436+ func insertShipment( siteID: Int64 , orderID: Int64 , index: String ) -> StorageWooShippingShipment {
1437+ let shipment = viewStorage. insertNewObject ( ofType: StorageWooShippingShipment . self)
1438+ shipment. siteID = siteID
1439+ shipment. orderID = orderID
1440+ shipment. index = index
1441+ return shipment
14031442 }
14041443}
0 commit comments