Skip to content

Commit 96484ad

Browse files
Image test data model migration (#943)
* feat(product): add image-dimensions model * chore: add changeset * refactor(product): migrate image model to the new implementation patterns * refactor(product): adjust imports * chore: add changeset * fix: adjust changeset * fix(image): adjust tests names * refactor(image): update js docs
1 parent 89a1d1d commit 96484ad

31 files changed

Lines changed: 708 additions & 295 deletions

.changeset/pretty-rats-peel.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@commercetools/composable-commerce-test-data': patch
3+
---
4+
5+
We've migrated the `Image` model to the new implementation patterns.
6+
7+
This change does not have any impact on consumers, however the `Image` model is now deprecated and you're expected to start using the `ImageGraphql` or `ImageRest` models instead depending of the type of API you're mocking.
8+
9+
```ts
10+
import {
11+
ImageGraphql,
12+
ImageRest,
13+
} from '@commercetools/composable-commerce-test-data/product';
14+
15+
const graphqlImage = ImageGraphql.random().build();
16+
const restImage = ImageRest.random().build();
17+
```

docs/guidelines/writing-changesets.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ We've migrated the `TaxRate` model to the new implementation patterns.
8181
This change does not have any impact on consumers, however the `TaxRate` model is now deprecated and you're expected to start using the `TaxRateGraphql` or `TaxRateRest` models instead depending of the type of API you're mocking.
8282

8383
```ts
84-
import { TaxRateGraphql, TaxRateRest } form '@commercetools/composable-commerce-test-data/tax-cateopry';
84+
import {
85+
TaxRateGraphql,
86+
TaxRateRest,
87+
} from '@commercetools/composable-commerce-test-data/tax-cateopry';
8588

8689
const graphqlTaxRate = TaxRateGraphql.random().build();
8790
const restTaxRate = TaxRateRest.random().build();

standalone/src/models/product/product/image/builder.spec.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

standalone/src/models/product/product/image/builder.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { TImage, TImageGraphql, TImageRest } from './types';
2+
import { Image, ImageGraphql, ImageRest } from './index';
3+
4+
const validateModel = (model: TImageGraphql | TImageRest) => {
5+
expect(model).toMatchObject({
6+
label: expect.any(String),
7+
url: expect.any(String),
8+
});
9+
};
10+
11+
const validateRestModel = (model: TImageRest | TImage) => {
12+
validateModel(model);
13+
expect(model.dimensions).toEqual(
14+
expect.objectContaining({
15+
w: expect.any(Number),
16+
h: expect.any(Number),
17+
})
18+
);
19+
};
20+
21+
const validateGraphqlModel = (model: TImageGraphql) => {
22+
validateModel(model);
23+
expect(model.dimensions).toEqual(
24+
expect.objectContaining({
25+
__typename: 'Dimensions',
26+
})
27+
);
28+
expect(model.__typename).toEqual('Image');
29+
};
30+
31+
describe('Image model builders', () => {
32+
it('builds a REST model', () => {
33+
const restModel = ImageRest.random().build();
34+
35+
validateRestModel(restModel);
36+
});
37+
38+
it('builds a GraphQL model', () => {
39+
const graphqlModel = ImageGraphql.random().build();
40+
41+
validateGraphqlModel(graphqlModel);
42+
});
43+
});
44+
45+
describe('Image model compatibility builders', () => {
46+
it('builds a default (REST) model', () => {
47+
const restModel = Image.random().build<TImage>();
48+
49+
validateRestModel(restModel);
50+
});
51+
52+
it('builds a REST model', () => {
53+
const restModel = Image.random().buildRest<TImageRest>();
54+
55+
validateRestModel(restModel);
56+
});
57+
58+
it('builds a GraphQL model', () => {
59+
const graphqlModel = Image.random().buildGraphql<TImageGraphql>();
60+
61+
validateGraphqlModel(graphqlModel);
62+
});
63+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
createCompatibilityBuilder,
3+
createSpecializedBuilder,
4+
type TModelFieldsConfig,
5+
} from '@/core';
6+
import { restFieldsConfig, graphqlFieldsConfig } from './fields-config';
7+
import { TImageGraphql, TImageRest, TCreateImageBuilder } from './types';
8+
9+
export const RestModelBuilder: TCreateImageBuilder<TImageRest> = () =>
10+
createSpecializedBuilder({
11+
name: 'ImageRestBuilder',
12+
type: 'rest',
13+
modelFieldsConfig: restFieldsConfig,
14+
});
15+
16+
export const GraphqlModelBuilder: TCreateImageBuilder<TImageGraphql> = () =>
17+
createSpecializedBuilder({
18+
name: 'ImageGraphqlBuilder',
19+
type: 'graphql',
20+
modelFieldsConfig: graphqlFieldsConfig,
21+
});
22+
23+
export const CompatModelBuilder = <
24+
TImageModel extends TImageRest | TImageGraphql,
25+
>() =>
26+
createCompatibilityBuilder({
27+
name: 'CompatImageModelBuilder',
28+
modelFieldsConfig: {
29+
rest: restFieldsConfig as TModelFieldsConfig<TImageModel>,
30+
graphql: graphqlFieldsConfig as TModelFieldsConfig<TImageModel>,
31+
},
32+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { fake, TModelFieldsConfig } from '@/core';
2+
import {
3+
ImageDimensionsGraphql,
4+
ImageDimensionsRest,
5+
} from '../image-dimensions';
6+
import { TImageGraphql, TImageRest } from './types';
7+
8+
const commonFieldsConfig = {
9+
label: fake((f) => f.lorem.slug(2)),
10+
url: fake((f) => f.image.url()),
11+
};
12+
13+
export const restFieldsConfig: TModelFieldsConfig<TImageRest> = {
14+
fields: {
15+
...commonFieldsConfig,
16+
dimensions: fake(() => ImageDimensionsRest.random()),
17+
},
18+
};
19+
20+
export const graphqlFieldsConfig: TModelFieldsConfig<TImageGraphql> = {
21+
fields: {
22+
...commonFieldsConfig,
23+
dimensions: fake(() => ImageDimensionsGraphql.random()),
24+
__typename: 'Image',
25+
},
26+
postBuild: (model, context) => {
27+
if (context?.isCompatMode) {
28+
model.dimensions = model.dimensions
29+
? ImageDimensionsGraphql.random()
30+
// @ts-expect-error - This is needed because of the compatibility mode using the REST API
31+
.width(model.dimensions.w)
32+
// @ts-expect-error - This is needed because of the compatibility mode using the REST API
33+
.height(model.dimensions.h)
34+
.build()
35+
: model.dimensions;
36+
}
37+
return model;
38+
},
39+
};

standalone/src/models/product/product/image/generator.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

standalone/src/models/product/product/image/image-draft/builder.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

standalone/src/models/product/product/image/image-draft/builder.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)