Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEC-39: Fix models related to Batch 2 (Part 1) #650

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .changeset/thick-experts-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@commercetools-test-data/product-type': minor
'@commercetools-test-data/commons': minor
'@commercetools-test-data/inventory-entry': patch
---

### Common Model (`common`)

- Introduced a new model called `discounted-price`.

### Inventory Entry Model (`inventory-entry`)

- Updated the transformer file to conditionally send the supply channel, making it optional.

### Product Type (`product-type`)

- Added new presets for enum and localized enum types.
70 changes: 70 additions & 0 deletions models/commons/src/discounted-price/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable jest/no-disabled-tests */
/* eslint-disable jest/valid-title */
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
import { TDiscountedPrice, TDiscountedPriceGraphql } from './types';
import * as DiscountedPrice from '.';

describe('builder', () => {
it(
...createBuilderSpec<TDiscountedPrice, TDiscountedPrice>(
'default',
DiscountedPrice.random(),
expect.objectContaining({
discount: {
id: expect.any(String),
typeId: 'product-discount',
},
value: {
type: 'centPrecision',
centAmount: expect.any(Number),
currencyCode: expect.any(String),
fractionDigits: 2,
},
})
)
);

it(
...createBuilderSpec<TDiscountedPrice, TDiscountedPrice>(
'rest',
DiscountedPrice.random(),
expect.objectContaining({
discount: {
id: expect.any(String),
typeId: 'product-discount',
obj: {
id: expect.any(String),
},
},
value: {
type: 'centPrecision',
centAmount: expect.any(Number),
currencyCode: expect.any(String),
fractionDigits: 2,
},
})
)
);

it(
...createBuilderSpec<TDiscountedPrice, TDiscountedPriceGraphql>(
'graphql',
DiscountedPrice.random(),
expect.objectContaining({
discount: {
id: expect.any(String),
typeId: 'product-discount',
__typename: 'Reference',
},
value: {
type: 'centPrecision',
centAmount: expect.any(Number),
currencyCode: expect.any(String),
fractionDigits: 2,
__typename: 'Money',
},
__typename: 'DiscountedPrice',
})
)
);
});
12 changes: 12 additions & 0 deletions models/commons/src/discounted-price/builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Builder } from '@commercetools-test-data/core';
import generator from './generator';
import transformers from './transformers';
import type { TDiscountedPrice, TCreateDiscountedPriceBuilder } from './types';

const Model: TCreateDiscountedPriceBuilder = () =>
Builder<TDiscountedPrice>({
generator,
transformers,
});

export default Model;
15 changes: 15 additions & 0 deletions models/commons/src/discounted-price/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Generator, fake } from '@commercetools-test-data/core';
import { CentPrecisionMoney } from '..';
import productDiscountReference from '../reference/presets/product-discount/product-discount-reference';
import { TDiscountedPrice } from './types';

// https://docs.commercetools.com/api/types#discountedprice

const generator = Generator<TDiscountedPrice>({
fields: {
discount: fake(() => productDiscountReference()),
value: fake(() => CentPrecisionMoney.random()),
},
});

export default generator;
3 changes: 3 additions & 0 deletions models/commons/src/discounted-price/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as random } from './builder';
export { default as presets } from './presets';
export * from './types';
3 changes: 3 additions & 0 deletions models/commons/src/discounted-price/presets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const presets = {};

export default presets;
19 changes: 19 additions & 0 deletions models/commons/src/discounted-price/transformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Transformer } from '@commercetools-test-data/core';
import type { TDiscountedPrice, TDiscountedPriceGraphql } from './types';

const transformers = {
default: Transformer<TDiscountedPrice, TDiscountedPrice>('default', {
buildFields: ['discount', 'value'],
}),
rest: Transformer<TDiscountedPrice, TDiscountedPrice>('rest', {
buildFields: ['discount', 'value'],
}),
graphql: Transformer<TDiscountedPrice, TDiscountedPriceGraphql>('graphql', {
buildFields: ['discount', 'value'],
addFields: () => ({
__typename: 'DiscountedPrice',
}),
}),
};

export default transformers;
20 changes: 20 additions & 0 deletions models/commons/src/discounted-price/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
DiscountedPrice,
DiscountedPriceDraft,
} from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';

export type TDiscountedPrice = DiscountedPrice;
export type TDiscountedPriceDraft = DiscountedPriceDraft;

export type TDiscountedPriceGraphql = TDiscountedPrice & {
__typename: 'DiscountedPrice';
};
export type TDiscountedPriceDraftGraphql = TDiscountedPriceDraft;

export type TDiscountedPriceBuilder = TBuilder<TDiscountedPrice>;
export type TDiscountedPriceDraftBuilder = TBuilder<TDiscountedPriceDraft>;

export type TCreateDiscountedPriceBuilder = () => TDiscountedPriceBuilder;
export type TCreateDiscountedPriceDraftBuilder =
() => TDiscountedPriceDraftBuilder;
1 change: 1 addition & 0 deletions models/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * as Reference from './reference';
export * as ReferenceDraft from './reference/reference-draft';
export * as PriceTier from './price-tier';
export * as PriceTierDraft from './price-tier/price-tier-draft';
export * as DiscountedPrice from './discounted-price';
50 changes: 50 additions & 0 deletions models/inventory-entry/src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,53 @@ describe('builder', () => {
)
);
});

describe('builder without supply channel', () => {
it(
...createBuilderSpec<TInventoryEntry, TInventoryEntryRest>(
'rest',
InventoryEntry.random().supplyChannel(null),
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: null,
lastModifiedAt: expect.any(String),
lastModifiedBy: null,
sku: expect.any(String),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
availableQuantity: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: undefined,
custom: null,
})
)
);

it(
...createBuilderSpec<TInventoryEntry, TInventoryEntryGraphql>(
'graphql',
InventoryEntry.random().supplyChannel(null),
expect.objectContaining({
id: expect.any(String),
key: expect.any(String),
version: expect.any(Number),
createdAt: expect.any(String),
createdBy: null,
lastModifiedAt: expect.any(String),
lastModifiedBy: null,
sku: expect.any(String),
quantityOnStock: expect.any(Number),
restockableInDays: expect.any(Number),
availableQuantity: expect.any(Number),
expectedDelivery: expect.any(String),
supplyChannel: null,
supplyChannelRef: undefined,
custom: null,
__typename: 'InventoryEntry',
})
)
);
});
22 changes: 14 additions & 8 deletions models/inventory-entry/src/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ const transformers = {
rest: Transformer<TInventoryEntry, TInventoryEntryRest>('rest', {
buildFields: ['supplyChannel'],
replaceFields: ({ fields }) => {
const supplyChannel = Reference.presets
.channelReference()
.id(fields.supplyChannel.id)
.build<TReference<'channel'>>();
let supplyChannel;
if (fields.supplyChannel) {
supplyChannel = Reference.presets
.channelReference()
.id(fields.supplyChannel.id)
.build<TReference<'channel'>>();
}
return {
...fields,
supplyChannel,
Expand All @@ -30,10 +33,13 @@ const transformers = {
graphql: Transformer<TInventoryEntry, TInventoryEntryGraphql>('graphql', {
buildFields: ['supplyChannel'],
replaceFields: ({ fields }) => {
const supplyChannelRef = Reference.presets
.channelReference()
.id(fields.supplyChannel.id)
.buildGraphql<TReferenceGraphql<'channel'>>();
let supplyChannelRef;
if (fields.supplyChannel) {
supplyChannelRef = Reference.presets
.channelReference()
.id(fields.supplyChannel.id)
.buildGraphql<TReferenceGraphql<'channel'>>();
}
return {
...fields,
supplyChannelRef,
Expand Down
4 changes: 2 additions & 2 deletions models/inventory-entry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { TBuilder } from '@commercetools-test-data/core';

// Default
export type TInventoryEntry = Omit<InventoryEntry, 'supplyChannel'> & {
supplyChannel: Channel;
supplyChannel?: Channel | null;
};
export type TInventoryEntryDraft = InventoryEntryDraft;

Expand All @@ -19,7 +19,7 @@ export type TInventoryEntryDraftRest = InventoryEntryDraft;
// GraphQL
export type TInventoryEntryGraphql = TInventoryEntry & {
__typename: 'InventoryEntry';
supplyChannelRef: TReferenceGraphql;
supplyChannelRef?: TReferenceGraphql;
};
export type TInventoryEntryDraftGraphql = TInventoryEntryDraft;

Expand Down
44 changes: 43 additions & 1 deletion models/product-type/src/attribute-definition/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
const presets = {};
import { LocalizedString } from '@commercetools-test-data/commons';
import {
AttributeEnumType,
AttributeNumberType,
AttributeTextType,
} from '../..';
import AttributeDefinition from '../builder';

const presets = {
number: () =>
AttributeDefinition()
.type(AttributeNumberType.random())
.name('Number Attribute Definition')
.label(
LocalizedString.presets
.empty()
.en(`Number Attribute Definition's Label`)
)
.inputTip(
LocalizedString.presets
.empty()
.en(`Number Attribute Definition's Input Tip`)
),

countryOfOrigin: () =>
AttributeDefinition()
.attributeConstraint('None')
.name('country-of-origin')
.label(LocalizedString.presets.empty().en('Country of Origin'))
.isRequired(true)
.type(AttributeTextType.random()),

size: () =>
AttributeDefinition()
.attributeConstraint('None')
.name('size')
.label(LocalizedString.presets.empty().en('Size'))
.inputTip(LocalizedString.presets.empty().en('Size of a product'))
.inputHint('SingleLine')
.isRequired(true)
.isSearchable(true)
.type(AttributeEnumType.presets.allSizesEnum()),
};

export default presets;
24 changes: 23 additions & 1 deletion models/product-type/src/attribute-enum-type/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
const presets = {};
import { AttributePlainEnumValue } from '../..';
import AttributeEnumType from '../builder';

const presets = {
allSizesEnum: () =>
AttributeEnumType().values([
AttributePlainEnumValue.presets.s(),
AttributePlainEnumValue.presets.m(),
AttributePlainEnumValue.presets.l(),
AttributePlainEnumValue.presets.xl(),
]),
smallSizesEnum: () =>
AttributeEnumType().values([
AttributePlainEnumValue.presets.s(),
AttributePlainEnumValue.presets.m(),
]),

bigSizesEnum: () =>
AttributeEnumType().values([
AttributePlainEnumValue.presets.l(),
AttributePlainEnumValue.presets.xl(),
]),
};

export default presets;
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
const presets = {};
import { AttributeLocalizedEnumValue } from '../..';
import AttributeLocalizedEnumType from '../builder';

const presets = {
allSizesLenum: () =>
AttributeLocalizedEnumType().values([
AttributeLocalizedEnumValue.presets.s(),
AttributeLocalizedEnumValue.presets.m(),
AttributeLocalizedEnumValue.presets.l(),
AttributeLocalizedEnumValue.presets.xl(),
]),

smallSizesLenum: () =>
AttributeLocalizedEnumType().values([
AttributeLocalizedEnumValue.presets.s(),
AttributeLocalizedEnumValue.presets.m(),
]),

bigSizesLenum: () =>
AttributeLocalizedEnumType().values([
AttributeLocalizedEnumValue.presets.l(),
AttributeLocalizedEnumValue.presets.xl(),
]),
};

export default presets;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const transformers = {
results: fields.values!.map((value) =>
buildField(value, 'graphql')
) as unknown as Array<TAttributeLocalizedEnumValueGraphql>,
total: fields.values.length,
__typename: 'LocalizableEnumValueTypeResult',
},
__typename: 'LocalizableEnumAttributeDefinitionType',
Expand Down
Loading