Skip to content

Commit bd7482e

Browse files
committed
Add unit tests
1 parent acd60f0 commit bd7482e

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

WooCommerce/Classes/ViewRelated/Products/Variations/ProductVariationsViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class ProductVariationsViewModel {
4040
print("Variations to Generate: \(variationsToGenerate.count)")
4141

4242
// Guard for 100 variation limit
43-
guard variationsToGenerate.count < 100 else {
43+
guard variationsToGenerate.count <= 100 else {
4444
return onCompletion(.failure(.tooManyVariations(variationCount: variationsToGenerate.count)))
4545
}
4646

@@ -82,7 +82,7 @@ extension ProductVariationsViewModel {
8282
extension ProductVariationsViewModel {
8383
/// Type to represent known generation errors
8484
///
85-
enum GenerationError: LocalizedError {
85+
enum GenerationError: LocalizedError, Equatable {
8686
case tooManyVariations(variationCount: Int)
8787

8888
var errorTitle: String {

WooCommerce/WooCommerceTests/ViewRelated/Products/Variations/ProductVariationsViewModelTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,27 @@ final class ProductVariationsViewModelTests: XCTestCase {
114114
XCTAssertTrue(product.existsRemotely)
115115
XCTAssertEqual(viewModel.formType, .readonly)
116116
}
117+
118+
func test_trying_to_generate_more_than_100_variations_will_return_error() {
119+
// Given
120+
let product = Product.fake().copy(attributes: [
121+
ProductAttribute.fake().copy(attributeID: 1, name: "Size", options: ["XS", "S", "M", "L", "XL"]),
122+
ProductAttribute.fake().copy(attributeID: 2, name: "Color", options: ["Red", "Green", "Blue", "White", "Black"]),
123+
ProductAttribute.fake().copy(attributeID: 3, name: "Fabric", options: ["Cotton", "Nylon", "Polyester", "Silk", "Linen"]),
124+
])
125+
let viewModel = ProductVariationsViewModel(formType: .edit)
126+
127+
// When
128+
let error = waitFor { promise in
129+
viewModel.generateAllVariations(for: product) { result in
130+
if case let .failure(error) = result {
131+
promise(error)
132+
}
133+
}
134+
}
135+
136+
// Then
137+
XCTAssertEqual(error, .tooManyVariations(variationCount: 125))
138+
139+
}
117140
}

0 commit comments

Comments
 (0)