|
1 | 1 | import XCTest |
2 | 2 | import Yosemite |
3 | 3 | import TestKit |
| 4 | +import Combine |
4 | 5 | @testable import WooCommerce |
5 | 6 |
|
6 | 7 | final class EditAddressFormViewModelTests: XCTestCase { |
7 | 8 |
|
8 | 9 | let sampleSiteID: Int64 = 123 |
9 | 10 |
|
| 11 | + let testingStorage = MockStorageManager() |
| 12 | + |
| 13 | + let testingStores = MockStoresManager(sessionManager: .testingInstance) |
| 14 | + |
| 15 | + var subscriptions = Set<AnyCancellable>() |
| 16 | + |
| 17 | + override func setUp() { |
| 18 | + super.setUp() |
| 19 | + |
| 20 | + testingStorage.reset() |
| 21 | + testingStores.reset() |
| 22 | + subscriptions.removeAll() |
| 23 | + } |
| 24 | + |
10 | 25 | func test_creating_with_address_prefills_fields_with_correct_data() { |
11 | 26 | // Given |
12 | 27 | let address = sampleAddress() |
@@ -72,6 +87,53 @@ final class EditAddressFormViewModelTests: XCTestCase { |
72 | 87 | // Then |
73 | 88 | XCTAssertFalse(viewModel.isDoneButtonEnabled) |
74 | 89 | } |
| 90 | + |
| 91 | + func test_starting_view_model_without_stored_countries_fetches_them_remotely() { |
| 92 | + // Given |
| 93 | + let viewModel = EditAddressFormViewModel(siteID: sampleSiteID, address: sampleAddress(), storageManager: testingStorage, stores: testingStores) |
| 94 | + |
| 95 | + // When |
| 96 | + let countriesFetched: Bool = waitFor { promise in |
| 97 | + self.testingStores.whenReceivingAction(ofType: DataAction.self) { action in |
| 98 | + switch action { |
| 99 | + case .synchronizeCountries: |
| 100 | + promise(true) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + viewModel.onLoadTrigger.send() |
| 105 | + } |
| 106 | + |
| 107 | + // Then |
| 108 | + XCTAssertTrue(countriesFetched) |
| 109 | + } |
| 110 | + |
| 111 | + func test_syncing_countries_correctly_sets_showPlaceholders_properties() { |
| 112 | + // Given |
| 113 | + let viewModel = EditAddressFormViewModel(siteID: sampleSiteID, address: sampleAddress(), storageManager: testingStorage, stores: testingStores) |
| 114 | + testingStores.whenReceivingAction(ofType: DataAction.self) { action in |
| 115 | + switch action { |
| 116 | + case .synchronizeCountries(_, let completion): |
| 117 | + completion(.success([])) // Sending an empty because we don't really care about countries on this test. |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // When |
| 122 | + let showPlaceholdersStates: [Bool] = waitFor { promise in |
| 123 | + viewModel.$showPlaceholders |
| 124 | + .dropFirst() // Drop initial value |
| 125 | + .collect(2) // Expect two state changes |
| 126 | + .sink { emittedValues in |
| 127 | + promise(emittedValues) |
| 128 | + } |
| 129 | + .store(in: &self.subscriptions) |
| 130 | + |
| 131 | + viewModel.onLoadTrigger.send() |
| 132 | + } |
| 133 | + |
| 134 | + // Then |
| 135 | + assertEqual(showPlaceholdersStates, [true, false]) // true: showPlaceholders, false: hide placeholders |
| 136 | + } |
75 | 137 | } |
76 | 138 |
|
77 | 139 | private extension EditAddressFormViewModelTests { |
|
0 commit comments