Skip to content
Merged
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
18 changes: 18 additions & 0 deletions .changeset/spicy-numbers-deny.md
Original file line number Diff line number Diff line change
@@ -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:
Comment thread
CarlosCortizasCT marked this conversation as resolved.
Comment thread
CarlosCortizasCT marked this conversation as resolved.

- `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.
94 changes: 0 additions & 94 deletions standalone/src/models/product-selection/builder.spec.ts

This file was deleted.

15 changes: 0 additions & 15 deletions standalone/src/models/product-selection/builder.ts

This file was deleted.

115 changes: 115 additions & 0 deletions standalone/src/models/product-selection/builders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { TProductSelectionGraphql, TProductSelectionRest } from './types';
import {
ProductSelection,
Comment thread
rajrdk marked this conversation as resolved.
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({
Comment thread
Rombelirk marked this conversation as resolved.
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),
Comment thread
Rombelirk marked this conversation as resolved.
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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this case.
In the tests we first validates the REST and GraphQL models but then we need to also validate the compatibility one, which is what's done here.


validateRestModel(restModel);
});

it('builds a REST model', () => {
const restModel = ProductSelection.random().buildRest();

validateRestModel(restModel);
});

it('builds a GraphQL model', () => {
const graphqlModel =
ProductSelection.random().buildGraphql<TProductSelectionGraphql>();

validateGraphqlModel(graphqlModel);
});
});
44 changes: 44 additions & 0 deletions standalone/src/models/product-selection/builders.ts
Original file line number Diff line number Diff line change
@@ -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<TProductSelectionModel>({
name: 'ProductSelectionCompatBuilder',

modelFieldsConfig: {
rest: restFieldsConfig as TModelFieldsConfig<TProductSelectionModel>,
graphql:
graphqlFieldsConfig as TModelFieldsConfig<TProductSelectionModel>,
},
});
4 changes: 4 additions & 0 deletions standalone/src/models/product-selection/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const productSelectionMode = {
Individual: 'Individual',
IndividualExclusion: 'IndividualExclusion',
} as const;
57 changes: 57 additions & 0 deletions standalone/src/models/product-selection/fields-config.ts
Original file line number Diff line number Diff line change
@@ -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<TProductSelectionRest> = {
fields: {
...commonFieldsConfig,
},
};

export const graphqlFieldsConfig: TModelFieldsConfig<TProductSelectionGraphql> =
{
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,
};
},
};
26 changes: 0 additions & 26 deletions standalone/src/models/product-selection/generator.ts

This file was deleted.

Loading