diff --git a/Modules/Sources/Fakes/Networking.generated.swift b/Modules/Sources/Fakes/Networking.generated.swift index c0b2ee4f8c7..1d5f76ca017 100644 --- a/Modules/Sources/Fakes/Networking.generated.swift +++ b/Modules/Sources/Fakes/Networking.generated.swift @@ -2483,6 +2483,19 @@ extension Networking.WooShippingSelectedRate { ) } } +extension Networking.WooShippingShipment { + /// Returns a "ready to use" type filled with fake values. + /// + public static func fake() -> Networking.WooShippingShipment { + .init( + siteID: .fake(), + orderID: .fake(), + index: .fake(), + items: .fake(), + shippingLabel: .fake() + ) + } +} extension Networking.WooShippingShipmentItem { /// Returns a "ready to use" type filled with fake values. /// diff --git a/Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift b/Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift index d1b14fea4d3..3d21b437725 100644 --- a/Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift +++ b/Modules/Sources/Networking/Model/Copiable/Models+Copiable.generated.swift @@ -3481,7 +3481,7 @@ extension Networking.WooShippingAddress { extension Networking.WooShippingConfig { public func copy( siteID: CopiableProp = .copy, - shipments: CopiableProp<[String: [WooShippingShipmentItem]]> = .copy, + shipments: CopiableProp<[WooShippingShipment]> = .copy, shippingLabelData: NullableCopiableProp = .copy ) -> Networking.WooShippingConfig { let siteID = siteID ?? self.siteID @@ -3724,6 +3724,30 @@ extension Networking.WooShippingPackagesResponse { } } +extension Networking.WooShippingShipment { + public func copy( + siteID: CopiableProp = .copy, + orderID: CopiableProp = .copy, + index: CopiableProp = .copy, + items: CopiableProp<[WooShippingShipmentItem]> = .copy, + shippingLabel: NullableCopiableProp = .copy + ) -> Networking.WooShippingShipment { + let siteID = siteID ?? self.siteID + let orderID = orderID ?? self.orderID + let index = index ?? self.index + let items = items ?? self.items + let shippingLabel = shippingLabel ?? self.shippingLabel + + return Networking.WooShippingShipment( + siteID: siteID, + orderID: orderID, + index: index, + items: items, + shippingLabel: shippingLabel + ) + } +} + extension Networking.WooShippingShipmentItem { public func copy( id: CopiableProp = .copy, diff --git a/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingConfigResponse.swift b/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingConfigResponse.swift index 86e744471ed..e1344acacef 100644 --- a/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingConfigResponse.swift +++ b/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingConfigResponse.swift @@ -15,14 +15,14 @@ public struct WooShippingConfig: Decodable, Equatable, GeneratedFakeable, Genera /// The remote ID of the site that owns this shipping label config info. public let siteID: Int64 - /// Shipments of this order. The keys are the ids of the shipment. - public let shipments: WooShippingShipments + /// Shipments of this order. + public let shipments: [WooShippingShipment] /// Holds info about the shipping labels public let shippingLabelData: WooShippingLabelData? public init(siteID: Int64, - shipments: WooShippingShipments, + shipments: [WooShippingShipment], shippingLabelData: WooShippingLabelData?) { self.siteID = siteID self.shipments = shipments @@ -39,17 +39,46 @@ public struct WooShippingConfig: Decodable, Equatable, GeneratedFakeable, Genera throw WooShippingConfigDecodingError.missingSiteID } + guard let orderID = decoder.userInfo[.orderID] as? Int64 else { + throw WooShippingConfigDecodingError.missingOrderID + } + let container = try decoder.container(keyedBy: CodingKeys.self) - let shipments: WooShippingShipments = { + let shippingLabelData = try container.decodeIfPresent(WooShippingLabelData.self, forKey: .shippingLabelData) + + let shipments: [WooShippingShipment] = { guard let shipmentsString = try? container.decodeIfPresent(String.self, forKey: .shipments), let data = shipmentsString.data(using: .utf8) else { - return [:] + return [] } - return (try? JSONDecoder().decode(WooShippingShipments.self, from: data)) ?? [:] + guard let contents = (try? JSONDecoder().decode(WooShippingShipments.self, from: data)) else { + return [] + } + + let labels = shippingLabelData?.currentOrderLabels ?? [] + var shipments = [WooShippingShipment]() + for (index, items) in contents { + let label: ShippingLabel? = { + let purchasedLabels = labels.filter { + $0.shipmentID == index && $0.status == .purchased + } + let sortedLabels = purchasedLabels.sorted { $0.dateCreated > $1.dateCreated } + if let completedLabel = sortedLabels.first(where: { $0.refund == nil }) { + return completedLabel + } else { + return sortedLabels.first + } + }() + shipments.append(WooShippingShipment(siteID: siteID, + orderID: orderID, + index: index, + items: items, + shippingLabel: label)) + } + return shipments }() - let shippingLabelData = try container.decodeIfPresent(WooShippingLabelData.self, forKey: .shippingLabelData) self.init(siteID: siteID, shipments: shipments, shippingLabelData: shippingLabelData) @@ -79,4 +108,5 @@ public struct WooShippingLabelData: Decodable, Equatable { // enum WooShippingConfigDecodingError: Error { case missingSiteID + case missingOrderID } diff --git a/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingShipmentItem.swift b/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingShipmentItem.swift index 979371562c6..c4c1b5f11c2 100644 --- a/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingShipmentItem.swift +++ b/Modules/Sources/Networking/Model/ShippingLabel/Shipments/WooShippingShipmentItem.swift @@ -1,8 +1,39 @@ import Foundation import Codegen -import WooFoundation -/// Represents a shipment in Shipping Labels for the WooCommerce Shipping extension. +/// Represents a shipment from the WooCommerce Shipping extension. +/// +public struct WooShippingShipment: Equatable, GeneratedFakeable, GeneratedCopiable { + /// ID of the site that the shipment belongs to. + public let siteID: Int64 + + /// ID of the order that the shipment belongs to. + public let orderID: Int64 + + /// Index of the shipment. + /// The expected format is a numeric string, e.g: "0", "1", etc. + public let index: String + + /// Contents of the shipment + public let items: [WooShippingShipmentItem] + + /// The latest label purchased for the shipment + public let shippingLabel: ShippingLabel? + + public init(siteID: Int64, + orderID: Int64, + index: String, + items: [WooShippingShipmentItem], + shippingLabel: ShippingLabel?) { + self.siteID = siteID + self.orderID = orderID + self.index = index + self.items = items + self.shippingLabel = shippingLabel + } +} + +/// Represents a shipment item from the WooCommerce Shipping extension. /// public struct WooShippingShipmentItem: Codable, Equatable, GeneratedFakeable, GeneratedCopiable { /// ID of the shipment diff --git a/Modules/Sources/Yosemite/Model/Model.swift b/Modules/Sources/Yosemite/Model/Model.swift index 78506115603..f22184700c1 100644 --- a/Modules/Sources/Yosemite/Model/Model.swift +++ b/Modules/Sources/Yosemite/Model/Model.swift @@ -200,6 +200,7 @@ public typealias WooShippingDestinationAddressUpdate = Networking.WooShippingDes public typealias WooShippingConfig = Networking.WooShippingConfig public typealias WooShippingUpdateShipment = Networking.WooShippingUpdateShipment public typealias WooShippingShipmentItem = Networking.WooShippingShipmentItem +public typealias WooShippingShipment = Networking.WooShippingShipment public typealias WooShippingShipments = Networking.WooShippingShipments public typealias WooShippingSelectedRate = Networking.WooShippingSelectedRate public typealias WPComPlan = Networking.WPComPlan diff --git a/Modules/Tests/NetworkingTests/Mapper/WooShippingConfigMapperTests.swift b/Modules/Tests/NetworkingTests/Mapper/WooShippingConfigMapperTests.swift index ea47e994bbf..ea6d195169a 100644 --- a/Modules/Tests/NetworkingTests/Mapper/WooShippingConfigMapperTests.swift +++ b/Modules/Tests/NetworkingTests/Mapper/WooShippingConfigMapperTests.swift @@ -15,8 +15,21 @@ final class WooShippingConfigMapperTests: XCTestCase { } XCTAssertEqual(config.shipments.count, 3) + let shipment0 = try XCTUnwrap(config.shipments.first(where: { $0.index == "0" })) + XCTAssertNil(shipment0.shippingLabel) // non-purchased status means no label found + XCTAssertEqual(shipment0.items.count, 1) + + let shipment1 = try XCTUnwrap(config.shipments.first(where: { $0.index == "1" })) + XCTAssertEqual(shipment1.shippingLabel?.shippingLabelID, 4871) + XCTAssertEqual(shipment1.items.count, 1) + + let shipment2 = try XCTUnwrap(config.shipments.first(where: { $0.index == "2" })) + XCTAssertEqual(shipment2.shippingLabel?.shippingLabelID, 4873) + XCTAssertNotNil(shipment2.shippingLabel?.refund) + XCTAssertEqual(shipment2.items.count, 1) + let shippingLabelData = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels) - XCTAssertEqual(shippingLabelData.count, 1) + XCTAssertEqual(shippingLabelData.count, 3) XCTAssertEqual(shippingLabelData.first?.shipmentID, "1") XCTAssertEqual(shippingLabelData.first?.shippingLabelID, 4871) } @@ -28,8 +41,22 @@ final class WooShippingConfigMapperTests: XCTestCase { } XCTAssertEqual(config.shipments.count, 3) + let shipment0 = try XCTUnwrap(config.shipments.first(where: { $0.index == "0" })) + XCTAssertNil(shipment0.shippingLabel) // non-purchased status means no label found + XCTAssertEqual(shipment0.items.count, 1) + + let shipment1 = try XCTUnwrap(config.shipments.first(where: { $0.index == "1" })) + XCTAssertEqual(shipment1.shippingLabel?.shippingLabelID, 4871) + XCTAssertNil(shipment1.shippingLabel?.refund) + XCTAssertEqual(shipment1.items.count, 1) + + let shipment2 = try XCTUnwrap(config.shipments.first(where: { $0.index == "2" })) + XCTAssertEqual(shipment2.shippingLabel?.shippingLabelID, 4873) + XCTAssertNotNil(shipment2.shippingLabel?.refund) + XCTAssertEqual(shipment2.items.count, 1) + let shippingLabelData = try XCTUnwrap(config.shippingLabelData?.currentOrderLabels) - XCTAssertEqual(shippingLabelData.count, 1) + XCTAssertEqual(shippingLabelData.count, 3) XCTAssertEqual(shippingLabelData.first?.shipmentID, "1") XCTAssertEqual(shippingLabelData.first?.shippingLabelID, 4871) } diff --git a/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success-without-data.json b/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success-without-data.json index 11f096785be..67b2aec7079 100644 --- a/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success-without-data.json +++ b/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success-without-data.json @@ -9,7 +9,7 @@ "created": 1742292110723, "carrier_id": "usps", "service_name": "USPS - Priority Mail", - "status": "PURCHASE_IN_PROGRESS", + "status": "PURCHASED", "commercial_invoice_url": "", "is_commercial_invoice_submitted_electronically": false, "package_name": "Small Flat Rate Box", @@ -23,6 +23,55 @@ "id": "1", "receipt_item_id": -1, "created_date": 1742292110000 + }, + { + "label_id": 4872, + "tracking": null, + "refundable_amount": 0, + "created": 1742292110723, + "carrier_id": "usps", + "service_name": "USPS - Priority Mail", + "status": "PURCHASE_ERROR", + "commercial_invoice_url": "", + "is_commercial_invoice_submitted_electronically": false, + "package_name": "Small Flat Rate Box", + "is_letter": false, + "product_names": [ + "BG upload" + ], + "product_ids": [ + 522 + ], + "id": "0", + "receipt_item_id": -1, + "created_date": 1742292110000 + }, + { + "label_id": 4873, + "tracking": null, + "refundable_amount": 0, + "created": 1742292110723, + "carrier_id": "usps", + "service_name": "USPS - Priority Mail", + "status": "PURCHASED", + "commercial_invoice_url": "", + "is_commercial_invoice_submitted_electronically": false, + "package_name": "Small Flat Rate Box", + "is_letter": false, + "product_names": [ + "BG upload" + ], + "product_ids": [ + 522 + ], + "refund": { + "request_date": 1603715617000, + "status": "pending", + "refund_date": 1 + }, + "id": "2", + "receipt_item_id": -1, + "created_date": 1742292110000 } ] }, diff --git a/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success.json b/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success.json index 29ddc601e92..e136752dfc9 100644 --- a/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success.json +++ b/Modules/Tests/NetworkingTests/Responses/shipping-label-config-success.json @@ -10,7 +10,7 @@ "created": 1742292110723, "carrier_id": "usps", "service_name": "USPS - Priority Mail", - "status": "PURCHASE_IN_PROGRESS", + "status": "PURCHASED", "commercial_invoice_url": "", "is_commercial_invoice_submitted_electronically": false, "package_name": "Small Flat Rate Box", @@ -24,6 +24,55 @@ "id": "1", "receipt_item_id": -1, "created_date": 1742292110000 + }, + { + "label_id": 4872, + "tracking": null, + "refundable_amount": 0, + "created": 1742292110723, + "carrier_id": "usps", + "service_name": "USPS - Priority Mail", + "status": "PURCHASE_ERROR", + "commercial_invoice_url": "", + "is_commercial_invoice_submitted_electronically": false, + "package_name": "Small Flat Rate Box", + "is_letter": false, + "product_names": [ + "BG upload" + ], + "product_ids": [ + 522 + ], + "id": "0", + "receipt_item_id": -1, + "created_date": 1742292110000 + }, + { + "label_id": 4873, + "tracking": null, + "refundable_amount": 0, + "created": 1742292110723, + "carrier_id": "usps", + "service_name": "USPS - Priority Mail", + "status": "PURCHASED", + "commercial_invoice_url": "", + "is_commercial_invoice_submitted_electronically": false, + "package_name": "Small Flat Rate Box", + "is_letter": false, + "product_names": [ + "BG upload" + ], + "product_ids": [ + 522 + ], + "refund": { + "request_date": 1603715617000, + "status": "pending", + "refund_date": 1 + }, + "id": "2", + "receipt_item_id": -1, + "created_date": 1742292110000 } ] }, diff --git a/Modules/Tests/YosemiteTests/Stores/WooShippingStoreTests.swift b/Modules/Tests/YosemiteTests/Stores/WooShippingStoreTests.swift index 0e277b90640..e7f3493aa8d 100644 --- a/Modules/Tests/YosemiteTests/Stores/WooShippingStoreTests.swift +++ b/Modules/Tests/YosemiteTests/Stores/WooShippingStoreTests.swift @@ -996,7 +996,7 @@ final class WooShippingStoreTests: XCTestCase { func test_loadConfig_returns_success_response() throws { // Given let remote = MockWooShippingRemote() - let expectedConfig = WooShippingConfig.fake().copy(shipments: ["0": [WooShippingShipmentItem.fake()]]) + let expectedConfig = WooShippingConfig.fake().copy(shipments: [WooShippingShipment.fake()]) remote.whenLoadingConfig(siteID: sampleSiteID, thenReturn: .success(expectedConfig)) let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote) @@ -1084,7 +1084,7 @@ final class WooShippingStoreTests: XCTestCase { expiryDate: nil) }() let expectedResponse = WooShippingConfig.fake().copy( - shipments: ["0": [WooShippingShipmentItem.fake()]], + shipments: [WooShippingShipment.fake()], shippingLabelData: WooShippingLabelData(currentOrderLabels: [expectedShippingLabel]) ) remote.whenLoadingConfig(siteID: sampleSiteID, thenReturn: .success(expectedResponse)) diff --git a/WooCommerce/Classes/ViewModels/Order Details/OrderDetailsDataSource.swift b/WooCommerce/Classes/ViewModels/Order Details/OrderDetailsDataSource.swift index cad8157d7a9..63d0e786621 100644 --- a/WooCommerce/Classes/ViewModels/Order Details/OrderDetailsDataSource.swift +++ b/WooCommerce/Classes/ViewModels/Order Details/OrderDetailsDataSource.swift @@ -1696,7 +1696,6 @@ extension OrderDetailsDataSource { } } - // MARK: - Constants extension OrderDetailsDataSource { enum Localization { diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Package Details/ShippingLabelSampleData.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Package Details/ShippingLabelSampleData.swift index ae0dc6e8917..b63e9ef0a35 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Package Details/ShippingLabelSampleData.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/Create Shipping Label Form/Package Details/ShippingLabelSampleData.swift @@ -57,7 +57,13 @@ enum ShippingLabelSampleData { static func sampleWooShippingConfig() -> WooShippingConfig { WooShippingConfig(siteID: 123, - shipments: ["0": [sampleWooShippingShipmentItem()]], + shipments: [WooShippingShipment( + siteID: 1, + orderID: 2, + index: "0", + items: [sampleWooShippingShipmentItem()], + shippingLabel: nil, + )], shippingLabelData: nil) } } diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Split Shipments/WooShippingSplitShipmentsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Split Shipments/WooShippingSplitShipmentsViewModel.swift index 5e4feaa198e..3176ef1ac83 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Split Shipments/WooShippingSplitShipmentsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Split Shipments/WooShippingSplitShipmentsViewModel.swift @@ -37,7 +37,7 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject { /// private var editedShipmentsInfo: WooShippingShipments { var shipmentsForRemote = [String: [WooShippingShipmentItem]]() - for shipment in shipments { + for shipment in shipments.sorted(by: { $0.index < $1.index}) { var items = [WooShippingShipmentItem]() for item in shipment.contents { if let mainItemID = Int(item.mainItemRow.itemID) { @@ -159,17 +159,18 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject { selectedShipmentIndex = 0 } - func didPurchaseLabel(for shipmentIndex: Int, purchasedLabelID: Int64) { + func didPurchaseLabel(for shipmentIndex: Int, label: ShippingLabel) { let currentShipment = shipments[shipmentIndex] let updatedContents = currentShipment.contents.map { CollapsibleShipmentItemCardViewModel(item: $0.packageItem, isSelectable: false, currency: order.currency) } shipments[shipmentIndex] = Shipment(index: shipmentIndex, contents: updatedContents, - purchasedLabelID: purchasedLabelID, + purchasedLabel: label, currency: order.currency, currencySettings: currencySettings, shippingSettingsService: shippingSettingsService) + shipmentsSavedInRemote = shipments } func didRequestRefund(for shipmentIndex: Int) { @@ -179,10 +180,11 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject { } shipments[shipmentIndex] = Shipment(index: shipmentIndex, contents: updatedContents, - purchasedLabelID: nil, + purchasedLabel: nil, currency: order.currency, currencySettings: currencySettings, shippingSettingsService: shippingSettingsService) + shipmentsSavedInRemote = shipments } func moveSelectedItems(to destination: MoveToShipmentNoticeViewModel.Destination) { @@ -542,42 +544,37 @@ extension WooShippingSplitShipmentsViewModel { return [shipment] } - let currentOrderLabels = config.shippingLabelData?.currentOrderLabels ?? [] - var shipments = [Shipment]() - - for key in config.shipments.keys.sorted() { - guard let shipmentItems = config.shipments[key] else { - continue - } - - let purchasedLabel = currentOrderLabels - .first(where: { $0.shipmentID == key && $0.status == .purchased && $0.refund == nil }) - - let isPurchased = purchasedLabel != nil + let shipments = config.shipments + .sorted(by: { $0.index.localizedStandardCompare($1.index) == .orderedAscending }) + .map { shipment in + var shipmentContents = ShipmentContents() + for shipmentItem in shipment.items { + guard let packageItem = packageItems.first(where: { $0.orderItemID == shipmentItem.id }), + let subItems = shipmentItem.subItems else { + continue + } - var shipmentContents = ShipmentContents() - for shipmentItem in shipmentItems { - guard let packageItem = packageItems.first(where: { $0.orderItemID == shipmentItem.id }), - let subItems = shipmentItem.subItems else { - continue + let quantity = subItems.count > 0 ? subItems.count : 1 + let updatedItem = ShippingLabelPackageItem(copy: packageItem, quantity: Decimal(quantity)) + let content = CollapsibleShipmentItemCardViewModel(item: updatedItem, + isSelectable: shipment.shippingLabel == nil, + currency: currency) + shipmentContents.append(content) } - let quantity = subItems.count > 0 ? subItems.count : 1 - let updatedItem = ShippingLabelPackageItem(copy: packageItem, quantity: Decimal(quantity)) - let content = CollapsibleShipmentItemCardViewModel(item: updatedItem, - isSelectable: !isPurchased, - currency: currency) - shipmentContents.append(content) + let purchasedLabel: ShippingLabel? = { + guard let label = shipment.shippingLabel, label.refund == nil else { + return nil + } + return label + }() + return Shipment(index: Int(shipment.index) ?? 0, + contents: shipmentContents, + purchasedLabel: purchasedLabel, + currency: currency, + currencySettings: currencySettings, + shippingSettingsService: shippingSettingsService) } - - let shipment = Shipment(index: Int(key) ?? 0, - contents: shipmentContents, - purchasedLabelID: purchasedLabel?.shippingLabelID, - currency: currency, - currencySettings: currencySettings, - shippingSettingsService: shippingSettingsService) - shipments.append(shipment) - } return shipments } @@ -597,7 +594,7 @@ extension WooShippingSplitShipmentsViewModel { let index: Int let contents: [CollapsibleShipmentItemCardViewModel] - let purchasedLabelID: Int64? + let purchasedLabel: ShippingLabel? let quantity: String let weight: String @@ -614,7 +611,7 @@ extension WooShippingSplitShipmentsViewModel { } var isPurchased: Bool { - purchasedLabelID != nil + purchasedLabel != nil } private let currency: String @@ -624,14 +621,14 @@ extension WooShippingSplitShipmentsViewModel { init(id: String = UUID().uuidString, index: Int = 0, contents: [CollapsibleShipmentItemCardViewModel], - purchasedLabelID: Int64? = nil, + purchasedLabel: ShippingLabel? = nil, currency: String, currencySettings: CurrencySettings, shippingSettingsService: ShippingSettingsService) { self.id = id self.index = index self.contents = contents - self.purchasedLabelID = purchasedLabelID + self.purchasedLabel = purchasedLabel let items = contents.map(\.packageItem) let itemsCount = items.map(\.quantity).reduce(0, +) @@ -672,7 +669,7 @@ extension WooShippingSplitShipmentsViewModel { Shipment(id: id, index: newIndex, contents: contents, - purchasedLabelID: purchasedLabelID, + purchasedLabel: purchasedLabel, currency: currency, currencySettings: currencySettings, shippingSettingsService: shippingSettingsService) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift index df1efcb51f1..0739fe45d41 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModel.swift @@ -26,9 +26,6 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { private(set) var orderItems: WooShippingItemsViewModel - /// The purchased shipping label. - @Published private var shippingLabels: [ShippingLabel] = [] - var canViewLabel: Bool { currentShipmentDetailsViewModel.canViewLabel } @@ -57,7 +54,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { } var hasUnfulfilledShipments: Bool { - shipments.contains(where: { $0.purchasedLabelID == nil }) + shipments.contains(where: { $0.purchasedLabel == nil }) } @Published var labelPurchaseErrorNotice: Notice? @@ -245,7 +242,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { // After shipment configs are updated, shipments are updated with purchased label details // Update the selected tab now by comparing the purchased labels with the initial selected label. if let selectedShippingLabel, - let matchingIndex = shipments.firstIndex(where: { $0.purchasedLabelID == selectedShippingLabel.shippingLabelID }) { + let matchingIndex = shipments.firstIndex(where: { $0.purchasedLabel?.shippingLabelID == selectedShippingLabel.shippingLabelID }) { selectedShipmentIndex = matchingIndex } } @@ -261,7 +258,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { } } - if shipments.contains(where: { $0.purchasedLabelID == nil }) { + if shipments.contains(where: { $0.purchasedLabel == nil }) { group.addTask { await self.loadOriginAddresses() } @@ -277,7 +274,7 @@ final class WooShippingCreateLabelsViewModel: ObservableObject { state = .missingRequiredData } else { state = .ready - let count = shipments.count(where: { $0.purchasedLabelID == nil }) + let count = shipments.count(where: { $0.purchasedLabel == nil }) analytics.track(event: .WooShipping.createShippingLabelFormShown(unfulfilledShipmentsCount: count)) } } @@ -435,7 +432,6 @@ private extension WooShippingCreateLabelsViewModel { } if let config { - shippingLabels = config.shippingLabelData?.currentOrderLabels.filter { $0.refund == nil && $0.status == .purchased } ?? [] splitShipmentsViewModel = WooShippingSplitShipmentsViewModel(order: order, config: config, items: itemsDataSource.items, @@ -494,14 +490,13 @@ private extension WooShippingCreateLabelsViewModel { func updateShipmentDetailsViewModels() { shipmentDetailViewModels = shipments.map { shipment in - let matchingShippingLabel = shippingLabels.first(where: { $0.shippingLabelID == shipment.purchasedLabelID }) let originAddressPublisher = $selectedOriginAddress .map { $0?.toWooShippingAddress() } .eraseToAnyPublisher() return WooShippingShipmentDetailsViewModel( order: order, shipment: shipment, - shippingLabel: matchingShippingLabel, + shippingLabel: shipment.purchasedLabel, originAddress: originAddressPublisher, destinationAddress: $destinationAddress.eraseToAnyPublisher(), stores: stores, @@ -518,33 +513,26 @@ private extension WooShippingCreateLabelsViewModel { } func handleLabelPurchaseSuccess(newLabel: ShippingLabel, in shipment: Shipment) { - shippingLabels.append(newLabel) - let index = shipment.index shipments[index] = Shipment(index: index, contents: shipment.contents, - purchasedLabelID: newLabel.shippingLabelID, + purchasedLabel: newLabel, currency: order.currency, currencySettings: currencySettings, shippingSettingsService: shippingSettingsService) - splitShipmentsViewModel.didPurchaseLabel(for: index, - purchasedLabelID: newLabel.shippingLabelID) + splitShipmentsViewModel.didPurchaseLabel(for: index, label: newLabel) onLabelPurchase?(markOrderComplete) } func handleLabelRefundRequested(labelID: Int64, in shipment: Shipment) { - let labelIndex = shippingLabels.firstIndex(where: { $0.shippingLabelID == labelID }) - guard let labelIndex else { return } - let shipmentIndex = shipment.index shipments[shipmentIndex] = Shipment(index: shipmentIndex, contents: shipment.contents, - purchasedLabelID: nil, + purchasedLabel: nil, currency: order.currency, currencySettings: currencySettings, shippingSettingsService: shippingSettingsService) - shippingLabels.remove(at: labelIndex) splitShipmentsViewModel.didRequestRefund(for: shipmentIndex) refundNotice = Notice(message: Localization.refundNotice) } diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/OrderDetailsDataSourceTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/OrderDetailsDataSourceTests.swift index f2daab1b770..99d5ea530f9 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/OrderDetailsDataSourceTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/OrderDetailsDataSourceTests.swift @@ -1217,7 +1217,6 @@ private extension OrderDetailsDataSourceTests { func insert(_ readOnlyPlugin: Yosemite.SitePlugin) { let plugin = storage.insertNewObject(ofType: StorageSitePlugin.self) plugin.update(with: readOnlyPlugin) - storage.saveIfNeeded() } /// Finds first section with a given title from the provided data source. diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/Split shipments/WooShippingSplitShipmentsViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/Split shipments/WooShippingSplitShipmentsViewModelTests.swift index 2a7eb206f9a..2a892f26ae8 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/Split shipments/WooShippingSplitShipmentsViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/Split shipments/WooShippingSplitShipmentsViewModelTests.swift @@ -105,10 +105,12 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1)] // When - let shippingLabelData = WooShippingLabelData(currentOrderLabels: [ShippingLabel.fake().copy(shipmentID: "1")]) + let label = ShippingLabel.fake().copy(shipmentID: "1") + let refundedLabel = ShippingLabel.fake().copy(shipmentID: "0", refund: .fake()) + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label, refundedLabel]) let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])], shippingLabel: refundedLabel), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])], shippingLabel: label) ], shippingLabelData: shippingLabelData) let viewModel = WooShippingSplitShipmentsViewModel(order: sampleOrder, config: config, @@ -237,10 +239,11 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { let items = [sampleItem(id: 1, weight: 5, value: 10, quantity: 2), sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1)] - let shippingLabelData = WooShippingLabelData(currentOrderLabels: [ShippingLabel.fake().copy(shipmentID: "1")]) + let label = ShippingLabel.fake().copy(shipmentID: "1") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])]), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])], shippingLabel: label) ], shippingLabelData: shippingLabelData) let viewModel = WooShippingSplitShipmentsViewModel(order: sampleOrder, @@ -596,11 +599,12 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1), sampleItem(id: 3, weight: 4, value: 5, quantity: 3)] - let shippingLabelData = WooShippingLabelData(currentOrderLabels: [ShippingLabel.fake().copy(shipmentID: "2")]) + let label = ShippingLabel.fake().copy(shipmentID: "2") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["1-sub-0", "1-sub-1"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])], - "2": [WooShippingShipmentItem(id: 3, subItems: ["3-sub-0", "3-sub-1", "3-sub-2"])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["1-sub-0", "1-sub-1"])]), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), + .fake().copy(index: "2", items: [.init(id: 3, subItems: ["3-sub-0", "3-sub-1", "3-sub-2"])], shippingLabel: label) ], shippingLabelData: shippingLabelData) var receivedShipmentToUpdate: WooShippingUpdateShipment? @@ -629,9 +633,9 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { // Then let expectedShipmentToUpdate = WooShippingUpdateShipment( shipmentIdsToUpdate: ["2": 1], - shipments: ["1": [WooShippingShipmentItem(id: 3, subItems: ["3-sub-0", "3-sub-1", "3-sub-2"])], - "0": [WooShippingShipmentItem(id: 1, subItems: ["1-sub-0", "1-sub-1"]), - WooShippingShipmentItem(id: 2, subItems: [])]] + shipments: ["0": [WooShippingShipmentItem(id: 1, subItems: ["1-sub-0", "1-sub-1"]), + WooShippingShipmentItem(id: 2, subItems: [])], + "1": [WooShippingShipmentItem(id: 3, subItems: ["3-sub-0", "3-sub-1", "3-sub-2"])]] ) XCTAssertEqual(receivedShipmentToUpdate, expectedShipmentToUpdate) } @@ -648,14 +652,14 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { let shippingLabel = ShippingLabel.fake().copy(shipmentID: "1") let config = WooShippingConfig.fake().copy( shipments: [ - "0": [ + .fake().copy(index: "0", items: [ WooShippingShipmentItem.fake().copy(id: 1, subItems: ["1-sub-0", "1-sub-1"]), WooShippingShipmentItem.fake().copy(id: 2, subItems: []), WooShippingShipmentItem.fake().copy(id: 3, subItems: ["3-sub-0", "3-sub-1", "3-sub-2"]) - ], - "1": [ + ]), + .fake().copy(index: "1", items: [ WooShippingShipmentItem.fake().copy(id: 4, subItems: nil) - ] + ], shippingLabel: shippingLabel) ], shippingLabelData: WooShippingLabelData(currentOrderLabels: [shippingLabel]) ) @@ -773,11 +777,12 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1), sampleItem(id: 3, weight: 4, value: 5, quantity: 3)] - let shippingLabelData = WooShippingLabelData(currentOrderLabels: [ShippingLabel.fake().copy(shipmentID: "1")]) + let label = ShippingLabel.fake().copy(shipmentID: "1") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])], - "2": [WooShippingShipmentItem(id: 3, subItems: ["sub-1", "sub-2", "sub-3"])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])]), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])], shippingLabel: label), + .fake().copy(index: "2", items: [.init(id: 3, subItems: ["sub-1", "sub-2", "sub-3"])]) ], shippingLabelData: shippingLabelData) let viewModel = WooShippingSplitShipmentsViewModel(order: sampleOrder, config: config, @@ -815,8 +820,8 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { let shippingLabelData = WooShippingLabelData(currentOrderLabels: []) // none purchased yet let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])]), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), ], shippingLabelData: shippingLabelData) let viewModel = WooShippingSplitShipmentsViewModel(order: sampleOrder, config: config, @@ -830,19 +835,21 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { // When let shipmentID = viewModel.shipments[0].index let purchasedLabelID: Int64 = 325 - viewModel.didPurchaseLabel(for: shipmentID, purchasedLabelID: purchasedLabelID) + let label = ShippingLabel.fake().copy(shippingLabelID: purchasedLabelID) + viewModel.didPurchaseLabel(for: shipmentID, label: label) // Then + XCTAssertFalse(viewModel.containsUnsavedChanges) XCTAssertEqual(viewModel.shipments.count, 2) XCTAssertEqual(viewModel.shipments[0].index, 0) - XCTAssertEqual(viewModel.shipments[0].purchasedLabelID, purchasedLabelID) + XCTAssertEqual(viewModel.shipments[0].purchasedLabel, label) XCTAssertFalse(viewModel.shipments[0].contents[0].mainItemRow.isSelectable) XCTAssertTrue(viewModel.shipments[0].contents[0].childItemRows.allSatisfy({ !$0.isSelectable })) XCTAssertEqual(viewModel.shipments[1].index, 1) XCTAssertEqual(viewModel.shipments[1].contents.count, 1) XCTAssertTrue(viewModel.shipments[1].contents[0].mainItemRow.isSelectable) - XCTAssertNil(viewModel.shipments[1].purchasedLabelID) + XCTAssertNil(viewModel.shipments[1].purchasedLabel) } // MARK: - `didRequestRefund` @@ -852,14 +859,11 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1), sampleItem(id: 3, weight: 4, value: 5, quantity: 3)] - let shippingLabelData = WooShippingLabelData( - currentOrderLabels: [ - ShippingLabel.fake().copy(shipmentID: "0") - ] - ) + let label = ShippingLabel.fake().copy(shipmentID: "0") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig(siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])], shippingLabel: label), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), ], shippingLabelData: shippingLabelData) let viewModel = WooShippingSplitShipmentsViewModel(order: sampleOrder, config: config, @@ -875,15 +879,16 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { viewModel.didRequestRefund(for: shipmentID) // Then + XCTAssertFalse(viewModel.containsUnsavedChanges) XCTAssertEqual(viewModel.shipments.count, 2) - XCTAssertNil(viewModel.shipments[0].purchasedLabelID) + XCTAssertNil(viewModel.shipments[0].purchasedLabel) XCTAssertTrue(viewModel.shipments[0].contents[0].mainItemRow.isSelectable) XCTAssertTrue(viewModel.shipments[0].contents[0].childItemRows.allSatisfy({ $0.isSelectable })) XCTAssertEqual(viewModel.shipments[1].contents.count, 1) XCTAssertTrue(viewModel.shipments[1].contents[0].mainItemRow.isSelectable) - XCTAssertNil(viewModel.shipments[1].purchasedLabelID) + XCTAssertNil(viewModel.shipments[1].purchasedLabel) } // MARK: - `enableDoneButton` @@ -1008,17 +1013,14 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1) ] - let shippingLabelData = WooShippingLabelData( - currentOrderLabels: [ - ShippingLabel.fake().copy(shipmentID: "0") - ] - ) + let label = ShippingLabel.fake().copy(shipmentID: "0") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig( siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])], shippingLabel: label), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), ], shippingLabelData: shippingLabelData ) @@ -1070,17 +1072,14 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { sampleItem(id: 2, weight: 3, value: 2.5, quantity: 1) ] - let shippingLabelData = WooShippingLabelData( - currentOrderLabels: [ - ShippingLabel.fake().copy(shipmentID: "1") - ] - ) + let label = ShippingLabel.fake().copy(shipmentID: "1") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig( siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])], shippingLabel: label), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), ], shippingLabelData: shippingLabelData ) @@ -1110,8 +1109,8 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { let config = WooShippingConfig( siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])], - "1": [WooShippingShipmentItem(id: 2, subItems: [])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])]), + .fake().copy(index: "1", items: [.init(id: 2, subItems: [])]), ], shippingLabelData: WooShippingLabelData(currentOrderLabels: []) ) @@ -1192,16 +1191,13 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { func test_isMergeAllUnfulfilledAvailable_returns_false_when_no_unfulfilled_shipments() throws { // Given - let shippingLabelData = WooShippingLabelData( - currentOrderLabels: [ - ShippingLabel.fake().copy(shipmentID: "0") - ] - ) + let label = ShippingLabel.fake().copy(shipmentID: "0") + let shippingLabelData = WooShippingLabelData(currentOrderLabels: [label]) let config = WooShippingConfig( siteID: 123, shipments: [ - "0": [WooShippingShipmentItem(id: 1, subItems: ["sub-1", "sub-2"])] + .fake().copy(index: "0", items: [.init(id: 1, subItems: ["sub-1", "sub-2"])], shippingLabel: label), ], shippingLabelData: shippingLabelData ) @@ -1315,18 +1311,18 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { ] let config = WooShippingConfig.fake().copy( shipments: [ - "0": [ + .fake().copy(index: "0", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ], - "1": [ + ]), + .fake().copy(index: "1", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ] + ]) ] ) let viewModel = WooShippingSplitShipmentsViewModel( @@ -1354,18 +1350,18 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { let shippingLabel = ShippingLabel.fake().copy(shipmentID: "0") let config = WooShippingConfig.fake().copy( shipments: [ - "0": [ + .fake().copy(index: "0", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ], - "1": [ + ], shippingLabel: shippingLabel), + .fake().copy(index: "1", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ] + ]) ], shippingLabelData: WooShippingLabelData( currentOrderLabels: [shippingLabel] @@ -1397,18 +1393,18 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { ] let config = WooShippingConfig.fake().copy( shipments: [ - "0": [ + .fake().copy(index: "0", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ], - "1": [ + ]), + .fake().copy(index: "1", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: nil ) - ] + ]) ] ) let viewModel = WooShippingSplitShipmentsViewModel( @@ -1435,24 +1431,24 @@ final class WooShippingSplitShipmentsViewModelTests: XCTestCase { ] let config = WooShippingConfig.fake().copy( shipments: [ - "0": [ + .fake().copy(index: "0", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: ["sub-1"] ) - ], - "1": [ + ]), + .fake().copy(index: "1", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: ["sub-2"] ) - ], - "2": [ + ]), + .fake().copy(index: "2", items: [ WooShippingShipmentItem.fake().copy( id: 1, subItems: ["sub-3"] ) - ] + ]) ] ) let viewModel = WooShippingSplitShipmentsViewModel( diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift index b718d64c009..672634f901a 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingCreateLabelsViewModelTests.swift @@ -537,8 +537,8 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { case .loadConfig(_, _, let completion): // There exist 2 shipments, one of which has been fulfilled. let shippingLabel = ShippingLabel.fake().copy(shippingLabelID: 134) - let shipments = ["shipment_0": [WooShippingShipmentItem.fake()], - "shipment_1": [WooShippingShipmentItem.fake()]] + let shipments = [WooShippingShipment.fake().copy(index: "shipment_0", items: [.fake()], shippingLabel: shippingLabel), + WooShippingShipment.fake().copy(index: "shipment_1", items: [.fake()])] let shippingLabelData = WooShippingLabelData(currentOrderLabels: [ ShippingLabel.fake().copy(shippingLabelID: shippingLabel.shippingLabelID, shipmentID: "shipment_0") @@ -610,8 +610,8 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { completion(.success(self.settings)) case .loadConfig(_, _, let completion): // There exist 2 shipments, one of which has been fulfilled. - let shipments = ["0": [WooShippingShipmentItem.fake()], - "1": [WooShippingShipmentItem.fake()]] + let shipments = [WooShippingShipment.fake().copy(index: "0", items: [.fake()], shippingLabel: shippingLabel), + WooShippingShipment.fake().copy(index: "1", items: [.fake()])] let shippingLabelData = WooShippingLabelData(currentOrderLabels: [shippingLabel]) completion(.success(WooShippingConfig.fake().copy(shipments: shipments, shippingLabelData: shippingLabelData))) @@ -627,14 +627,14 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { } // Then - XCTAssertEqual(viewModel.currentShipment.purchasedLabelID, shippingLabel.shippingLabelID) + XCTAssertEqual(viewModel.currentShipment.purchasedLabel?.shippingLabelID, shippingLabel.shippingLabelID) XCTAssertEqual(viewModel.originAddressLines?.first, labelOriginAddress.address1) // When viewModel.selectedShipmentIndex = 1 // Then - XCTAssertNil(viewModel.currentShipment.purchasedLabelID) + XCTAssertNil(viewModel.currentShipment.purchasedLabel) XCTAssertEqual(viewModel.originAddressLines?.first, originAddress.address1) } @@ -658,8 +658,8 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { completion(.success(self.settings)) case .loadConfig(_, _, let completion): // There exist 2 shipments, one of which has been fulfilled. - let shipments = ["0": [WooShippingShipmentItem.fake()], - "1": [WooShippingShipmentItem.fake()]] + let shipments = [WooShippingShipment.fake().copy(index: "0", items: [.fake()], shippingLabel: shippingLabel), + WooShippingShipment.fake().copy(index: "1", items: [.fake()])] let shippingLabelData = WooShippingLabelData(currentOrderLabels: [shippingLabel]) completion(.success(WooShippingConfig.fake().copy(shipments: shipments, shippingLabelData: shippingLabelData))) @@ -679,14 +679,14 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { } // Then - XCTAssertEqual(viewModel.currentShipment.purchasedLabelID, shippingLabel.shippingLabelID) + XCTAssertEqual(viewModel.currentShipment.purchasedLabel?.shippingLabelID, shippingLabel.shippingLabelID) XCTAssertEqual(viewModel.destinationAddressLines?.first, labelDestinationAddress.address1) // When viewModel.selectedShipmentIndex = 1 // Then - XCTAssertNil(viewModel.currentShipment.purchasedLabelID) + XCTAssertNil(viewModel.currentShipment.purchasedLabel) XCTAssertEqual(viewModel.destinationAddressLines?.first, destinationAddress.address1) } @@ -702,7 +702,7 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { let order = Order.fake().copy(shippingLabels: [shippingLabel]) let shipment = Shipment( contents: [], - purchasedLabelID: shippingLabel.shippingLabelID, + purchasedLabel: shippingLabel, currency: "USD", currencySettings: ServiceLocator.currencySettings, shippingSettingsService: MockShippingSettingsService() @@ -746,7 +746,7 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { ) let shipment = Shipment( contents: [], - purchasedLabelID: nil, + purchasedLabel: nil, currency: "USD", currencySettings: ServiceLocator.currencySettings, shippingSettingsService: MockShippingSettingsService() @@ -813,7 +813,7 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { let shipment = Shipment( contents: [], - purchasedLabelID: nil, + purchasedLabel: nil, currency: "USD", currencySettings: ServiceLocator.currencySettings, shippingSettingsService: MockShippingSettingsService() @@ -895,7 +895,7 @@ final class WooShippingCreateLabelsViewModelTests: XCTestCase { let shipment = Shipment( contents: [], - purchasedLabelID: nil, + purchasedLabel: nil, currency: "USD", currencySettings: ServiceLocator.currencySettings, shippingSettingsService: MockShippingSettingsService()