Skip to content

Commit

Permalink
Upsert deletePackages response into storage
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelmcr committed Dec 19, 2024
1 parent 74202f1 commit 0a7021f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,9 @@ final class WooShippingAddPackageViewModel: ObservableObject {

// delete on backend
let deleteAction = WooShippingAction.deletePackage(siteID: siteID, packageID: packageToRemove.id) { result in
switch result {
case .success:
self.transformLoadedPackages()
case .failure:
if case .failure(let error) = result {
DDLogError("⛔️ Error removing saved Woo Shipping package: \(error)")

// undo removing of the package
// first: undo starring
if let carrierID = removedStarredCarrierID {
Expand Down
11 changes: 10 additions & 1 deletion Yosemite/Yosemite/Stores/WooShippingStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ private extension WooShippingStore {
func deletePackage(siteID: Int64,
packageID: String,
completion: @escaping (Result<WooShippingCreatePackageResponse, Error>) -> Void) {
remote.deletePackage(siteID: siteID, packageID: packageID, completion: completion)
remote.deletePackage(siteID: siteID, packageID: packageID) { [weak self] result in
switch result {
case .success(let packages):
self?.upsertCreatePackagesResponseInBackground(readOnlyPackages: packages, siteID: siteID, onCompletion: {
completion(.success(packages))
})
case .failure(let error):
completion(.failure(error))
}
}
}

func loadLabelRates(siteID: Int64,
Expand Down
78 changes: 60 additions & 18 deletions Yosemite/YosemiteTests/Stores/WooShippingStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ final class WooShippingStoreTests: XCTestCase {
XCTAssertTrue(result.isFailure)
}

func test_createPackage_when_successful_then_upserts_packages_into_storage() throws {
// Given
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
network.simulateResponse(requestUrlSuffix: "packages", filename: "wooshipping-create-package-success")
storageManager.insertSamplePackages(readOnlyPackages: .init(siteID: sampleSiteID,
customPackages: [],
savedPredefinedPackages: [],
allPredefinedOptions: [sampleCarrierPredefinedOptions()]))

// Confidence check
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingCustomPackage.self), 0)
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingSavedPredefinedPackage.self), 0)

// When
let onSuccess: Bool = waitFor { promise in
let action = WooShippingAction.createPackage(siteID: self.sampleSiteID,
customPackage: .fake(),
predefinedOption: .fake()) { result in
promise(result.isSuccess)
}
store.onAction(action)
}

// Then
XCTAssertTrue(onSuccess)
let storedPackages = try XCTUnwrap(storageManager.viewStorage.firstObject(ofType: StorageWooShippingPackagesResponse.self)).toReadOnly()
XCTAssertEqual(storedPackages.siteID, sampleSiteID)
XCTAssertEqual(storedPackages.customPackages.count, 5)
XCTAssertEqual(storedPackages.savedPredefinedPackages.count, 2)
}

// MARK: `deletePackage`

func test_deletePackage_returns_success_response() throws {
Expand Down Expand Up @@ -121,38 +152,49 @@ final class WooShippingStoreTests: XCTestCase {
XCTAssertTrue(result.isFailure)
}

func test_createPackage_when_successful_then_upserts_packages_into_storage() throws {
func test_deletePackage_when_successful_then_upserts_packages_into_storage() throws {
// Given
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
network.simulateResponse(requestUrlSuffix: "packages", filename: "wooshipping-create-package-success")
let remote = MockWooShippingRemote()
let response = WooShippingCreatePackageResponse.fake().copy(customPackages: [WooShippingCustomPackage.fake().copy(id: "2")],
predefinedOptions: [.init(id: "usps", predefinedPackageIDs: ["small_flat_box"])])
remote.whenDeletePackage(siteID: sampleSiteID, thenReturn: .success(response))
storageManager.insertSamplePackages(readOnlyPackages: .init(siteID: sampleSiteID,
customPackages: [],
savedPredefinedPackages: [],
customPackages: [WooShippingCustomPackage.fake().copy(id: "1"),
WooShippingCustomPackage.fake().copy(id: "2")],
savedPredefinedPackages: [.init(groupTitle: "pri_flat_boxes",
providerID: "usps",
package: .init(id: "small_flat_box",
name: "",
isLetter: false,
dimensions: "",
boxWeight: "",
groupId: "")),
.init(groupTitle: "pri_flat_boxes",
providerID: "usps",
package: .init(id: "medium_flat_box_top",
name: "",
isLetter: false,
dimensions: "",
boxWeight: "",
groupId: ""))],
allPredefinedOptions: [sampleCarrierPredefinedOptions()]))

// Confidence check
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingCustomPackage.self), 0)
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingSavedPredefinedPackage.self), 0)
let store = WooShippingStore(dispatcher: dispatcher, storageManager: storageManager, network: network, remote: remote)

// When
let onSuccess: Bool = waitFor { promise in
let action = WooShippingAction.createPackage(siteID: self.sampleSiteID,
customPackage: .fake(),
predefinedOption: .fake()) { result in
let action = WooShippingAction.deletePackage(siteID: self.sampleSiteID,
packageID: WooShippingCustomPackage.fake().id) { result in
promise(result.isSuccess)
}
store.onAction(action)
}

let storedPackages = try XCTUnwrap(storageManager.viewStorage.firstObject(ofType: StorageWooShippingPackagesResponse.self)).toReadOnly()

// Then
XCTAssertTrue(onSuccess)
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingCustomPackage.self), 5)
XCTAssertEqual(storageManager.viewStorage.countObjects(ofType: StorageWooShippingSavedPredefinedPackage.self), 2)
let storedPackages = try XCTUnwrap(storageManager.viewStorage.firstObject(ofType: StorageWooShippingPackagesResponse.self)).toReadOnly()
XCTAssertEqual(storedPackages.siteID, sampleSiteID)
XCTAssertEqual(storedPackages.customPackages.count, 5)
XCTAssertEqual(storedPackages.savedPredefinedPackages.count, 2)
XCTAssertEqual(storedPackages.customPackages.count, 1)
XCTAssertEqual(storedPackages.savedPredefinedPackages.count, 1)
}

// MARK: `loadLabelRates`
Expand Down

0 comments on commit 0a7021f

Please sign in to comment.