@@ -531,17 +531,22 @@ final class WooShippingStoreTests: XCTestCase {
531
531
532
532
// MARK: `purchaseShippingLabel`
533
533
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 {
535
535
// Given
536
- let expectedLabel = ShippingLabel . fake ( ) . copy ( shippingLabelID: 13579 )
536
+ let expectedLabel = ShippingLabel . fake ( ) . copy ( siteID : sampleSiteID , orderID : sampleOrderID , shippingLabelID: 13579 , shipmentID : " 0 " )
537
537
let labelStatusResponse = ShippingLabelStatusPollingResponse . purchased ( expectedLabel)
538
538
let remote = MockWooShippingRemote ( )
539
539
remote. whenPurchaseShippingLabel ( siteID: sampleSiteID, thenReturn: . success( [ ShippingLabelPurchase . fake ( ) . copy ( shippingLabelID: 13579 ) ] ) )
540
540
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
+
541
546
let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
542
547
543
548
// When
544
- let result : Result < ShippingLabel , Error > = waitFor { promise in
549
+ let result : Result < ShippingLabel , Error > = waitFor ( timeout : 10 ) { promise in
545
550
let action = WooShippingAction . purchaseShippingLabel ( siteID: self . sampleSiteID,
546
551
orderID: self . sampleOrderID,
547
552
originAddress: . fake( ) ,
@@ -558,6 +563,15 @@ final class WooShippingStoreTests: XCTestCase {
558
563
XCTAssertTrue ( result. isSuccess)
559
564
let actualLabel = try XCTUnwrap ( result. get ( ) )
560
565
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)
561
575
}
562
576
563
577
func test_purchaseShippingLabel_returns_error_on_purchaseShippingLabel_request_failure( ) throws {
@@ -1037,11 +1051,13 @@ final class WooShippingStoreTests: XCTestCase {
1037
1051
1038
1052
// MARK: `updateShipment`
1039
1053
1040
- func test_updateShipment_returns_success_response ( ) throws {
1054
+ func test_updateShipment_returns_success_response_and_persists_shipments ( ) throws {
1041
1055
// Given
1042
1056
let remote = MockWooShippingRemote ( )
1043
1057
let expected = [ " 0 " : [ WooShippingShipmentItem . fake ( ) ] ]
1044
1058
remote. whenUpdatingShipment ( siteID: sampleSiteID, thenReturn: . success( expected) )
1059
+
1060
+ insertOrder ( siteID: sampleSiteID, orderID: sampleOrderID)
1045
1061
let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
1046
1062
1047
1063
// When
@@ -1057,6 +1073,10 @@ final class WooShippingStoreTests: XCTestCase {
1057
1073
// Then
1058
1074
let actual = try XCTUnwrap ( result. get ( ) )
1059
1075
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 " )
1060
1080
}
1061
1081
1062
1082
func test_updateShipment_returns_error_on_failure( ) throws {
@@ -1088,16 +1108,18 @@ final class WooShippingStoreTests: XCTestCase {
1088
1108
let sampleOrderID : Int64 = 134
1089
1109
let remote = MockWooShippingRemote ( )
1090
1110
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 " )
1092
1112
1093
1113
remote. whenRefundingShippingLabel ( siteID: shippingLabel. siteID,
1094
1114
orderID: shippingLabel. orderID,
1095
1115
shippingLabelID: shippingLabel. shippingLabelID,
1096
1116
thenReturn: . success( expectedRefund) )
1097
1117
let store = WooShippingStore ( dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)
1098
1118
1119
+ let shipment = insertShipment ( siteID: sampleSiteID, orderID: sampleOrderID, index: " 0 " )
1099
1120
// Inserts a shipping label without a refund.
1100
- insertShippingLabel ( shippingLabel)
1121
+ let storedLabel = insertShippingLabel ( shippingLabel)
1122
+ shipment. shippingLabel = storedLabel
1101
1123
1102
1124
XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabel . self) , 1 )
1103
1125
XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabelRefund . self) , 0 )
@@ -1121,6 +1143,10 @@ final class WooShippingStoreTests: XCTestCase {
1121
1143
1122
1144
XCTAssertEqual ( viewStorage. countObjects ( ofType: StorageShippingLabel . self) , 1 )
1123
1145
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)
1124
1150
}
1125
1151
1126
1152
func test_refundShippingLabel_returns_error_on_failure( ) throws {
@@ -1390,15 +1416,28 @@ private extension WooShippingStoreTests {
1390
1416
groupId: " " ) ] ) ] )
1391
1417
}
1392
1418
1393
- func insertShippingLabel( _ readOnlyShippingLabel: Yosemite . ShippingLabel ) {
1419
+ @discardableResult
1420
+ func insertShippingLabel( _ readOnlyShippingLabel: Yosemite . ShippingLabel ) -> StorageShippingLabel {
1394
1421
let shippingLabel = viewStorage. insertNewObject ( ofType: StorageShippingLabel . self)
1395
1422
shippingLabel. update ( with: readOnlyShippingLabel)
1423
+ return shippingLabel
1396
1424
}
1397
1425
1398
- func insertOrder( siteID: Int64 , orderID: Int64 ) {
1426
+ @discardableResult
1427
+ func insertOrder( siteID: Int64 , orderID: Int64 ) -> StorageOrder {
1399
1428
let order = viewStorage. insertNewObject ( ofType: StorageOrder . self)
1400
1429
order. siteID = siteID
1401
1430
order. orderID = orderID
1402
1431
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
1403
1442
}
1404
1443
}
0 commit comments