Skip to content

Commit

Permalink
Migrate Reference model (#741)
Browse files Browse the repository at this point in the history
* refactor(common): migrating reference model

* refactor(common): update reference model docs

* chore: changesets added

* fix(commons): adjust imports and exports
  • Loading branch information
CarlosCortizasCT authored Jan 20, 2025
1 parent 59c602f commit 608b432
Show file tree
Hide file tree
Showing 313 changed files with 1,365 additions and 1,643 deletions.
41 changes: 41 additions & 0 deletions .changeset/afraid-pandas-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'@commercetools-test-data/commons': patch
---

Migrated the `Reference` model to the new implementation patterns.

Now REST and GraphQL model instances can be created using their own builders:

```ts
import {
ReferenceRest,
ReferenceGraphql,
ReferenceDraftRest,
ReferenceDraftGraphql,
} from '@commercetools-test-data/commons';

// Query models
const restProductRef = ReferenceRest.random().
.typeId('product')
.build();
const graphqlProductRef = ReferenceGraphql.random().
.typeId('product')
.build();

// Draft models
const restProductRefDraft = ReferenceDraftRest.random().
.typeId('product')
.build();
const graphqlProductRefDraft = ReferenceDraftGraphql.random().
.typeId('product')
.build();


// Presets
const restCategoryRef = ReferenceRest.presets
.category()
.build();
const graphqlCategoryRefDraft = ReferenceDraftGraphql.presets
.category()
.build();
```
5 changes: 5 additions & 0 deletions .changeset/nervous-weeks-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/product': patch
---

Fix test assertion.
28 changes: 24 additions & 4 deletions models/commons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,45 @@ const priceDraft = PriceDraft.random().build<TPriceDraft>();
```ts
import {
Reference,
ReferenceRest,
ReferenceGraphql,
ReferenceDraft,
ReferenceDraftRest,
ReferenceDraftGraphql,
type TReference,
type TReferenceDraft,
} from '@commercetools-test-data/commons';

// DEPRECATED. Please use one of the methods below
const productRef = Reference.random()
.typeId('product')
.build<TReference<'product'>>();
const restProductRef = ReferenceRest.random().
.typeId('product')
.build();
const graphqlProductRef = ReferenceGraphql.random().
.typeId('product')
.build();

// DEPRECATED. Please use one of the methods below
const productRefDraft = ReferenceDraft.random()
.typeId('product')
.build<TReferenceDraft<'product'>>();
const restProductRefDraft = ReferenceDraftRest.random().
.typeId('product')
.build();
const graphqlProductRefDraft = ReferenceDraftGraphql.random().
.typeId('product')
.build();


// Presets
const categoryRef = Reference.presets
const restCategoryRef = ReferenceRest.presets
.category()
.build<TReference<'category'>>();
const categoryRefDraft = ReferenceDraft.presets
.build();
const graphqlCategoryRefDraft = ReferenceDraftGraphql.presets
.category()
.build<TReferenceDraft<'category'>>();
.build();
```

## `PriceTier`
Expand Down
2 changes: 1 addition & 1 deletion models/commons/src/client-logging/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Generator, fake } from '@commercetools-test-data/core';
import * as Reference from '../reference';
import { Reference } from '../reference';
import type { TClientLogging } from './types';

// https://docs.commercetools.com/api/types#client-logging
Expand Down
2 changes: 1 addition & 1 deletion models/commons/src/client-logging/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buildField, Transformer } from '@commercetools-test-data/core';
import { faker } from '@faker-js/faker';
import * as Reference from '../reference';
import { Reference } from '../reference';
import { TReference } from '../reference/types';
import type { TClientLogging, TClientLoggingGraphql } from './types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fake, TModelFieldsConfig } from '@commercetools-test-data/core';
import * as Money from '../../money';
import * as Reference from '../../reference';
import { Reference } from '../../reference';
import type {
TDiscountedPriceDraftRest,
TDiscountedPriceDraftGraphql,
Expand Down
2 changes: 1 addition & 1 deletion models/commons/src/discounted-price/fields-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fake, TModelFieldsConfig } from '@commercetools-test-data/core';
import { ProductDiscount } from '@commercetools-test-data/product-discount';
import * as Money from '../money';
import * as Reference from '../reference';
import { Reference } from '../reference';
import type { TDiscountedPriceGraphql, TDiscountedPriceRest } from './types';

// https://docs.commercetools.com/api/types#discountedprice
Expand Down
3 changes: 1 addition & 2 deletions models/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ export * as Money from './money';
export * as MoneyDraft from './money/money-draft';
export * as Price from './price';
export * as PriceDraft from './price/price-draft';
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 * from './discounted-line-item-portion';
export * from './discounted-line-item-price';
export * from './geometry';
export * from './reference';
42 changes: 0 additions & 42 deletions models/commons/src/reference/builder.spec.ts

This file was deleted.

13 changes: 0 additions & 13 deletions models/commons/src/reference/builder.ts

This file was deleted.

122 changes: 122 additions & 0 deletions models/commons/src/reference/builders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
createSpecializedBuilder,
fake,
TBuilder,
TModelFieldsConfig,
} from '@commercetools-test-data/core';
import type { TReferenceGraphql, TReferenceRest } from './types';
import { Reference, ReferenceGraphql, ReferenceRest } from './index';

// Simple model for testing purposes
type TTestModelRest = {
id: string;
value: string;
version?: number;
};
const RestTestModelBuilder: () => TBuilder<TTestModelRest> = () =>
createSpecializedBuilder({
name: 'ChannelRestBuilder',
type: 'rest',
modelFieldsConfig: {
fields: {
id: fake((f) => f.string.uuid()),
value: fake((f) => f.lorem.words()),
version: fake((f) => f.number.int()),
},
} as TModelFieldsConfig<TTestModelRest>,
});

const validateRestModel = (
restModel: TReferenceRest,
typeId: string | null = null
) => {
expect(restModel).toEqual(
expect.objectContaining({
id: expect.any(String),
typeId,
obj: expect.objectContaining({
id: expect.any(String),
}),
})
);
};

const validateGraphqlModel = (graphqlModel: TReferenceGraphql) => {
expect(graphqlModel).toEqual(
expect.objectContaining({
id: expect.any(String),
typeId: null,
__typename: 'Reference',
})
);
};

describe('Channel model builders', () => {
it('builds a REST model', () => {
const restModel = ReferenceRest.random().build();

validateRestModel(restModel);
});

it('builds a populated REST model', () => {
const restModel = ReferenceRest.random()
.id('12345')
.typeId('foo')
.obj(RestTestModelBuilder())
.build<TReferenceRest<'foo', TTestModelRest>>();

validateRestModel(restModel, 'foo');
expect(restModel.obj?.value).toEqual(expect.any(String));
});

it('builds a GraphQL model', () => {
const graphqlModel = ReferenceGraphql.random().build();

validateGraphqlModel(graphqlModel);
});

it('builds a populated GraphQL model', () => {
const graphqlModel = ReferenceGraphql.random()
.id('12345')
.typeId('foo')
.build();

expect(graphqlModel.id).toEqual('12345');
expect(graphqlModel.typeId).toEqual('foo');
});
});

describe('Channel model compatibility builders', () => {
it('builds a REST model', () => {
const restModel = Reference.random().buildRest();

validateRestModel(restModel);
});

it('builds a populated REST model', () => {
const restModel = Reference.random()
.id('12345')
.typeId('foo')
.obj(RestTestModelBuilder())
.buildRest<TReferenceRest<'foo', TTestModelRest>>();

validateRestModel(restModel, 'foo');
expect(restModel.obj?.value).toEqual(expect.any(String));
});

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

validateGraphqlModel(graphqlModel);
});

it('builds a populated GraphQL model', () => {
const graphqlModel = Reference.random()
.id('12345')
.typeId('foo')
.buildGraphql();

expect(graphqlModel.id).toEqual('12345');
expect(graphqlModel.typeId).toEqual('foo');
});
});
38 changes: 38 additions & 0 deletions models/commons/src/reference/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
createCompatibilityBuilder,
createSpecializedBuilder,
TModelFieldsConfig,
} from '@commercetools-test-data/core';
import { restFieldsConfig, graphqlFieldsConfig } from './fields-config';
import type {
TReferenceRest,
TReferenceGraphql,
TCreateReferenceBuilder,
} from './types';

export const RestModelBuilder: TCreateReferenceBuilder<TReferenceRest> = () =>
createSpecializedBuilder({
name: 'ReferenceRestBuilder',
type: 'rest',
modelFieldsConfig: restFieldsConfig,
});

export const GraphqlModelBuilder: TCreateReferenceBuilder<
TReferenceGraphql
> = () =>
createSpecializedBuilder({
name: 'ReferenceGraphqlBuilder',
type: 'graphql',
modelFieldsConfig: graphqlFieldsConfig,
});

export const CompatModelBuilder = <
TReferenceModel extends TReferenceRest | TReferenceGraphql = TReferenceRest,
>() =>
createCompatibilityBuilder<TReferenceModel>({
name: 'ReferenceCompatBuilder',
modelFieldsConfig: {
rest: restFieldsConfig as TModelFieldsConfig<TReferenceModel>,
graphql: graphqlFieldsConfig as TModelFieldsConfig<TReferenceModel>,
},
});
8 changes: 8 additions & 0 deletions models/commons/src/reference/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export const referenceTypes = {
associateRole: 'associate-role',
attributeGroup: 'attribute-group',
businessUnit: 'business-unit',
cart: 'cart',
cartDiscount: 'cart-discount',
category: 'category',
channel: 'channel',
customer: 'customer',
customerGroup: 'customer-group',
discountCode: 'discount-code',
extension: 'extension',
inventoryEntry: 'inventory-entry',
keyValueDocument: 'key-value-document',
payment: 'payment',
product: 'product',
Expand All @@ -14,10 +19,13 @@ export const referenceTypes = {
productType: 'product-type',
order: 'order',
orderEdit: 'order-edit',
review: 'review',
shippingMethod: 'shipping-method',
shoppingList: 'shopping-list',
standalonePrice: 'standalone-price',
state: 'state',
store: 'store',
subscription: 'subscription',
taxCategory: 'tax-category',
type: 'type',
zone: 'zone',
Expand Down
Loading

0 comments on commit 608b432

Please sign in to comment.