|
| 1 | +import XCTest |
| 2 | +import Yosemite |
| 3 | +@testable import WooCommerce |
| 4 | +@testable import Storage |
| 5 | + |
| 6 | +class AddProductVariationToOrderViewModelTests: XCTestCase { |
| 7 | + |
| 8 | + private let sampleSiteID: Int64 = 123 |
| 9 | + private let sampleProductID: Int64 = 12 |
| 10 | + private var storageManager: StorageManagerType! |
| 11 | + private var storage: StorageType { |
| 12 | + storageManager.viewStorage |
| 13 | + } |
| 14 | + private let stores = MockStoresManager(sessionManager: .testingInstance) |
| 15 | + |
| 16 | + override func setUp() { |
| 17 | + super.setUp() |
| 18 | + storageManager = MockStorageManager() |
| 19 | + stores.reset() |
| 20 | + } |
| 21 | + |
| 22 | + override func tearDown() { |
| 23 | + storageManager = nil |
| 24 | + super.tearDown() |
| 25 | + } |
| 26 | + |
| 27 | + func test_view_model_adds_product_rows_with_unchangeable_quantity() { |
| 28 | + // Given |
| 29 | + let product = Product.fake().copy(productID: sampleProductID) |
| 30 | + let productVariation = ProductVariation.fake().copy(siteID: sampleSiteID, productID: sampleProductID, purchasable: true) |
| 31 | + insert(productVariation) |
| 32 | + |
| 33 | + // When |
| 34 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager) |
| 35 | + |
| 36 | + // Then |
| 37 | + XCTAssertEqual(viewModel.productRows.count, 1) |
| 38 | + |
| 39 | + let productRow = viewModel.productRows[0] |
| 40 | + XCTAssertFalse(productRow.canChangeQuantity, "Product row canChangeQuantity property should be false but is true instead") |
| 41 | + } |
| 42 | + |
| 43 | + func test_product_rows_only_include_purchasable_product_variations() { |
| 44 | + // Given |
| 45 | + let product = Product.fake().copy(productID: sampleProductID) |
| 46 | + let purchasableProductVariation = ProductVariation.fake().copy(siteID: sampleSiteID, |
| 47 | + productID: sampleProductID, |
| 48 | + productVariationID: 1, |
| 49 | + purchasable: true) |
| 50 | + let nonPurchasableProductVariation = ProductVariation.fake().copy(siteID: sampleSiteID, productVariationID: 2, purchasable: false) |
| 51 | + insert([purchasableProductVariation, nonPurchasableProductVariation]) |
| 52 | + |
| 53 | + // When |
| 54 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager) |
| 55 | + |
| 56 | + // Then |
| 57 | + XCTAssertTrue(viewModel.productRows.contains(where: { $0.productOrVariationID == 1 }), "Product rows do not include purchasable product variation") |
| 58 | + XCTAssertFalse(viewModel.productRows.contains(where: { $0.productOrVariationID == 2 }), "Product rows include non-purchasable product variation") |
| 59 | + } |
| 60 | + |
| 61 | + func test_scrolling_indicator_appears_only_during_sync() { |
| 62 | + // Given |
| 63 | + let product = Product.fake() |
| 64 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager, stores: stores) |
| 65 | + XCTAssertFalse(viewModel.shouldShowScrollIndicator, "Scroll indicator is not disabled at start") |
| 66 | + stores.whenReceivingAction(ofType: ProductVariationAction.self) { action in |
| 67 | + switch action { |
| 68 | + case let .synchronizeProductVariations(_, _, _, _, onCompletion): |
| 69 | + XCTAssertTrue(viewModel.shouldShowScrollIndicator, "Scroll indicator is not enabled during sync") |
| 70 | + onCompletion(nil) |
| 71 | + default: |
| 72 | + XCTFail("Unsupported Action") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // When |
| 77 | + viewModel.sync(pageNumber: 1, pageSize: 25, onCompletion: { _ in }) |
| 78 | + |
| 79 | + // Then |
| 80 | + XCTAssertFalse(viewModel.shouldShowScrollIndicator, "Scroll indicator is not disabled after sync ends") |
| 81 | + } |
| 82 | + |
| 83 | + func test_sync_status_updates_as_expected_for_empty_product_variation_list() { |
| 84 | + // Given |
| 85 | + let product = Product.fake().copy(productID: sampleProductID) |
| 86 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager, stores: stores) |
| 87 | + stores.whenReceivingAction(ofType: ProductVariationAction.self) { action in |
| 88 | + switch action { |
| 89 | + case let .synchronizeProductVariations(_, _, _, _, onCompletion): |
| 90 | + XCTAssertEqual(viewModel.syncStatus, .firstPageSync) |
| 91 | + onCompletion(nil) |
| 92 | + default: |
| 93 | + XCTFail("Unsupported Action") |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + // When |
| 98 | + viewModel.sync(pageNumber: 1, pageSize: 25, onCompletion: { _ in }) |
| 99 | + |
| 100 | + // Then |
| 101 | + XCTAssertEqual(viewModel.syncStatus, .empty) |
| 102 | + } |
| 103 | + |
| 104 | + func test_sync_status_updates_as_expected_when_product_variations_are_synced() { |
| 105 | + // Given |
| 106 | + let product = Product.fake().copy(productID: sampleProductID) |
| 107 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager, stores: stores) |
| 108 | + stores.whenReceivingAction(ofType: ProductVariationAction.self) { action in |
| 109 | + switch action { |
| 110 | + case let .synchronizeProductVariations(_, _, _, _, onCompletion): |
| 111 | + XCTAssertEqual(viewModel.syncStatus, .firstPageSync) |
| 112 | + let productVariation = ProductVariation.fake().copy(siteID: self.sampleSiteID, productID: self.sampleProductID, purchasable: true) |
| 113 | + self.insert(productVariation) |
| 114 | + onCompletion(nil) |
| 115 | + default: |
| 116 | + XCTFail("Unsupported Action") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // When |
| 121 | + viewModel.sync(pageNumber: 1, pageSize: 25, onCompletion: { _ in }) |
| 122 | + |
| 123 | + // Then |
| 124 | + XCTAssertEqual(viewModel.syncStatus, .results) |
| 125 | + } |
| 126 | + |
| 127 | + func test_sync_status_does_not_change_while_syncing_when_storage_contains_product_variations() { |
| 128 | + // Given |
| 129 | + let product = Product.fake().copy(productID: sampleProductID) |
| 130 | + let productVariation = ProductVariation.fake().copy(siteID: sampleSiteID, productID: sampleProductID, purchasable: true) |
| 131 | + insert(productVariation) |
| 132 | + |
| 133 | + let viewModel = AddProductVariationToOrderViewModel(siteID: sampleSiteID, product: product, storageManager: storageManager, stores: stores) |
| 134 | + stores.whenReceivingAction(ofType: ProductVariationAction.self) { action in |
| 135 | + switch action { |
| 136 | + case let .synchronizeProductVariations(_, _, _, _, onCompletion): |
| 137 | + XCTAssertEqual(viewModel.syncStatus, .results) |
| 138 | + onCompletion(nil) |
| 139 | + default: |
| 140 | + XCTFail("Unsupported Action") |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // When |
| 145 | + viewModel.sync(pageNumber: 1, pageSize: 25, onCompletion: { _ in }) |
| 146 | + |
| 147 | + // Then |
| 148 | + XCTAssertEqual(viewModel.syncStatus, .results) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// MARK: - Utils |
| 153 | +private extension AddProductVariationToOrderViewModelTests { |
| 154 | + /// Insert a `ProductVariation` into storage |
| 155 | + func insert(_ readOnlyProduct: Yosemite.ProductVariation) { |
| 156 | + let product = storage.insertNewObject(ofType: StorageProductVariation.self) |
| 157 | + product.update(with: readOnlyProduct) |
| 158 | + } |
| 159 | + |
| 160 | + /// Insert an array of `ProductVariation`s into storage |
| 161 | + func insert(_ readOnlyProducts: [Yosemite.ProductVariation]) { |
| 162 | + for readOnlyProduct in readOnlyProducts { |
| 163 | + let product = storage.insertNewObject(ofType: StorageProductVariation.self) |
| 164 | + product.update(with: readOnlyProduct) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments