Skip to content

Commit

Permalink
Set status if there are any invalid fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelmcr committed Jan 15, 2025
1 parent 68c066a commit 6d99303
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")])
Expand All @@ -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: "",
Expand All @@ -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")])
Expand Down Expand Up @@ -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: "",
Expand All @@ -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() {
Expand Down

0 comments on commit 6d99303

Please sign in to comment.