|
| 1 | +import XCTest |
| 2 | +@testable import WooCommerce |
| 3 | +@testable import Yosemite |
| 4 | + |
| 5 | +final class ProductVariationGeneratorTests: XCTestCase { |
| 6 | + |
| 7 | + func test_all_variations_are_generated_correctly() { |
| 8 | + // Given |
| 9 | + let product = Product.fake().copy(attributes: [ |
| 10 | + ProductAttribute.fake().copy(attributeID: 1, name: "Size", options: ["S", "M"]), |
| 11 | + ProductAttribute.fake().copy(attributeID: 2, name: "Color", options: ["Red", "Green"]), |
| 12 | + ProductAttribute.fake().copy(attributeID: 3, name: "Fabric", options: ["Cotton", "Nylon"]), |
| 13 | + ]) |
| 14 | + |
| 15 | + // When |
| 16 | + let variations = ProductVariationGenerator.generateVariations(for: product, excluding: []) |
| 17 | + |
| 18 | + // Then |
| 19 | + XCTAssertEqual(variations, [ |
| 20 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 21 | + .init(id: 1, name: "Size", option: "S"), |
| 22 | + .init(id: 2, name: "Color", option: "Red"), |
| 23 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 24 | + ]), |
| 25 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 26 | + .init(id: 1, name: "Size", option: "S"), |
| 27 | + .init(id: 2, name: "Color", option: "Red"), |
| 28 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 29 | + ]), |
| 30 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 31 | + .init(id: 1, name: "Size", option: "S"), |
| 32 | + .init(id: 2, name: "Color", option: "Green"), |
| 33 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 34 | + ]), |
| 35 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 36 | + .init(id: 1, name: "Size", option: "S"), |
| 37 | + .init(id: 2, name: "Color", option: "Green"), |
| 38 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 39 | + ]), |
| 40 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 41 | + .init(id: 1, name: "Size", option: "M"), |
| 42 | + .init(id: 2, name: "Color", option: "Red"), |
| 43 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 44 | + ]), |
| 45 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 46 | + .init(id: 1, name: "Size", option: "M"), |
| 47 | + .init(id: 2, name: "Color", option: "Red"), |
| 48 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 49 | + ]), |
| 50 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 51 | + .init(id: 1, name: "Size", option: "M"), |
| 52 | + .init(id: 2, name: "Color", option: "Green"), |
| 53 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 54 | + ]), |
| 55 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 56 | + .init(id: 1, name: "Size", option: "M"), |
| 57 | + .init(id: 2, name: "Color", option: "Green"), |
| 58 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 59 | + ]), |
| 60 | + ]) |
| 61 | + } |
| 62 | + |
| 63 | + func test_existing_variations_are_excluded_correctly() { |
| 64 | + // Given |
| 65 | + let product = Product.fake().copy(attributes: [ |
| 66 | + ProductAttribute.fake().copy(attributeID: 1, name: "Size", options: ["S", "M"]), |
| 67 | + ProductAttribute.fake().copy(attributeID: 2, name: "Color", options: ["Red", "Green"]), |
| 68 | + ProductAttribute.fake().copy(attributeID: 3, name: "Fabric", options: ["Cotton", "Nylon"]), |
| 69 | + ]) |
| 70 | + |
| 71 | + let existingVariations = [ |
| 72 | + ProductVariation.fake().copy(attributes: [ |
| 73 | + .init(id: 1, name: "Size", option: "M"), |
| 74 | + .init(id: 2, name: "Color", option: "Green"), |
| 75 | + .init(id: 3, name: "Fabric", option: "Cotton"), |
| 76 | + ]), |
| 77 | + ProductVariation.fake().copy(attributes: [ |
| 78 | + .init(id: 1, name: "Size", option: "S"), |
| 79 | + .init(id: 2, name: "Color", option: "Red"), |
| 80 | + .init(id: 3, name: "Fabric", option: "Nylon"), |
| 81 | + ]) |
| 82 | + ] |
| 83 | + |
| 84 | + // When |
| 85 | + let variations = ProductVariationGenerator.generateVariations(for: product, excluding: existingVariations) |
| 86 | + |
| 87 | + // Then |
| 88 | + XCTAssertEqual(variations, [ |
| 89 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 90 | + .init(id: 1, name: "Size", option: "S"), |
| 91 | + .init(id: 2, name: "Color", option: "Red"), |
| 92 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 93 | + ]), |
| 94 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 95 | + .init(id: 1, name: "Size", option: "S"), |
| 96 | + .init(id: 2, name: "Color", option: "Green"), |
| 97 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 98 | + ]), |
| 99 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 100 | + .init(id: 1, name: "Size", option: "S"), |
| 101 | + .init(id: 2, name: "Color", option: "Green"), |
| 102 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 103 | + ]), |
| 104 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 105 | + .init(id: 1, name: "Size", option: "M"), |
| 106 | + .init(id: 2, name: "Color", option: "Red"), |
| 107 | + .init(id: 3, name: "Fabric", option: "Cotton") |
| 108 | + ]), |
| 109 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 110 | + .init(id: 1, name: "Size", option: "M"), |
| 111 | + .init(id: 2, name: "Color", option: "Red"), |
| 112 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 113 | + ]), |
| 114 | + CreateProductVariation(regularPrice: "", attributes: [ |
| 115 | + .init(id: 1, name: "Size", option: "M"), |
| 116 | + .init(id: 2, name: "Color", option: "Green"), |
| 117 | + .init(id: 3, name: "Fabric", option: "Nylon") |
| 118 | + ]), |
| 119 | + ]) |
| 120 | + } |
| 121 | +} |
0 commit comments