Skip to content

Commit

Permalink
fix(tests): check before create new product type (#796)
Browse files Browse the repository at this point in the history
* fix(tests): check before create new product type

* fix(tests): check before create new product type
  • Loading branch information
lojzatran authored Sep 2, 2024
1 parent 4f05110 commit b45a560
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -126,7 +125,6 @@ describe('testing cart API calls', () => {
await deleteCart(cart)
await deleteProduct(product1)
await deleteProduct(product2)
await deleteProductType(productType)
await deleteCategory(category)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -48,7 +48,6 @@ describe('testing graphQL API calls', () => {
expect(graphQLResponse).not.toBe(null)

await deleteProduct(product)
await deleteProductType(productType)
await deleteCategory(category)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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 */
Expand Down
14 changes: 5 additions & 9 deletions packages/platform-sdk/test/integration-tests/order/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -183,7 +181,6 @@ describe('testing order API calls', () => {
.execute()
await deleteCart(getCart)
await deleteProduct(product)
await deleteProductType(productType)
await deleteCategory(category)
})

Expand All @@ -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,
Expand Down Expand Up @@ -279,7 +276,6 @@ describe('testing order API calls', () => {
.execute()
await deleteCart(getCart)
await deleteProduct(product)
await deleteProductType(productType)
await deleteCategory(category)
}, 50_000)
})
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
})
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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({
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Loading

0 comments on commit b45a560

Please sign in to comment.