Skip to content

Commit 8983f0c

Browse files
committed
Update tests for AddressEditViewModel
1 parent 813ace0 commit 8983f0c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

WooCommerce/WooCommerceTests/ViewRelated/Orders/Order Details/Addresses/EditAddressFormViewModelTests.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
import XCTest
22
import Yosemite
33
import TestKit
4+
import Combine
45
@testable import WooCommerce
56

67
final class EditAddressFormViewModelTests: XCTestCase {
78

89
let sampleSiteID: Int64 = 123
910

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+
1025
func test_creating_with_address_prefills_fields_with_correct_data() {
1126
// Given
1227
let address = sampleAddress()
@@ -72,6 +87,53 @@ final class EditAddressFormViewModelTests: XCTestCase {
7287
// Then
7388
XCTAssertFalse(viewModel.isDoneButtonEnabled)
7489
}
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+
}
75137
}
76138

77139
private extension EditAddressFormViewModelTests {

0 commit comments

Comments
 (0)