From b45a5609f894ba007c27c371ab5bb53be2cbfce9 Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Mon, 2 Sep 2024 16:29:12 +0200 Subject: [PATCH] fix(tests): check before create new product type (#796) * fix(tests): check before create new product type * fix(tests): check before create new product type --- .../integration-tests/cart/cart.me.test.ts | 4 +-- .../test/integration-tests/cart/cart.test.ts | 6 ++--- .../integration-tests/graphql/graphql.test.ts | 5 ++-- .../integration-tests/message/message.test.ts | 9 +++---- .../integration-tests/order/order.test.ts | 14 ++++------ .../product-projection.test.ts | 15 ++++------- .../product-type/product-type-fixture.ts | 26 +++++++++++++++++-- .../product-type/product-type.test.ts | 18 ++++++++----- .../integration-tests/product/product.test.ts | 26 +++++++------------ .../sdk-v3/middlewares.test.ts | 4 +-- 10 files changed, 65 insertions(+), 62 deletions(-) diff --git a/packages/platform-sdk/test/integration-tests/cart/cart.me.test.ts b/packages/platform-sdk/test/integration-tests/cart/cart.me.test.ts index 8bc730328..42aadc948 100644 --- a/packages/platform-sdk/test/integration-tests/cart/cart.me.test.ts +++ b/packages/platform-sdk/test/integration-tests/cart/cart.me.test.ts @@ -14,7 +14,7 @@ import { apiRoot } from '../test-utils' import { createCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { createProduct, createProductDraft } from '../product/product-fixture' @@ -76,8 +76,8 @@ describe('testing me endpoint cart', () => { // https://github.com/commercetools/commercetools-sdk-typescript/issues/446 it('should expand active cart using me endpoint in a store', async () => { const category = await createCategory() + const productType = await ensureProductType(productTypeDraftForProduct) const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, diff --git a/packages/platform-sdk/test/integration-tests/cart/cart.test.ts b/packages/platform-sdk/test/integration-tests/cart/cart.test.ts index 26e1531ee..8d381b47e 100644 --- a/packages/platform-sdk/test/integration-tests/cart/cart.test.ts +++ b/packages/platform-sdk/test/integration-tests/cart/cart.test.ts @@ -42,8 +42,7 @@ import { createType, deleteType } from '../type/type-fixture' import { createCategory, deleteCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, - deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -85,7 +84,7 @@ describe('testing cart API calls', () => { it('should create a cart with line items', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) //Published product const productDraft1 = await createProductDraft( @@ -126,7 +125,6 @@ describe('testing cart API calls', () => { await deleteCart(cart) await deleteProduct(product1) await deleteProduct(product2) - await deleteProductType(productType) await deleteCategory(category) }) diff --git a/packages/platform-sdk/test/integration-tests/graphql/graphql.test.ts b/packages/platform-sdk/test/integration-tests/graphql/graphql.test.ts index a6e608813..1cbcc46d6 100644 --- a/packages/platform-sdk/test/integration-tests/graphql/graphql.test.ts +++ b/packages/platform-sdk/test/integration-tests/graphql/graphql.test.ts @@ -2,8 +2,8 @@ import { apiRoot } from '../test-utils' import { createCategory, deleteCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -17,7 +17,7 @@ describe('testing graphQL API calls', () => { it('should make a graphQL request with string', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -48,7 +48,6 @@ describe('testing graphQL API calls', () => { expect(graphQLResponse).not.toBe(null) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) }) diff --git a/packages/platform-sdk/test/integration-tests/message/message.test.ts b/packages/platform-sdk/test/integration-tests/message/message.test.ts index e5cf1e0a5..7e379e4be 100644 --- a/packages/platform-sdk/test/integration-tests/message/message.test.ts +++ b/packages/platform-sdk/test/integration-tests/message/message.test.ts @@ -6,8 +6,7 @@ import { import { createCategory, deleteCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, - deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { apiRoot } from '../test-utils' @@ -16,7 +15,7 @@ describe('testing message API calls', () => { it('should get a message', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = createProductDraft( category, taxCategory, @@ -25,7 +24,6 @@ describe('testing message API calls', () => { ) const product = await createProduct(productDraft) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) const message = await apiRoot.messages().get().execute() @@ -36,7 +34,7 @@ describe('testing message API calls', () => { it('should get a message by Id', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = createProductDraft( category, taxCategory, @@ -63,7 +61,6 @@ describe('testing message API calls', () => { expect(message.body).not.toBe(null) expect(message.body.id).toEqual(messageId) - await deleteProductType(productType) await deleteCategory(category) } catch (e) { /** noop */ diff --git a/packages/platform-sdk/test/integration-tests/order/order.test.ts b/packages/platform-sdk/test/integration-tests/order/order.test.ts index 5786656cf..9711e27f8 100644 --- a/packages/platform-sdk/test/integration-tests/order/order.test.ts +++ b/packages/platform-sdk/test/integration-tests/order/order.test.ts @@ -3,8 +3,8 @@ import { createOrder, deleteOrder } from './order-fixture' import { createCategory, deleteCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -21,7 +21,7 @@ describe('testing order API calls', () => { it('should get a order by Id', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -65,14 +65,13 @@ describe('testing order API calls', () => { .execute() await deleteCart(getCart) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should get a order by order number', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -116,14 +115,13 @@ describe('testing order API calls', () => { .execute() await deleteCart(getCart) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should update a order', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -183,7 +181,6 @@ describe('testing order API calls', () => { .execute() await deleteCart(getCart) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) @@ -208,7 +205,7 @@ describe('testing order API calls', () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -279,7 +276,6 @@ describe('testing order API calls', () => { .execute() await deleteCart(getCart) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }, 50_000) }) diff --git a/packages/platform-sdk/test/integration-tests/product-projection/product-projection.test.ts b/packages/platform-sdk/test/integration-tests/product-projection/product-projection.test.ts index 4568b61ca..b60cebf7f 100644 --- a/packages/platform-sdk/test/integration-tests/product-projection/product-projection.test.ts +++ b/packages/platform-sdk/test/integration-tests/product-projection/product-projection.test.ts @@ -2,8 +2,7 @@ import { apiRoot } from '../test-utils' import { createCategory, deleteCategory } from '../category/category-fixture' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, - deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -17,7 +16,7 @@ describe('testing product projection API calls', () => { it('should get a product by Id', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -40,14 +39,13 @@ describe('testing product projection API calls', () => { expect(productProjectionResponse.body.id).toEqual(product.body.id) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should get a product by key', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -106,14 +104,13 @@ describe('testing product projection API calls', () => { ).toEqual({ key: 'test', label: 'test' }) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should query a product by product projection', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -136,14 +133,13 @@ describe('testing product projection API calls', () => { ) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should search a product by product projection', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -181,7 +177,6 @@ describe('testing product projection API calls', () => { expect(productProjectionSearchResponse.body.facets).not.toBe(null) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }, 40_000) }) diff --git a/packages/platform-sdk/test/integration-tests/product-type/product-type-fixture.ts b/packages/platform-sdk/test/integration-tests/product-type/product-type-fixture.ts index 7dd6b3024..b35a78f75 100644 --- a/packages/platform-sdk/test/integration-tests/product-type/product-type-fixture.ts +++ b/packages/platform-sdk/test/integration-tests/product-type/product-type-fixture.ts @@ -1,6 +1,7 @@ import { randomUUID } from 'crypto' import { apiRoot } from '../test-utils' import { AttributeDefinitionDraft, ProductTypeDraft } from '../../../src' +import { createTaxCategory } from '../tax-category/tax-category-fixture' const attributeDefinitionDraft: AttributeDefinitionDraft = { type: { @@ -91,19 +92,40 @@ const productTypeDraft: ProductTypeDraft = { } export const productTypeDraftForProduct: ProductTypeDraft = { - key: 'test-productType-key-' + randomUUID(), + key: 'test-productTypeForProduct-key', name: 'test-name-productType-' + randomUUID(), description: 'test-productType-description-' + randomUUID(), attributes: attributeDefinitionDraftProduct, } -export const createProductType = async (productTypeDraftBody?) => { +export const ensureProductType = async (productTypeDraftBody?) => { + try { + return await apiRoot + .productTypes() + .withKey({ key: productTypeDraftBody?.key || productTypeDraft.key }) + .get() + .execute() + } catch (e) { + return await createProductType(productTypeDraftBody || productTypeDraft) + } +} + +const createProductType = async (productTypeDraftBody?) => { return await apiRoot .productTypes() .post({ body: productTypeDraftBody || productTypeDraft }) .execute() } +export const createRandomProductType = async () => { + return await createProductType({ + key: 'test-productType-key-' + randomUUID(), + name: 'test-name-productType-' + randomUUID(), + description: 'test-productType-description-' + randomUUID(), + attributes: attributeDefinitionDraftProduct, + }) +} + export const deleteProductType = async (responseCreatedProductType) => { return await apiRoot .productTypes() diff --git a/packages/platform-sdk/test/integration-tests/product-type/product-type.test.ts b/packages/platform-sdk/test/integration-tests/product-type/product-type.test.ts index dc06cfca4..d4f51795f 100644 --- a/packages/platform-sdk/test/integration-tests/product-type/product-type.test.ts +++ b/packages/platform-sdk/test/integration-tests/product-type/product-type.test.ts @@ -1,7 +1,11 @@ import { randomUUID } from 'crypto' import { apiRoot } from '../test-utils' import { AttributeDefinitionDraft, ProductTypeDraft } from '../../../src' -import { createProductType, deleteProductType } from './product-type-fixture' +import { + ensureProductType, + deleteProductType, + createRandomProductType, +} from './product-type-fixture' describe('testing product type API calls', () => { it('should create and delete a product type by ID', async () => { @@ -45,7 +49,7 @@ describe('testing product type API calls', () => { }) it('should get a product type by ID', async () => { - const productType = await createProductType() + const productType = await ensureProductType() const getProductType = await apiRoot .productTypes() @@ -60,7 +64,7 @@ describe('testing product type API calls', () => { }) it('should get a product type by key', async () => { - const productType = await createProductType() + const productType = await ensureProductType() const getProductType = await apiRoot .productTypes() @@ -75,7 +79,7 @@ describe('testing product type API calls', () => { }) it('should query a product type', async () => { - const productType = await createProductType() + const productType = await ensureProductType() const queryProductType = await apiRoot .productTypes() .get({ @@ -91,7 +95,7 @@ describe('testing product type API calls', () => { }) it('should update a product type by Id', async () => { - const productType = await createProductType() + const productType = await createRandomProductType() const updateProductType = await apiRoot .productTypes() @@ -116,7 +120,7 @@ describe('testing product type API calls', () => { }) it('should update a product type by Key', async () => { - const productType = await createProductType() + const productType = await createRandomProductType() const updateProductType = await apiRoot .productTypes() @@ -141,7 +145,7 @@ describe('testing product type API calls', () => { }) it('should delete a product type by Key', async () => { - const productType = await createProductType() + const productType = await createRandomProductType() const responseProductTypeDeleted = await apiRoot .productTypes() diff --git a/packages/platform-sdk/test/integration-tests/product/product.test.ts b/packages/platform-sdk/test/integration-tests/product/product.test.ts index e039af35c..0dec84667 100644 --- a/packages/platform-sdk/test/integration-tests/product/product.test.ts +++ b/packages/platform-sdk/test/integration-tests/product/product.test.ts @@ -2,8 +2,7 @@ import { randomUUID } from 'crypto' import { apiRoot } from '../test-utils' import { ensureTaxCategory } from '../tax-category/tax-category-fixture' import { - createProductType, - deleteProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -33,7 +32,7 @@ describe('testing product API calls', () => { it('should create and delete a product by ID', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productTypeResourceIdentifier: ProductTypeResourceIdentifier = { typeId: 'product-type', @@ -155,14 +154,13 @@ describe('testing product API calls', () => { expect(responseProductDeleted.statusCode).toEqual(200) - await deleteProductType(productType) await deleteCategory(category) }) it('should get a product by Id', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -181,14 +179,13 @@ describe('testing product API calls', () => { expect(getProduct.body.id).toEqual(product.body.id) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should get a product by key', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -207,14 +204,13 @@ describe('testing product API calls', () => { expect(getProduct.body.key).toEqual(product.body.key) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should get a product by SKU using query predicates', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -241,14 +237,13 @@ describe('testing product API calls', () => { expect(getProduct.body.results[0].key).toEqual(product.body.key) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) it('should update a product by Id', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -277,14 +272,13 @@ describe('testing product API calls', () => { expect(updateProduct.statusCode).toEqual(200) await deleteProduct(updateProduct) - await deleteProductType(productType) await deleteCategory(category) }) it('should update a product by key', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -313,14 +307,13 @@ describe('testing product API calls', () => { expect(updateProduct.statusCode).toEqual(200) await deleteProduct(updateProduct) - await deleteProductType(productType) await deleteCategory(category) }) it('should query a product', async () => { const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, @@ -340,7 +333,6 @@ describe('testing product API calls', () => { expect(queryProduct.body.results[0].id).toEqual(product.body.id) await deleteProduct(product) - await deleteProductType(productType) await deleteCategory(category) }) @@ -349,7 +341,7 @@ describe('testing product API calls', () => { const imageFile = await fs.readFile(imagePath) const category = await createCategory() const taxCategory = await ensureTaxCategory() - const productType = await createProductType(productTypeDraftForProduct) + const productType = await ensureProductType(productTypeDraftForProduct) const productDraft = await createProductDraft( category, taxCategory, diff --git a/packages/platform-sdk/test/integration-tests/sdk-v3/middlewares.test.ts b/packages/platform-sdk/test/integration-tests/sdk-v3/middlewares.test.ts index 02babaf8f..fa9fe2aa1 100644 --- a/packages/platform-sdk/test/integration-tests/sdk-v3/middlewares.test.ts +++ b/packages/platform-sdk/test/integration-tests/sdk-v3/middlewares.test.ts @@ -5,7 +5,7 @@ import { projectKey, } from '../test-utils' import { - createProductType, + ensureProductType, productTypeDraftForProduct, } from '../product-type/product-type-fixture' import { @@ -36,7 +36,7 @@ describe('Concurrent Modification Middleware', () => { beforeAll(async () => { category = await createCategory() taxCategory = await ensureTaxCategory() - productType = await createProductType(productTypeDraftForProduct) + productType = await ensureProductType(productTypeDraftForProduct) }) beforeEach(async () => {