Skip to content

Commit fc4f068

Browse files
committed
Add method to remove a product identity and make it look like a new product
1 parent fb7325e commit fc4f068

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

WooCommerce/Classes/ViewRelated/Products/Add Product/ProductFactory.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ struct ProductFactory {
1616
return nil
1717
}
1818
}
19+
20+
/// Copies a product by cleaning properties like `id, name, and statusKey` to their default state.
21+
/// This is usefult to turn an existing(on core) `auto-draft` product into a new app-product ready to be saved.
22+
///
23+
func newProduct(from existingProduct: Product) -> Product {
24+
existingProduct.copy(productID: 0, name: "", statusKey: ProductStatus.published.rawValue)
25+
}
1926
}
2027

2128
private extension ProductFactory {

WooCommerce/WooCommerceTests/ViewRelated/Products/Add Product/ProductFactoryTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import XCTest
2+
import Fakes
3+
import Networking
24
@testable import WooCommerce
35

46
final class ProductFactoryTests: XCTestCase {
@@ -40,4 +42,23 @@ final class ProductFactoryTests: XCTestCase {
4042
let product = ProductFactory().createNewProduct(type: .custom("any"), isVirtual: false, siteID: siteID)
4143
XCTAssertNil(product)
4244
}
45+
46+
func test_creating_new_product_removes_expected_fields() {
47+
// Given
48+
let product = Product.fake().copy(siteID: 123, productID: 1234, name: "Test Product", statusKey: ProductStatus.autoDraft.rawValue, price: "20.00")
49+
50+
// When
51+
let newProduct = ProductFactory().newProduct(from: product)
52+
53+
// Then
54+
55+
// Stay the same
56+
XCTAssertEqual(newProduct.siteID, product.siteID)
57+
XCTAssertEqual(newProduct.price, product.price)
58+
59+
// Changes
60+
XCTAssertEqual(newProduct.productID, 0)
61+
XCTAssertEqual(newProduct.name, "")
62+
XCTAssertEqual(newProduct.statusKey, ProductStatus.published.rawValue)
63+
}
4364
}

0 commit comments

Comments
 (0)