diff --git a/.changeset/spicy-numbers-deny.md b/.changeset/spicy-numbers-deny.md new file mode 100644 index 0000000000..0300d9d006 --- /dev/null +++ b/.changeset/spicy-numbers-deny.md @@ -0,0 +1,18 @@ +--- +'@commercetools/composable-commerce-test-data': patch +--- + +The `ProductSelection` and `ProductSelectionDraft` models have been refactored to use the new implementation patterns but that does not affect consumers.. +Added new submodel to the ProductSelection model: + +- `ProductOfSelection` + +You can use it like this: + +```ts +import { ProductOfSelectionGraphql } from '@commercetools/composable-commerce-test-data/product-selection'; + +const productOfSelectionModel = ProductOfSelectionGraphql.random().build(); +``` + +This submodel is now used to populate the required `productRefs` field of the ProductSelection GraphQL model, which was previously `null` by default. diff --git a/standalone/src/models/product-selection/builder.spec.ts b/standalone/src/models/product-selection/builder.spec.ts deleted file mode 100644 index 001d8b2551..0000000000 --- a/standalone/src/models/product-selection/builder.spec.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* eslint-disable jest/no-disabled-tests */ -/* eslint-disable jest/valid-title */ -import { createBuilderSpec } from '@/core/test-utils'; -import type { TProductSelection, TProductSelectionGraphql } from './types'; -import * as ProductSelection from './index'; - -describe('builder', () => { - it( - ...createBuilderSpec( - 'default', - ProductSelection.random(), - expect.objectContaining({ - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - productCount: expect.any(Number), - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - }) - ) - ); - it( - ...createBuilderSpec( - 'rest', - ProductSelection.random(), - expect.objectContaining({ - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - productCount: expect.any(Number), - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customer: expect.objectContaining({ typeId: 'customer' }), - }), - }) - ) - ); - - it( - ...createBuilderSpec( - 'graphql', - ProductSelection.random(), - expect.objectContaining({ - __typename: 'ProductSelection', - id: expect.any(String), - version: expect.any(Number), - key: expect.any(String), - nameAllLocales: expect.arrayContaining([ - expect.objectContaining({ - __typename: 'LocalizedString', - }), - ]), - productCount: expect.any(Number), - productRefs: null, - mode: expect.any(String), - custom: null, - createdAt: expect.any(String), - createdBy: expect.objectContaining({ - customerRef: expect.objectContaining({ typeId: 'customer' }), - userRef: expect.objectContaining({ typeId: 'user' }), - }), - lastModifiedAt: expect.any(String), - lastModifiedBy: expect.objectContaining({ - customerRef: expect.objectContaining({ typeId: 'customer' }), - userRef: expect.objectContaining({ typeId: 'user' }), - }), - }) - ) - ); -}); diff --git a/standalone/src/models/product-selection/builder.ts b/standalone/src/models/product-selection/builder.ts deleted file mode 100644 index 2e8519c018..0000000000 --- a/standalone/src/models/product-selection/builder.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Builder } from '@/core'; -import generator from './generator'; -import transformers from './transformers'; -import type { - TCreateProductSelectionBuilder, - TProductSelection, -} from './types'; - -const Model: TCreateProductSelectionBuilder = () => - Builder({ - generator, - transformers, - }); - -export default Model; diff --git a/standalone/src/models/product-selection/builders.spec.ts b/standalone/src/models/product-selection/builders.spec.ts new file mode 100644 index 0000000000..6f3d4054d3 --- /dev/null +++ b/standalone/src/models/product-selection/builders.spec.ts @@ -0,0 +1,115 @@ +import { TProductSelectionGraphql, TProductSelectionRest } from './types'; +import { + ProductSelection, + ProductSelectionGraphql, + ProductSelectionRest, +} from './index'; + +const validateRestModel = (model: TProductSelectionRest): void => { + expect(model).toMatchObject({ + version: expect.any(Number), + id: expect.any(String), + createdAt: expect.any(String), + lastModifiedAt: expect.any(String), + lastModifiedBy: expect.objectContaining({ + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customer: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + obj: expect.objectContaining({ + id: expect.any(String), + }), + }), + }), + createdBy: expect.objectContaining({ + externalUserId: expect.any(String), + anonymousId: expect.any(String), + clientId: expect.any(String), + customer: expect.objectContaining({ + id: expect.any(String), + typeId: 'customer', + obj: expect.objectContaining({ + id: expect.any(String), + }), + }), + }), + key: expect.any(String), + productCount: expect.any(Number), + mode: expect.any(String), + custom: expect.any(Object), + name: expect.objectContaining({ + en: expect.any(String), + }), + }); +}; + +const validateGraphqlModel = (model: TProductSelectionGraphql): void => { + expect(model).toMatchObject({ + version: expect.any(Number), + id: expect.any(String), + createdAt: expect.any(String), + lastModifiedAt: expect.any(String), + lastModifiedBy: expect.objectContaining({ + __typename: 'Initiator', + }), + createdBy: expect.objectContaining({ + __typename: 'Initiator', + }), + nameAllLocales: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'LocalizedString', + }), + ]), + name: expect.any(String), + key: expect.any(String), + productCount: expect.any(Number), + mode: expect.any(String), + custom: expect.any(Object), + productRefs: expect.objectContaining({ + __typename: 'SelectionOfProductQueryResult', + results: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'ProductOfSelection', + }), + ]), + }), + __typename: 'ProductSelection', + }); +}; + +describe('ProductSelection model builders', () => { + it('builds a REST model', () => { + const restModel = ProductSelectionRest.random().build(); + + validateRestModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionGraphql.random().build(); + + validateGraphqlModel(graphqlModel); + }); +}); + +describe('ProductSelection model compatibility builders', () => { + it('builds a default (REST) model', () => { + const restModel = ProductSelection.random().build(); + + validateRestModel(restModel); + }); + + it('builds a REST model', () => { + const restModel = ProductSelection.random().buildRest(); + + validateRestModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = + ProductSelection.random().buildGraphql(); + + validateGraphqlModel(graphqlModel); + }); +}); diff --git a/standalone/src/models/product-selection/builders.ts b/standalone/src/models/product-selection/builders.ts new file mode 100644 index 0000000000..6a97398d82 --- /dev/null +++ b/standalone/src/models/product-selection/builders.ts @@ -0,0 +1,44 @@ +import { + createCompatibilityBuilder, + createSpecializedBuilder, + TModelFieldsConfig, +} from '@/core'; +import { restFieldsConfig, graphqlFieldsConfig } from './fields-config'; +import type { + TCreateProductSelectionBuilder, + TProductSelectionGraphql, + TProductSelectionRest, +} from './types'; + +export const RestModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionRest +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionRestBuilder', + type: 'rest', + modelFieldsConfig: restFieldsConfig, + }); + +export const GraphqlModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); + +export const CompatModelBuilder = < + TProductSelectionModel extends + | TProductSelectionRest + | TProductSelectionGraphql = TProductSelectionRest, +>() => + createCompatibilityBuilder({ + name: 'ProductSelectionCompatBuilder', + + modelFieldsConfig: { + rest: restFieldsConfig as TModelFieldsConfig, + graphql: + graphqlFieldsConfig as TModelFieldsConfig, + }, + }); diff --git a/standalone/src/models/product-selection/constants.ts b/standalone/src/models/product-selection/constants.ts new file mode 100644 index 0000000000..a5f22b7d51 --- /dev/null +++ b/standalone/src/models/product-selection/constants.ts @@ -0,0 +1,4 @@ +export const productSelectionMode = { + Individual: 'Individual', + IndividualExclusion: 'IndividualExclusion', +} as const; diff --git a/standalone/src/models/product-selection/fields-config.ts b/standalone/src/models/product-selection/fields-config.ts new file mode 100644 index 0000000000..4341e638a6 --- /dev/null +++ b/standalone/src/models/product-selection/fields-config.ts @@ -0,0 +1,57 @@ +import { + fake, + oneOf, + sequence, + TModelFieldsConfig, + buildGraphqlList, +} from '@/core'; +import { ClientLogging, LocalizedString } from '@/models/commons'; +import { createRelatedDates } from '@/utils'; +import { productSelectionMode } from './constants'; +import { ProductOfSelectionGraphql } from './product-of-selection'; +import { TProductSelectionRest, TProductSelectionGraphql } from './types'; + +const [getOlderDate, getNewerDate] = createRelatedDates(); + +const commonFieldsConfig = { + id: fake((f) => f.string.uuid()), + version: sequence(), + key: fake((f) => f.lorem.slug(2)), + name: fake(() => LocalizedString.random()), + productCount: fake((f) => f.number.int()), + mode: oneOf(...Object.values(productSelectionMode)), + custom: null, + createdAt: fake(getOlderDate), + createdBy: fake(() => ClientLogging.random()), + lastModifiedAt: fake(getNewerDate), + lastModifiedBy: fake(() => ClientLogging.random()), +}; + +export const restFieldsConfig: TModelFieldsConfig = { + fields: { + ...commonFieldsConfig, + }, +}; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + ...commonFieldsConfig, + __typename: 'ProductSelection', + nameAllLocales: fake(() => LocalizedString.random()), + productRefs: fake(() => + buildGraphqlList([ProductOfSelectionGraphql.random()], { + __typename: 'SelectionOfProductQueryResult', + }) + ), + }, + postBuild: (model) => { + const name = model.nameAllLocales + ? LocalizedString.resolveGraphqlDefaultLocaleValue(model.nameAllLocales) + : undefined; + + return { + name, + }; + }, + }; diff --git a/standalone/src/models/product-selection/generator.ts b/standalone/src/models/product-selection/generator.ts deleted file mode 100644 index a7231fb86b..0000000000 --- a/standalone/src/models/product-selection/generator.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Generator, fake, oneOf, sequence } from '@/core'; -import { ClientLogging, LocalizedString } from '@/models/commons'; -import { createRelatedDates } from '@/utils'; -import { TProductSelection } from './types'; - -const [getOlderDate, getNewerDate] = createRelatedDates(); - -// https://docs.commercetools.com/api/projects/product-selections#productselection - -const generator = Generator({ - fields: { - id: fake((f) => f.string.uuid()), - version: sequence(), - key: fake((f) => f.lorem.slug(2)), - name: fake(() => LocalizedString.random()), - productCount: fake((f) => f.number.int()), - mode: oneOf('Individual', 'IndividualExclusion'), - custom: null, - createdAt: fake(getOlderDate), - createdBy: fake(() => ClientLogging.random()), - lastModifiedAt: fake(getNewerDate), - lastModifiedBy: fake(() => ClientLogging.random()), - }, -}); - -export default generator; diff --git a/standalone/src/models/product-selection/index.ts b/standalone/src/models/product-selection/index.ts index ac78772c7a..7770c3c74e 100644 --- a/standalone/src/models/product-selection/index.ts +++ b/standalone/src/models/product-selection/index.ts @@ -1,6 +1,32 @@ -export * as ProductSelectionDraft from './product-selection-draft'; -export * as ProductSelection from '.'; +import { + RestModelBuilder, + GraphqlModelBuilder, + CompatModelBuilder, +} from './builders'; +import * as modelPresets from './presets'; -export { default as random } from './builder'; -export { default as presets } from './presets'; export * from './types'; +export * from './product-of-selection/types'; + +export * from './product-selection-draft'; +export * from './product-of-selection'; + +export * from './constants'; + +export const ProductSelectionRest = { + random: RestModelBuilder, + presets: modelPresets.restPresets, +}; + +export const ProductSelectionGraphql = { + random: GraphqlModelBuilder, + presets: modelPresets.graphqlPresets, +}; + +/** + * @deprecated Use `ProductSelectionRest` or `ProductSelectionGraphql` exported models instead of `ProductSelection`. + */ +export const ProductSelection = { + random: CompatModelBuilder, + presets: modelPresets.compatPresets, +}; diff --git a/standalone/src/models/product-selection/presets/index.ts b/standalone/src/models/product-selection/presets/index.ts index 763e57fe0a..ec0bad6e96 100644 --- a/standalone/src/models/product-selection/presets/index.ts +++ b/standalone/src/models/product-selection/presets/index.ts @@ -1,3 +1,3 @@ -const presets = {}; - -export default presets; +export const restPresets = {}; +export const graphqlPresets = {}; +export const compatPresets = {}; diff --git a/standalone/src/models/product-selection/product-of-selection/builders.spec.ts b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts new file mode 100644 index 0000000000..810754f2ca --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/builders.spec.ts @@ -0,0 +1,19 @@ +import { ProductOfSelectionGraphql } from './index'; + +describe('ProductOfSelection Builder', () => { + it('should build properties for the GraphQL representation', () => { + const graphqlModel = ProductOfSelectionGraphql.random().build(); + + expect(graphqlModel).toEqual( + expect.objectContaining({ + productRef: expect.objectContaining({ + __typename: 'Reference', + }), + product: expect.any(Object), + variantExclusion: expect.any(Object), + variantSelection: expect.any(Object), + __typename: 'ProductOfSelection', + }) + ); + }); +}); diff --git a/standalone/src/models/product-selection/product-of-selection/builders.ts b/standalone/src/models/product-selection/product-of-selection/builders.ts new file mode 100644 index 0000000000..14191d72db --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/builders.ts @@ -0,0 +1,15 @@ +import { createSpecializedBuilder } from '@/core'; +import { graphqlFieldsConfig } from './fields-config'; +import type { + TCreateProductOfSelectionBuilder, + TProductOfSelectionGraphql, +} from './types'; + +export const GraphqlModelBuilder: TCreateProductOfSelectionBuilder< + TProductOfSelectionGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductOfSelectionGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); diff --git a/standalone/src/models/product-selection/product-of-selection/fields-config.ts b/standalone/src/models/product-selection/product-of-selection/fields-config.ts new file mode 100644 index 0000000000..8f24cced14 --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/fields-config.ts @@ -0,0 +1,14 @@ +import { type TModelFieldsConfig } from '@/core'; +import { ReferenceGraphql } from '@/models/commons'; +import type { TProductOfSelectionGraphql } from './types'; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + productRef: ReferenceGraphql.presets.productReference().buildGraphql(), + product: null, + variantSelection: null, + variantExclusion: null, + __typename: 'ProductOfSelection', + }, + }; diff --git a/standalone/src/models/product-selection/product-of-selection/index.ts b/standalone/src/models/product-selection/product-of-selection/index.ts new file mode 100644 index 0000000000..c163b7109d --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/index.ts @@ -0,0 +1,9 @@ +import { GraphqlModelBuilder } from './builders'; +import * as ProductOfSelectionPresets from './presets'; +export * from './types'; + +// This model only exists in the GrahpQL API and that's why there's no REST version of it. +export const ProductOfSelectionGraphql = { + random: GraphqlModelBuilder, + presets: ProductOfSelectionPresets.graphqlPresets, +}; diff --git a/standalone/src/models/product-selection/product-of-selection/presets/index.ts b/standalone/src/models/product-selection/product-of-selection/presets/index.ts new file mode 100644 index 0000000000..16cb57eefa --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/presets/index.ts @@ -0,0 +1 @@ +export const graphqlPresets = {}; diff --git a/standalone/src/models/product-selection/product-of-selection/types.ts b/standalone/src/models/product-selection/product-of-selection/types.ts new file mode 100644 index 0000000000..508ad049bb --- /dev/null +++ b/standalone/src/models/product-selection/product-of-selection/types.ts @@ -0,0 +1,8 @@ +import type { TBuilder } from '@/core'; +import { TCtpProductOfSelection } from '@/graphql-types'; + +export type TProductOfSelectionGraphql = TCtpProductOfSelection; + +export type TCreateProductOfSelectionBuilder< + TProductOfSelectionModel extends TCtpProductOfSelection, +> = () => TBuilder; diff --git a/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts deleted file mode 100644 index da959218ea..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/builder.spec.ts +++ /dev/null @@ -1,67 +0,0 @@ -/* eslint-disable jest/no-disabled-tests */ -/* eslint-disable jest/valid-title */ -import { createBuilderSpec } from '@/core/test-utils'; -import type { - TProductSelectionDraft, - TProductSelectionDraftGraphql, -} from '../types'; -import * as ProductSelectionDraft from './index'; - -describe('builder', () => { - it( - ...createBuilderSpec( - 'default', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - mode: expect.any(String), - custom: null, - }) - ) - ); - it( - ...createBuilderSpec( - 'rest', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - name: expect.objectContaining({ - de: expect.any(String), - en: expect.any(String), - fr: expect.any(String), - }), - mode: expect.any(String), - custom: null, - }) - ) - ); - it( - ...createBuilderSpec( - 'graphql', - ProductSelectionDraft.random(), - expect.objectContaining({ - key: expect.any(String), - mode: expect.any(String), - name: expect.arrayContaining([ - expect.objectContaining({ - locale: 'en', - value: expect.any(String), - }), - expect.objectContaining({ - locale: 'fr', - value: expect.any(String), - }), - expect.objectContaining({ - locale: 'de', - value: expect.any(String), - }), - ]), - }) - ) - ); -}); diff --git a/standalone/src/models/product-selection/product-selection-draft/builder.ts b/standalone/src/models/product-selection/product-selection-draft/builder.ts deleted file mode 100644 index 8ada0fcf63..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/builder.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Builder } from '@/core'; -import type { - TCreateProductSelectionDraftBuilder, - TProductSelectionDraft, -} from '../types'; -import generator from './generator'; -import transformers from './transformers'; - -const Model: TCreateProductSelectionDraftBuilder = () => - Builder({ - generator, - transformers, - }); - -export default Model; diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts new file mode 100644 index 0000000000..e328438d42 --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/builders.spec.ts @@ -0,0 +1,55 @@ +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; +import { + ProductSelectionDraft, + ProductSelectionDraftGraphql, + ProductSelectionDraftRest, +} from './index'; + +const validateModel = ( + model: TProductSelectionDraftGraphql | TProductSelectionDraftRest +): void => { + expect(model).toEqual( + expect.objectContaining({ + key: expect.any(String), + name: expect.any(Object), + mode: expect.any(String), + custom: expect.any(Object), + }) + ); +}; + +describe('ProductSelectionDraft model builders', () => { + it('builds a REST model', () => { + const restModel = ProductSelectionDraftRest.random().build(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionDraftGraphql.random().build(); + validateModel(graphqlModel); + }); +}); + +describe('ProductSelectionDraft model compatibility builders', () => { + it('builds a default (REST) model', () => { + const restModel = ProductSelectionDraft.random().build(); + + validateModel(restModel); + }); + + it('builds a REST model', () => { + const restModel = ProductSelectionDraft.random().buildRest(); + + validateModel(restModel); + }); + + it('builds a GraphQL model', () => { + const graphqlModel = ProductSelectionDraft.random().buildGraphql(); + + validateModel(graphqlModel); + }); +}); diff --git a/standalone/src/models/product-selection/product-selection-draft/builders.ts b/standalone/src/models/product-selection/product-selection-draft/builders.ts new file mode 100644 index 0000000000..26c4d21ba8 --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/builders.ts @@ -0,0 +1,43 @@ +import { + createCompatibilityBuilder, + createSpecializedBuilder, + TModelFieldsConfig, +} from '@/core'; +import type { + TCreateProductSelectionBuilder, + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; +import { restFieldsConfig, graphqlFieldsConfig } from './fields-config'; + +export const RestModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionDraftRest +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionDraftRestBuilder', + type: 'rest', + modelFieldsConfig: restFieldsConfig, + }); + +export const GraphqlModelBuilder: TCreateProductSelectionBuilder< + TProductSelectionDraftGraphql +> = () => + createSpecializedBuilder({ + name: 'ProductSelectionDraftGraphqlBuilder', + type: 'graphql', + modelFieldsConfig: graphqlFieldsConfig, + }); + +export const CompatModelBuilder = < + TProductSelectionModel extends + | TProductSelectionDraftRest + | TProductSelectionDraftGraphql = TProductSelectionDraftRest, +>() => + createCompatibilityBuilder({ + name: 'ProductSelectionCompatBuilder', + modelFieldsConfig: { + rest: restFieldsConfig as TModelFieldsConfig, + graphql: + graphqlFieldsConfig as TModelFieldsConfig, + }, + }); diff --git a/standalone/src/models/product-selection/product-selection-draft/fields-config.ts b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts new file mode 100644 index 0000000000..75bb76fcfb --- /dev/null +++ b/standalone/src/models/product-selection/product-selection-draft/fields-config.ts @@ -0,0 +1,27 @@ +import { fake, TModelFieldsConfig } from '@/core'; +import { LocalizedString } from '@/models/commons'; +import { productSelectionMode } from '../constants'; +import { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../types'; + +export const restFieldsConfig: TModelFieldsConfig = + { + fields: { + name: fake(() => LocalizedString.random()), + key: fake((f) => f.lorem.slug()), + custom: null, + mode: productSelectionMode.Individual, + }, + }; + +export const graphqlFieldsConfig: TModelFieldsConfig = + { + fields: { + name: fake((f) => LocalizedString.random()), + key: fake((f) => f.lorem.slug()), + custom: null, + mode: productSelectionMode.Individual, + }, + }; diff --git a/standalone/src/models/product-selection/product-selection-draft/generator.ts b/standalone/src/models/product-selection/product-selection-draft/generator.ts deleted file mode 100644 index b93f6521cc..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/generator.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fake, Generator, oneOf } from '@/core'; -import { LocalizedStringDraft } from '@/models/commons'; -import { TProductSelectionDraft } from '../types'; - -//https://docs.commercetools.com/api/projects/product-selections#productselectiondraft - -const generator = Generator({ - fields: { - key: fake((f) => f.lorem.slug(2)), - name: fake(() => LocalizedStringDraft.random()), - mode: oneOf('Individual', 'IndividualExclusion'), - custom: null, - }, -}); - -export default generator; diff --git a/standalone/src/models/product-selection/product-selection-draft/index.ts b/standalone/src/models/product-selection/product-selection-draft/index.ts index 96e2519e1a..77df47a126 100644 --- a/standalone/src/models/product-selection/product-selection-draft/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/index.ts @@ -1,2 +1,24 @@ -export { default as random } from './builder'; -export { default as presets } from './presets'; +import { + RestModelBuilder, + GraphqlModelBuilder, + CompatModelBuilder, +} from './builders'; +import * as modelPresets from './presets'; + +export const ProductSelectionDraftRest = { + random: RestModelBuilder, + presets: modelPresets.restPresets, +}; + +export const ProductSelectionDraftGraphql = { + random: GraphqlModelBuilder, + presets: modelPresets.graphqlPresets, +}; + +/** + * @deprecated Use `ProductSelectionDraftRest` or `ProductSelectionDraftGraphql` exported models instead of `ProductSelectionDraft`. + */ +export const ProductSelectionDraft = { + random: CompatModelBuilder, + presets: modelPresets.compatPresets, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts index 9317451438..7bf0e60f72 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.spec.ts @@ -1,11 +1,43 @@ -import { TProductSelectionDraft } from '../../types'; -import empty from './empty'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../../types'; +import { restPreset, graphqlPreset, compatPreset } from './empty'; -it(`should set all specified fields to undefined`, () => { - const emptyDraft = empty().build(); - expect(emptyDraft).toMatchObject({ +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ key: undefined, mode: undefined, custom: undefined, }); +}; + +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + key: undefined, + mode: undefined, + custom: undefined, + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); + }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts index 09855126e6..90402a9734 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/empty.ts @@ -1,7 +1,29 @@ -import type { TProductSelectionDraftBuilder } from '../../types'; -import ProductSelectionDraft from '../builder'; +import type { TBuilder } from '@/core'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../index'; -const empty = (): TProductSelectionDraftBuilder => - ProductSelectionDraft().key(undefined).mode(undefined).custom(undefined); +export const restPreset = (): TBuilder => + ProductSelectionDraftRest.random() + .key(undefined) + .mode(undefined) + .custom(undefined); -export default empty; +export const graphqlPreset = (): TBuilder => + ProductSelectionDraftGraphql.random() + .key(undefined) + .mode(undefined) + .custom(undefined); + +export const compatPreset = (): TBuilder => + ProductSelectionDraft.random() + .key(undefined) + .mode(undefined) + .custom(undefined); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts index ce328b9dd3..1d552e4069 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/index.ts @@ -1,6 +1,15 @@ -import empty from './empty'; -import sampleDataB2B from './sample-data-b2b'; +import * as empty from './empty'; +import * as sampleDataB2B from './sample-data-b2b'; -const presets = { empty, sampleDataB2B }; - -export default presets; +export const restPresets = { + empty: empty.restPreset, + sampleDataB2B: sampleDataB2B.restPresets, +}; +export const graphqlPresets = { + empty: empty.graphqlPreset, + sampleDataB2B: sampleDataB2B.graphqlPresets, +}; +export const compatPresets = { + empty: empty.compatPreset, + sampleDataB2B: sampleDataB2B.compatPresets, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts index 1a9a94c852..df5b3d7d75 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.spec.ts @@ -1,85 +1,73 @@ -import type { TProductSelectionDraft } from '../../../types'; -import defaultProductSelection from './default-product-selection'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + restPreset, + graphqlPreset, + compatPreset, +} from './default-product-selection'; -describe(`with defaultProductSelection preset`, () => { - it(`should return a defaultProductSelection preset`, () => { - const defaultProductSelectionPreset = - defaultProductSelection().build(); - expect(defaultProductSelectionPreset).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "default-product-selection", - "mode": "Individual", - "name": { - "de": undefined, - "de-DE": "Standard", - "en": undefined, - "en-AU": "Default", - "en-GB": "Default", - "en-NZ": "Default", - "en-US": "Default", - "es-ES": "Predeterminado", - "fr": undefined, - "fr-FR": "Défaut", - "it-IT": "Predefinito", - "nl-NL": "Standaard", - "pt-PT": "Padrão", - }, - } - `); +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'default-product-selection', + mode: 'Individual', + name: { + de: undefined, + 'de-DE': 'Standard', + en: undefined, + 'en-AU': 'Default', + 'en-GB': 'Default', + 'en-NZ': 'Default', + 'en-US': 'Default', + 'es-ES': 'Predeterminado', + fr: undefined, + 'fr-FR': 'Défaut', + 'it-IT': 'Predefinito', + 'nl-NL': 'Standaard', + 'pt-PT': 'Padrão', + }, }); +}; - it(`should return a defaultProductSelection preset when built for graphql`, () => { - const defaultProductSelectionPresetGraphql = - defaultProductSelection().buildGraphql(); - expect(defaultProductSelectionPresetGraphql).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "default-product-selection", - "mode": "Individual", - "name": [ - { - "locale": "de-DE", - "value": "Standard", - }, - { - "locale": "it-IT", - "value": "Predefinito", - }, - { - "locale": "nl-NL", - "value": "Standaard", - }, - { - "locale": "fr-FR", - "value": "Défaut", - }, - { - "locale": "en-AU", - "value": "Default", - }, - { - "locale": "es-ES", - "value": "Predeterminado", - }, - { - "locale": "en-GB", - "value": "Default", - }, - { - "locale": "en-NZ", - "value": "Default", - }, - { - "locale": "pt-PT", - "value": "Padrão", - }, - { - "locale": "en-US", - "value": "Default", - }, - ], - } - `); +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + key: 'default-product-selection', + name: [ + { locale: 'de-DE', value: 'Standard' }, + { locale: 'it-IT', value: 'Predefinito' }, + { locale: 'nl-NL', value: 'Standaard' }, + { locale: 'fr-FR', value: 'Défaut' }, + { locale: 'en-AU', value: 'Default' }, + { locale: 'es-ES', value: 'Predeterminado' }, + { locale: 'en-GB', value: 'Default' }, + { locale: 'en-NZ', value: 'Default' }, + { locale: 'pt-PT', value: 'Padrão' }, + { locale: 'en-US', value: 'Default' }, + ], + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts index b87bfa19bd..6460211691 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/default-product-selection.ts @@ -1,10 +1,21 @@ -import { LocalizedStringDraft } from '../../../../commons'; -import type { TProductSelectionDraftBuilder } from '../../../types'; -import * as ProductSelectionDraft from '../../index'; +import type { TBuilder } from '@/core'; +import { TCtpProductSelectionMode } from '@/graphql-types'; +import { LocalizedStringDraft } from '@/models/commons'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../../index'; -const defaultProductSelection = (): TProductSelectionDraftBuilder => - ProductSelectionDraft.presets - .empty() +function populateBuilder< + TModel extends TProductSelectionDraftRest | TProductSelectionDraftGraphql, +>(builder: TBuilder): TBuilder { + return builder .key('default-product-selection') .name( LocalizedStringDraft.presets @@ -20,6 +31,14 @@ const defaultProductSelection = (): TProductSelectionDraftBuilder => ['pt-PT']('Padrão') ['en-US']('Default') ) - .mode('Individual'); + .mode(TCtpProductSelectionMode.Individual); +} -export default defaultProductSelection; +export const restPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftRest.presets.empty()); + +export const graphqlPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftGraphql.presets.empty()); + +export const compatPreset = (): TBuilder => + populateBuilder(ProductSelectionDraft.presets.empty()); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts index d3a9c6b1a0..bd2aa147ac 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/index.ts @@ -1,9 +1,17 @@ -import defaultProductSelection from './default-product-selection'; -import usMediumCustomersCatalog from './us-medium-customers-catalog'; +import * as defaultProductSelection from './default-product-selection'; +import * as usMediumCustomersCatalog from './us-medium-customers-catalog'; -const presets = { - usMediumCustomersCatalog, - defaultProductSelection, +export const restPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.restPreset, + defaultProductSelection: defaultProductSelection.restPreset, }; -export default presets; +export const graphqlPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.graphqlPreset, + defaultProductSelection: defaultProductSelection.graphqlPreset, +}; + +export const compatPresets = { + usMediumCustomersCatalog: usMediumCustomersCatalog.compatPreset, + defaultProductSelection: defaultProductSelection.compatPreset, +}; diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts index f004f90e40..8d0aa4d14d 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.spec.ts @@ -1,85 +1,104 @@ -import type { TProductSelectionDraft } from '../../../types'; -import usMediumCustomersCatalog from './us-medium-customers-catalog'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, +} from '../../../types'; +import { + restPreset, + graphqlPreset, + compatPreset, +} from './us-medium-customers-catalog'; -describe(`with usMediumCustomersCatalog preset`, () => { - it(`should return a usMediumCustomersCatalog preset`, () => { - const usMediumCustomersCatalogPreset = - usMediumCustomersCatalog().build(); - expect(usMediumCustomersCatalogPreset).toMatchInlineSnapshot(` - { - "custom": undefined, - "key": "us-medium-customers-catalog", - "mode": "Individual", - "name": { - "de": undefined, - "de-DE": "US Mittel Kundenkatalog", - "en": undefined, - "en-AU": "US Medium Customers Catalog", - "en-GB": "US Medium Customers Catalog", - "en-NZ": "US Medium Customers Catalog", - "en-US": "US Medium Customers Catalog", - "es-ES": "Catálogo de Clientes Medianos de EE. UU.", - "fr": undefined, - "fr-FR": "Catalogue Clients Moyens des États-Unis", - "it-IT": "Catalogo Clienti Medio USA", - "nl-NL": "Catalogus voor Middelgrote Klanten in de VS", - "pt-PT": "Catálogo de Clientes Médios dos EUA", - }, - } - `); +const restExpectation = (productSelection: TProductSelectionDraftRest) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'us-medium-customers-catalog', + mode: 'Individual', + name: { + de: undefined, + 'de-DE': 'US Mittel Kundenkatalog', + en: undefined, + 'en-AU': 'US Medium Customers Catalog', + 'en-GB': 'US Medium Customers Catalog', + 'en-NZ': 'US Medium Customers Catalog', + 'en-US': 'US Medium Customers Catalog', + 'es-ES': 'Catálogo de Clientes Medianos de EE. UU.', + fr: undefined, + 'fr-FR': 'Catalogue Clients Moyens des États-Unis', + 'it-IT': 'Catalogo Clienti Medio USA', + 'nl-NL': 'Catalogus voor Middelgrote Klanten in de VS', + 'pt-PT': 'Catálogo de Clientes Médios dos EUA', + }, }); +}; - it(`should return a usMediumCustomersCatalog preset when built for graphql`, () => { - const usMediumCustomersCatalogPresetGraphql = - usMediumCustomersCatalog().buildGraphql(); - expect(usMediumCustomersCatalogPresetGraphql).toMatchInlineSnapshot(` +const graphqlExpectation = ( + productSelection: TProductSelectionDraftGraphql +) => { + expect(productSelection).toMatchObject({ + custom: undefined, + key: 'us-medium-customers-catalog', + mode: 'Individual', + name: [ + { + locale: 'de-DE', + value: 'US Mittel Kundenkatalog', + }, + { + locale: 'it-IT', + value: 'Catalogo Clienti Medio USA', + }, + { + locale: 'nl-NL', + value: 'Catalogus voor Middelgrote Klanten in de VS', + }, + { + locale: 'fr-FR', + value: 'Catalogue Clients Moyens des États-Unis', + }, + { + locale: 'en-AU', + value: 'US Medium Customers Catalog', + }, + { + locale: 'es-ES', + value: 'Catálogo de Clientes Medianos de EE. UU.', + }, + { + locale: 'en-GB', + value: 'US Medium Customers Catalog', + }, { - "custom": undefined, - "key": "us-medium-customers-catalog", - "mode": "Individual", - "name": [ - { - "locale": "de-DE", - "value": "US Mittel Kundenkatalog", - }, - { - "locale": "it-IT", - "value": "Catalogo Clienti Medio USA", - }, - { - "locale": "nl-NL", - "value": "Catalogus voor Middelgrote Klanten in de VS", - }, - { - "locale": "fr-FR", - "value": "Catalogue Clients Moyens des États-Unis", - }, - { - "locale": "en-AU", - "value": "US Medium Customers Catalog", - }, - { - "locale": "es-ES", - "value": "Catálogo de Clientes Medianos de EE. UU.", - }, - { - "locale": "en-GB", - "value": "US Medium Customers Catalog", - }, - { - "locale": "en-NZ", - "value": "US Medium Customers Catalog", - }, - { - "locale": "pt-PT", - "value": "Catálogo de Clientes Médios dos EUA", - }, - { - "locale": "en-US", - "value": "US Medium Customers Catalog", - }, - ], - } - `); + locale: 'en-NZ', + value: 'US Medium Customers Catalog', + }, + { + locale: 'pt-PT', + value: 'Catálogo de Clientes Médios dos EUA', + }, + { + locale: 'en-US', + value: 'US Medium Customers Catalog', + }, + ], + }); +}; + +describe('Default product selection', () => { + it('[REST] should set all specified fields to undefined', () => { + const productSelectionDraft = restPreset().build(); + restExpectation(productSelectionDraft); + }); + it('[Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = graphqlPreset().build(); + graphqlExpectation(productSelectionDraft); + }); + it('[Compat - REST] should set all specified fields to undefined', () => { + const productSelectionDraft = compatPreset().buildRest(); + restExpectation(productSelectionDraft); + }); + it('[Compat - Graphql] should set all specified fields to undefined', () => { + const productSelectionDraft = + compatPreset().buildGraphql(); + graphqlExpectation(productSelectionDraft); }); }); diff --git a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts index 6bb6f96654..51058c650e 100644 --- a/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts +++ b/standalone/src/models/product-selection/product-selection-draft/presets/sample-data-b2b/us-medium-customers-catalog.ts @@ -1,10 +1,21 @@ -import { LocalizedStringDraft } from '../../../../commons'; -import type { TProductSelectionDraftBuilder } from '../../../types'; -import * as ProductSelectionDraft from '../../index'; +import type { TBuilder } from '@/core'; +import { TCtpProductSelectionMode } from '@/graphql-types'; +import { LocalizedStringDraft } from '@/models/commons'; +import type { + TProductSelectionDraftGraphql, + TProductSelectionDraftRest, + TProductSelectionDraft, +} from '../../../types'; +import { + ProductSelectionDraftRest, + ProductSelectionDraftGraphql, + ProductSelectionDraft, +} from '../../index'; -const usMediumCustomersCatalog = (): TProductSelectionDraftBuilder => - ProductSelectionDraft.presets - .empty() +function populateBuilder< + TModel extends TProductSelectionDraftRest | TProductSelectionDraftGraphql, +>(builder: TBuilder): TBuilder { + return builder .key('us-medium-customers-catalog') .name( LocalizedStringDraft.presets @@ -20,6 +31,14 @@ const usMediumCustomersCatalog = (): TProductSelectionDraftBuilder => ['pt-PT']('Catálogo de Clientes Médios dos EUA') ['en-US']('US Medium Customers Catalog') ) - .mode('Individual'); + .mode(TCtpProductSelectionMode.Individual); +} -export default usMediumCustomersCatalog; +export const restPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftRest.presets.empty()); + +export const graphqlPreset = (): TBuilder => + populateBuilder(ProductSelectionDraftGraphql.presets.empty()); + +export const compatPreset = (): TBuilder => + populateBuilder(ProductSelectionDraft.presets.empty()); diff --git a/standalone/src/models/product-selection/product-selection-draft/transformers.ts b/standalone/src/models/product-selection/product-selection-draft/transformers.ts deleted file mode 100644 index 234aa5c2f6..0000000000 --- a/standalone/src/models/product-selection/product-selection-draft/transformers.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Transformer } from '@/core'; -import type { - TProductSelectionDraft, - TProductSelectionDraftGraphql, -} from '../types'; - -const transformers = { - default: Transformer( - 'default', - { - buildFields: ['name'], - } - ), - rest: Transformer('rest', { - buildFields: ['name'], - }), - - graphql: Transformer( - 'graphql', - { - buildFields: ['name'], - } - ), -}; - -export default transformers; diff --git a/standalone/src/models/product-selection/transformers.ts b/standalone/src/models/product-selection/transformers.ts deleted file mode 100644 index b3e3456354..0000000000 --- a/standalone/src/models/product-selection/transformers.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Transformer } from '@/core'; -import { LocalizedString } from '@/models/commons'; -import type { TProductSelection, TProductSelectionGraphql } from './types'; - -const transformers = { - default: Transformer('default', { - buildFields: ['createdBy', 'lastModifiedBy', 'name'], - }), - rest: Transformer('rest', { - buildFields: ['createdBy', 'lastModifiedBy', 'name'], - }), - - graphql: Transformer('graphql', { - buildFields: ['createdBy', 'lastModifiedBy'], - removeFields: ['name'], - addFields: ({ fields }) => ({ - nameAllLocales: LocalizedString.toLocalizedField(fields.name), - productRefs: null, - __typename: 'ProductSelection', - }), - }), -}; - -export default transformers; diff --git a/standalone/src/models/product-selection/types.ts b/standalone/src/models/product-selection/types.ts index a93f7a2f3b..7a6c3f6654 100644 --- a/standalone/src/models/product-selection/types.ts +++ b/standalone/src/models/product-selection/types.ts @@ -3,24 +3,33 @@ import type { ProductSelectionDraft, } from '@commercetools/platform-sdk'; import type { TBuilder } from '@/core'; -import { TLocalizedStringGraphql } from '@/models/commons'; +import { + TCtpProductSelection, + TCtpCreateProductSelectionDraft, +} from '@/graphql-types'; -//ProductSelectionDraft +/** + * @deprecated use `TProductSelectionDraftRest` or `TProductSelectionDraftGraphql` instead + */ export type TProductSelectionDraft = ProductSelectionDraft; -export type TProductSelectionDraftBuilder = TBuilder; -export type TCreateProductSelectionDraftBuilder = - () => TProductSelectionDraftBuilder; -export type TProductSelectionDraftGraphql = Omit< - TProductSelectionDraft, - 'name' -> & { - name: TLocalizedStringGraphql; -}; - -//ProductSelection +/** + * @deprecated use `TProductSelectionRest` or `TProductSelectionGraphql` instead + */ export type TProductSelection = ProductSelection; -export type TProductSelectionBuilder = TBuilder; -export type TCreateProductSelectionBuilder = () => TProductSelectionBuilder; -export type TProductSelectionGraphql = TProductSelection & { - __typename: 'ProductSelection'; -}; + +// REST types +export type TProductSelectionDraftRest = ProductSelectionDraft; +export type TProductSelectionRest = ProductSelection; + +// GraphQL types +export type TProductSelectionDraftGraphql = TCtpCreateProductSelectionDraft; +export type TProductSelectionGraphql = TCtpProductSelection; + +// Builders types +export type TCreateProductSelectionBuilder< + TProductSelectionModel extends + | TProductSelectionRest + | TProductSelectionGraphql + | TProductSelectionDraftRest + | TProductSelectionDraftGraphql, +> = () => TBuilder; diff --git a/standalone/src/models/store/product-selection-setting/builder.spec.ts b/standalone/src/models/store/product-selection-setting/builder.spec.ts index e227f86fc0..298b4c425b 100644 --- a/standalone/src/models/store/product-selection-setting/builder.spec.ts +++ b/standalone/src/models/store/product-selection-setting/builder.spec.ts @@ -75,7 +75,14 @@ describe('builder', () => { }), ]), productCount: expect.any(Number), - productRefs: null, + productRefs: expect.objectContaining({ + __typename: 'SelectionOfProductQueryResult', + results: expect.arrayContaining([ + expect.objectContaining({ + __typename: 'ProductOfSelection', + }), + ]), + }), mode: expect.any(String), custom: null, createdAt: expect.any(String),