Skip to content

Commit

Permalink
Handle origin address selection
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelmcr committed Jan 8, 2025
1 parent 32559f8 commit a3611c5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ final class WooShippingOriginAddressListViewModel: ObservableObject {
let addresses: [WooShippingOriginAddress]
@Published private(set) var selectedAddressID: String?

/// Closure (set externally) called when an address is selected.
var onSelect: ((WooShippingOriginAddress) -> Void)?

init(addresses: [WooShippingOriginAddress],
selectedAddressID: String? = nil) {
self.addresses = addresses
Expand All @@ -22,6 +25,7 @@ final class WooShippingOriginAddressListViewModel: ObservableObject {
return
}
selectedAddressID = address.id
onSelect?(address)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ private extension WooShippingCreateLabelsViewModel {
switch result {
case .success(let addresses):
selectedOriginAddress = addresses.first(where: \.defaultAddress)
originAddresses = WooShippingOriginAddressListViewModel(addresses: addresses, selectedAddressID: selectedOriginAddress?.id)
originAddresses = WooShippingOriginAddressListViewModel(addresses: addresses,
selectedAddressID: selectedOriginAddress?.id)
originAddresses.onSelect = { [weak self] selectedAddress in
self?.selectedOriginAddress = selectedAddress
}
case .failure(let error):
DDLogError("⛔️ Error loading origin addresses for Woo Shipping labels: \(error)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ final class WooShippingOriginAddressListViewModelTests: XCTestCase {
XCTAssertEqual(viewModel.selectedAddressID, addressToSelect.id)
}

func test_select_calls_onSelect_closure() {
// Given
let addressToSelect = WooShippingOriginAddress.fake().copy(id: "1")
let addresses = [addressToSelect, WooShippingOriginAddress.fake().copy(id: "2")]
let viewModel = WooShippingOriginAddressListViewModel(addresses: addresses, selectedAddressID: nil)

// When
let selectedAddress = waitFor { promise in
viewModel.onSelect = { address in
promise(address)
}
viewModel.select(addressToSelect)
}

// Then
XCTAssertEqual(selectedAddress, addressToSelect)
}

}

0 comments on commit a3611c5

Please sign in to comment.