@@ -24,10 +24,15 @@ class AddProductVariationToOrderViewModelTests: XCTestCase {
2424 super. tearDown ( )
2525 }
2626
27- func test_view_model_adds_product_variation_rows_with_unchangeable_quantity ( ) {
27+ func test_view_model_adds_product_variation_rows_with_expected_values ( ) {
2828 // Given
29- let product = Product . fake ( ) . copy ( productID: sampleProductID)
30- let productVariation = ProductVariation . fake ( ) . copy ( siteID: sampleSiteID, productID: sampleProductID, purchasable: true )
29+ let product = Product . fake ( ) . copy ( productID: sampleProductID,
30+ attributes: [ ProductAttribute . fake ( ) . copy ( siteID: sampleSiteID, attributeID: 1 , name: " Color " , variation: true ) ,
31+ ProductAttribute . fake ( ) . copy ( siteID: sampleSiteID, attributeID: 2 , name: " Size " , variation: true ) ] )
32+ let productVariation = ProductVariation . fake ( ) . copy ( siteID: sampleSiteID,
33+ productID: sampleProductID,
34+ attributes: [ ProductVariationAttribute ( id: 1 , name: " Color " , option: " Blue " ) ] ,
35+ purchasable: true )
3136 insert ( productVariation)
3237
3338 // When
@@ -39,6 +44,7 @@ class AddProductVariationToOrderViewModelTests: XCTestCase {
3944 let productVariationRow = viewModel. productVariationRows [ 0 ]
4045 XCTAssertFalse ( productVariationRow. canChangeQuantity,
4146 " Product variation row canChangeQuantity property should be false but is true instead " )
47+ XCTAssertEqual ( productVariationRow. name, " Blue - Any Size " )
4248 }
4349
4450 func test_product_variation_rows_only_include_purchasable_product_variations( ) {
@@ -61,20 +67,6 @@ class AddProductVariationToOrderViewModelTests: XCTestCase {
6167 " Product variation rows include non-purchasable product variation " )
6268 }
6369
64- func test_createVariationName_creates_expected_name_for_product_variation_rows( ) {
65- // Given
66- let product = Product . fake ( ) . copy ( attributes: [ ProductAttribute . fake ( ) . copy ( siteID: sampleSiteID, attributeID: 1 , name: " Color " , variation: true ) ,
67- ProductAttribute . fake ( ) . copy ( siteID: sampleSiteID, attributeID: 2 , name: " Size " , variation: true ) ] )
68- let viewModel = AddProductVariationToOrderViewModel ( siteID: sampleSiteID, product: product)
69- let productVariation = ProductVariation . fake ( ) . copy ( attributes: [ ProductVariationAttribute ( id: 1 , name: " Color " , option: " Blue " ) ] )
70-
71- // When
72- let variationName = viewModel. createVariationName ( for: productVariation)
73-
74- // Then
75- XCTAssertEqual ( variationName, " Blue - Any Size " )
76- }
77-
7870 func test_scrolling_indicator_appears_only_during_sync( ) {
7971 // Given
8072 let product = Product . fake ( )
@@ -190,16 +182,24 @@ class AddProductVariationToOrderViewModelTests: XCTestCase {
190182// MARK: - Utils
191183private extension AddProductVariationToOrderViewModelTests {
192184 /// Insert a `ProductVariation` into storage
193- func insert( _ readOnlyProduct: Yosemite . ProductVariation ) {
194- let product = storage. insertNewObject ( ofType: StorageProductVariation . self)
195- product. update ( with: readOnlyProduct)
185+ func insert( _ readOnlyVariation: Yosemite . ProductVariation ) {
186+ let productVariation = storage. insertNewObject ( ofType: StorageProductVariation . self)
187+ productVariation. update ( with: readOnlyVariation)
188+
189+ // Inserts the attributes from the read-only product variation.
190+ var storageAttributes = [ StorageAttribute] ( )
191+ for readOnlyAttribute in readOnlyVariation. attributes {
192+ let newStorageAttribute = storage. insertNewObject ( ofType: Storage . GenericAttribute. self)
193+ newStorageAttribute. update ( with: readOnlyAttribute)
194+ storageAttributes. append ( newStorageAttribute)
195+ }
196+ productVariation. attributes = NSOrderedSet ( array: storageAttributes)
196197 }
197198
198199 /// Insert an array of `ProductVariation`s into storage
199- func insert( _ readOnlyProducts: [ Yosemite . ProductVariation ] ) {
200- for readOnlyProduct in readOnlyProducts {
201- let product = storage. insertNewObject ( ofType: StorageProductVariation . self)
202- product. update ( with: readOnlyProduct)
200+ func insert( _ readOnlyVariations: [ Yosemite . ProductVariation ] ) {
201+ for readOnlyVariation in readOnlyVariations {
202+ insert ( readOnlyVariation)
203203 }
204204 }
205205}
0 commit comments