@@ -137,8 +137,8 @@ final class ProductVariationsViewModelTests: XCTestCase {
137137
138138 // When
139139 let error = waitFor { promise in
140- viewModel. generateAllVariations ( for: product) { result in
141- if case let . failure ( error) = result {
140+ viewModel. generateAllVariations ( for: product) { state in
141+ if case let . error ( error) = state {
142142 promise ( error)
143143 }
144144 }
@@ -148,7 +148,7 @@ final class ProductVariationsViewModelTests: XCTestCase {
148148 XCTAssertEqual ( error, . tooManyVariations( variationCount: 125 ) )
149149 }
150150
151- func test_generating_less_than_100_variations_invokes_create_action ( ) {
151+ func test_generating_less_than_100_variations_ask_for_confirmation_and_creates_variations ( ) {
152152 // Given
153153 let product = Product . fake ( ) . copy ( attributes: [
154154 ProductAttribute . fake ( ) . copy ( attributeID: 1 , name: " Size " , options: [ " XS " , " S " , " M " , " L " , " XL " ] ) ,
@@ -171,8 +171,11 @@ final class ProductVariationsViewModelTests: XCTestCase {
171171
172172 // When
173173 let succeeded = waitFor { promise in
174- viewModel. generateAllVariations ( for: product) { result in
175- if case . success = result {
174+ viewModel. generateAllVariations ( for: product) { state in
175+ if case let . confirmation( _, onCompletion) = state {
176+ onCompletion ( true )
177+ }
178+ if case . finished = state {
176179 promise ( true )
177180 }
178181 }
@@ -181,4 +184,41 @@ final class ProductVariationsViewModelTests: XCTestCase {
181184 // Then
182185 XCTAssertTrue ( succeeded)
183186 }
187+
188+ func test_generating_less_than_100_variations_ask_for_confirmation_and_sends_cancel_state( ) {
189+ // Given
190+ let product = Product . fake ( ) . copy ( attributes: [
191+ ProductAttribute . fake ( ) . copy ( attributeID: 1 , name: " Size " , options: [ " XS " , " S " , " M " , " L " , " XL " ] ) ,
192+ ProductAttribute . fake ( ) . copy ( attributeID: 2 , name: " Color " , options: [ " Red " , " Green " , " Blue " , " White " , " Black " ] ) ,
193+ ] )
194+
195+ let stores = MockStoresManager ( sessionManager: SessionManager . makeForTesting ( ) )
196+ stores. whenReceivingAction ( ofType: ProductVariationAction . self) { action in
197+ switch action {
198+ case . synchronizeAllProductVariations( _, _, let onCompletion) :
199+ onCompletion ( . success( [ ] ) )
200+ case . createProductVariations( _, _, _, let onCompletion) :
201+ onCompletion ( . success( [ ] ) )
202+ default :
203+ break
204+ }
205+ }
206+
207+ let viewModel = ProductVariationsViewModel ( stores: stores, formType: . edit)
208+
209+ // When
210+ let canceled = waitFor { promise in
211+ viewModel. generateAllVariations ( for: product) { state in
212+ if case let . confirmation( _, onCompletion) = state {
213+ onCompletion ( false )
214+ }
215+ if case . canceled = state {
216+ promise ( true )
217+ }
218+ }
219+ }
220+
221+ // Then
222+ XCTAssertTrue ( canceled)
223+ }
184224}
0 commit comments