From 6d99303697dcbe3e38402223f9652da57ece87fa Mon Sep 17 00:00:00 2001 From: Rachel McRoberts Date: Wed, 15 Jan 2025 14:57:47 +0000 Subject: [PATCH] Set status if there are any invalid fields --- .../WooShippingEditAddressViewModel.swift | 18 +++++++++++++++--- .../WooShippingEditAddressViewModelTests.swift | 12 ++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift index fa59a8cd92a..f46795048f1 100644 --- a/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift +++ b/WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShippingAddresses/WooShippingEditAddressViewModel.swift @@ -45,15 +45,27 @@ final class WooShippingEditAddressViewModel: ObservableObject, Identifiable { // MARK: Local requirements & validation + /// Whether the address has been remotely verified. + private var isVerified: Bool + /// Fields that are invalid based on local validation. @Published private(set) var invalidFields: [WooShippingEditAddressView.AddressField] = [] /// Whether the phone number is required. private let phoneNumberRequired: Bool - // TODO: Set status based on initial verified status, whether any changes have been made, and local validation. + // TODO: Set status to unverified if the address was verified remotely but there are unsaved changes. /// Status of the address, based on local validation and remote verification. - var status: WooShippingAddressStatus + var status: WooShippingAddressStatus { + switch (isVerified, invalidFields.isEmpty) { + case (true, true): + return .verified + case (false, true): + return .unverified + case (_, false): + return .missingInformation + } + } // MARK: State/Country @@ -143,7 +155,7 @@ final class WooShippingEditAddressViewModel: ObservableObject, Identifiable { self.phone = phone self.isDefaultAddress = isDefaultAddress self.showCompanyField = showCompanyField - self.status = isVerified ? .verified : .unverified + self.isVerified = isVerified self.phoneNumberRequired = phoneNumberRequired self.stores = stores self.siteID = stores.sessionManager.defaultStoreID ?? Int64.min diff --git a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift index ca9f590da2b..41ca4c9e1cb 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/Shipping Label/WooShipping Create Shipping Labels/WooShippingEditAddressViewModelTests.swift @@ -435,7 +435,7 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { XCTAssertNotNil(viewModel.selectedState) } - func test_validateAddress_sets_empty_invalidFields_when_all_fields_valid() { + func test_validateAddress_sets_expected_properties_when_all_fields_valid() { // Given let storageManager = MockStorageManager() let country = Country(code: "US", name: "United States", states: [StateOfACountry(code: "NY", name: "New York")]) @@ -462,9 +462,10 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { // Then XCTAssertTrue(viewModel.invalidFields.isEmpty) + XCTAssertEqual(viewModel.status, .verified) } - func test_validateAddress_sets_expected_invalidFields_when_all_fields_empty() { + func test_validateAddress_sets_expected_properties_when_all_fields_empty() { // Given let viewModel = WooShippingEditAddressViewModel(type: .destination, id: "", @@ -489,9 +490,10 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { // Note that empty state is valid when country is empty (has no states). let expectedInvalidFields = WooShippingEditAddressView.AddressField.allCases.filter { $0 != .state } XCTAssertEqual(viewModel.invalidFields, expectedInvalidFields) + XCTAssertEqual(viewModel.status, .missingInformation) } - func test_validate_sets_empty_invalidFields_when_all_fields_valid() { + func test_validate_sets_expected_properties_when_all_fields_valid() { // Given let storageManager = MockStorageManager() let country = Country(code: "US", name: "United States", states: [StateOfACountry(code: "NY", name: "New York")]) @@ -520,9 +522,10 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { // Then XCTAssertTrue(viewModel.invalidFields.isEmpty) + XCTAssertEqual(viewModel.status, .verified) } - func test_validate_sets_expected_invalidFields_when_all_fields_empty() { + func test_validate_sets_expected_properties_when_all_fields_empty() { // Given let viewModel = WooShippingEditAddressViewModel(type: .destination, id: "", @@ -549,6 +552,7 @@ final class WooShippingEditAddressViewModelTests: XCTestCase { // Note that empty state is valid when country is empty (has no states). let expectedInvalidFields = WooShippingEditAddressView.AddressField.allCases.filter { $0 != .state } XCTAssertEqual(viewModel.invalidFields, expectedInvalidFields) + XCTAssertEqual(viewModel.status, .missingInformation) } func test_validate_sets_state_as_invalid_field_when_empty_and_country_contains_states() {