Skip to content

Commit e374c6f

Browse files
Migrate ProductProjection model (#738)
* refactor(product-projection): migrate to new implementation patterns * refactor(common): temporary reference adjustment * fix(product-projection): adjust preset * refactor(common): temporary reference adjustment * refactor(product-data): add missing nested model prop * chore: add changesets
1 parent dbb519f commit e374c6f

21 files changed

+470
-491
lines changed

.changeset/cuddly-pillows-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-test-data/product': patch
3+
---
4+
5+
Added missing `obj` property to category references in the `ProductData` REST model.

.changeset/fresh-boats-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-test-data/commons': patch
3+
---
4+
5+
When providing a model to the `obj` property of the `Reference` model, that one was not used at build time in the REST representation.

.changeset/shiny-poets-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-test-data/product-projection': patch
3+
---
4+
5+
Migrated the `ProductProjection` model to the new implementation patterns.

models/commons/src/reference/transformers.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { Transformer } from '@commercetools-test-data/core';
22
import omit from 'lodash/omit';
3-
import type { TReference, TReferenceGraphql, TReferenceRest } from './types';
3+
import type {
4+
TExpandedReferenceObject,
5+
TReference,
6+
TReferenceGraphql,
7+
TReferenceRest,
8+
} from './types';
49

510
const transformers = {
611
default: Transformer<TReference, TReference>('default', {}),
712
rest: Transformer<TReference, TReferenceRest>('rest', {
8-
replaceFields: ({ fields }) => ({
9-
...fields,
10-
obj: omit(fields, ['typeId']),
11-
}),
13+
buildFields: ['obj'],
14+
replaceFields: ({ fields }) => {
15+
return {
16+
...fields,
17+
obj:
18+
(fields.obj as TExpandedReferenceObject) || omit(fields, ['typeId']),
19+
};
20+
},
1221
}),
1322
// we do not add the expanded object ourselves
1423
// since some fields are pure `*Ref`, e.g `channelsRef`

models/commons/src/reference/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export type TReferenceBuilder<TypeId = string> = TBuilder<TReference<TypeId>>;
66
export interface TReference<TypeId = string> {
77
typeId: TypeId;
88
id: string;
9+
// This is a temporary think. We will be migrating this model to the
10+
// new implementation patterns as a follow-up whih will make this unncecessary.
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
12+
obj?: any;
913
}
1014

1115
export type TExpandedReferenceObject<TypeId = string> = {

models/product-projection/README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@ $ pnpm add -D @commercetools-test-data/product-projection
1414

1515
```ts
1616
import {
17-
ProductProjection,
18-
type TProductProjection,
19-
type TProductProjectionRest,
20-
type TProductProjectionGraphql,
17+
ProductProjectionRest,
18+
ProductProjectionGraphql,
2119
} from '@commercetools-test-data/product-projection';
2220

23-
const productProjection =
24-
ProductProjection.random().build<TProductProjection>();
25-
2621
// For REST entities
27-
const productProjectionRest =
28-
ProductProjection.random().buildRest<TProductProjectionRest>();
22+
const productProjectionRest = ProductProjectionRest.random().build();
2923

3024
// For Graphql entities
31-
const productProjectionGraphql =
32-
ProductProjection.random().buildGraphql<TProductProjectionGraphql>();
25+
const productProjectionGraphql = ProductProjectionGraphql.random().build();
3326

3427
// Presets
35-
const happyCowProductProjection = ProductProjection.presets;
36-
happyCowMilkProductProjection().build<TProductProjection>();
28+
const happyCowProductProjectionRest = ProductProjectionRest.presets
29+
.happyCowMilk()
30+
.build();
31+
const happyCowProductProjectionGraphql = ProductProjectionGraphql.presets
32+
.happyCowMilk()
33+
.build();
3734
```

models/product-projection/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@commercetools-test-data/category": "10.11.2",
2323
"@commercetools-test-data/commons": "10.11.2",
2424
"@commercetools-test-data/core": "10.11.2",
25+
"@commercetools-test-data/graphql-types": "10.11.2",
2526
"@commercetools-test-data/product": "10.11.2",
2627
"@commercetools-test-data/product-type": "10.11.2",
2728
"@commercetools-test-data/state": "10.11.2",

models/product-projection/src/builder.spec.ts

Lines changed: 59 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/* eslint-disable jest/no-disabled-tests */
22
/* eslint-disable jest/valid-title */
33
import { Category } from '@commercetools-test-data/category';
4-
import { LocalizedString, Reference } from '@commercetools-test-data/commons';
4+
import { LocalizedString } from '@commercetools-test-data/commons';
55
import { createBuilderSpec } from '@commercetools-test-data/core/test-utils';
66
import { ProductVariant } from '@commercetools-test-data/product';
77
import { ProductType } from '@commercetools-test-data/product-type';
8-
import {
9-
TProductProjection,
10-
TProductProjectionGraphql,
11-
TProductProjectionRest,
12-
} from './types';
8+
import { State } from '@commercetools-test-data/state';
9+
import { TaxCategory } from '@commercetools-test-data/tax-category';
10+
import { TProductProjectionGraphql, TProductProjectionRest } from './types';
1311
import * as ProductProjection from './index';
1412

1513
describe('builder', () => {
@@ -27,99 +25,9 @@ describe('builder', () => {
2725
]);
2826

2927
it(
30-
...createBuilderSpec<TProductProjection, TProductProjection>(
31-
'default',
32-
ProductProjection.random(),
33-
expect.objectContaining({
34-
id: expect.any(String),
35-
version: expect.any(Number),
36-
key: expect.any(String),
37-
productType: expect.objectContaining({
38-
id: expect.any(String),
39-
key: expect.any(String),
40-
version: expect.any(Number),
41-
createdAt: expect.any(String),
42-
createdBy: expect.objectContaining({
43-
customer: expect.objectContaining({ typeId: 'customer' }),
44-
}),
45-
lastModifiedAt: expect.any(String),
46-
lastModifiedBy: expect.objectContaining({
47-
customer: expect.objectContaining({ typeId: 'customer' }),
48-
}),
49-
name: expect.any(String),
50-
description: expect.any(String),
51-
attributes: expect.arrayContaining([
52-
expect.objectContaining({
53-
type: expect.objectContaining({ name: expect.any(String) }),
54-
name: expect.any(String),
55-
label: expect.objectContaining({
56-
de: expect.any(String),
57-
en: expect.any(String),
58-
fr: expect.any(String),
59-
}),
60-
isRequired: expect.any(Boolean),
61-
attributeConstraint: expect.any(String),
62-
inputTip: expect.objectContaining({
63-
de: expect.any(String),
64-
en: expect.any(String),
65-
fr: expect.any(String),
66-
}),
67-
inputHint: expect.any(String),
68-
isSearchable: expect.any(Boolean),
69-
}),
70-
]),
71-
}),
72-
name: expectedGeneralLocalizedString,
73-
description: expectedGeneralLocalizedString,
74-
slug: expectedGeneralLocalizedString,
75-
categories: [],
76-
categoryOrderHints: null,
77-
metaTitle: null,
78-
metaDescription: null,
79-
metaKeywords: null,
80-
searchKeywords: [],
81-
hasStagedChanges: expect.any(Boolean),
82-
published: expect.any(Boolean),
83-
masterVariant: expect.objectContaining({
84-
key: expect.any(String),
85-
sku: expect.any(String),
86-
prices: expect.arrayContaining([
87-
expect.objectContaining({
88-
value: expect.any(Object),
89-
}),
90-
]),
91-
price: null,
92-
images: expect.arrayContaining([
93-
expect.objectContaining({
94-
url: expect.any(String),
95-
}),
96-
]),
97-
attributes: expect.arrayContaining([
98-
expect.objectContaining({
99-
name: expect.any(String),
100-
}),
101-
]),
102-
assets: expect.arrayContaining([]),
103-
availability: null,
104-
isMatchingVariant: expect.any(Boolean),
105-
scopedPrice: null,
106-
scopedPriceDiscounted: null,
107-
}),
108-
variants: [],
109-
taxCategory: null,
110-
state: null,
111-
createdAt: expect.any(String),
112-
lastModifiedAt: expect.any(String),
113-
reviewRatingStatistics: null,
114-
priceMode: null,
115-
})
116-
)
117-
);
118-
119-
it(
120-
...createBuilderSpec<TProductProjection, TProductProjectionRest>(
28+
...createBuilderSpec<TProductProjectionRest, TProductProjectionRest>(
12129
'rest',
122-
ProductProjection.random(),
30+
ProductProjection.ProductProjectionRest.random(),
12331
expect.objectContaining({
12432
id: expect.any(String),
12533
version: expect.any(Number),
@@ -199,9 +107,9 @@ describe('builder', () => {
199107
);
200108

201109
it(
202-
...createBuilderSpec<TProductProjection, TProductProjectionGraphql>(
110+
...createBuilderSpec<TProductProjectionGraphql, TProductProjectionGraphql>(
203111
'graphql',
204-
ProductProjection.random(),
112+
ProductProjection.ProductProjectionGraphql.random(),
205113
expect.objectContaining({
206114
id: expect.any(String),
207115
version: expect.any(Number),
@@ -225,8 +133,11 @@ describe('builder', () => {
225133
categories: [],
226134
categoryOrderHints: [],
227135
metaTitle: null,
136+
metaTitleAllLocales: null,
228137
metaDescription: null,
138+
metaDescriptionAllLocales: null,
229139
metaKeywords: null,
140+
metaKeywordsAllLocales: null,
230141
searchKeywords: [],
231142
hasStagedChanges: expect.any(Boolean),
232143
published: expect.any(Boolean),
@@ -236,47 +147,55 @@ describe('builder', () => {
236147
sku: expect.any(String),
237148
}),
238149
variants: [],
239-
taxCategory: undefined,
240-
state: undefined,
150+
taxCategory: null,
151+
state: null,
241152
createdAt: expect.any(String),
242153
lastModifiedAt: expect.any(String),
243-
reviewRatingStatistics: undefined,
244-
priceMode: null,
154+
reviewRatingStatistics: null,
245155
})
246156
)
247157
);
248158

249159
it('should allow customization', () => {
250-
const productProjectionMock = ProductProjection.presets
251-
.happyCowMilkProductProjection()
252-
.categories([
253-
Category.random()
254-
.id('category-id')
255-
.name(LocalizedString.presets.empty().en('category-name')),
256-
])
257-
.categoryOrderHints({
258-
'category-1': '0.4',
259-
})
260-
.id('happy-cow-milk-id')
261-
.key('happy-cow-milk-key')
262-
.metaKeywords(LocalizedString.presets.empty().en('happy'))
263-
.priceMode('Embedded')
264-
.productType(ProductType.presets.milk().id('product-type-id'))
265-
.reviewRatingStatistics({
266-
averageRating: 3.12345,
267-
highestRating: 4.9,
268-
lowestRating: 2.1,
269-
count: 25,
270-
ratingsDistribution: {},
271-
})
272-
.searchKeywords({
273-
en: [{ text: 'product search keyword' }],
274-
})
275-
.state(Reference.presets.stateReference())
276-
.taxCategory(Reference.presets.taxCategoryReference())
277-
.variants([ProductVariant.random().key('alternative-variant-key')])
278-
.version(222)
279-
.buildGraphql<TProductProjectionGraphql>();
160+
const productProjectionMock =
161+
ProductProjection.ProductProjectionGraphql.presets
162+
.happyCowMilk()
163+
.categories([
164+
Category.random()
165+
.id('category-id')
166+
.name(LocalizedString.presets.empty().en('category-name')),
167+
])
168+
.categoryOrderHints([
169+
{
170+
__typename: 'CategoryOrderHintProductSearch',
171+
categoryId: 'category-1',
172+
orderHint: '0.4',
173+
},
174+
])
175+
.id('happy-cow-milk-id')
176+
.key('happy-cow-milk-key')
177+
.metaKeywordsAllLocales(LocalizedString.presets.empty().en('happy'))
178+
.productType(ProductType.presets.milk().id('product-type-id'))
179+
.reviewRatingStatistics({
180+
averageRating: 3.12345,
181+
highestRating: 4.9,
182+
lowestRating: 2.1,
183+
count: 25,
184+
ratingsDistribution: {},
185+
__typename: 'ReviewRatingStatistics',
186+
})
187+
.searchKeywords([
188+
{
189+
__typename: 'SearchKeywordsProductSearch',
190+
locale: 'en',
191+
searchKeywords: [{ text: 'product search keyword' }],
192+
},
193+
])
194+
.state(State.random().id('state-id'))
195+
.taxCategory(TaxCategory.random().id('tax-category-id'))
196+
.variants([ProductVariant.random().key('alternative-variant-key')])
197+
.version(222)
198+
.buildGraphql<TProductProjectionGraphql>();
280199

281200
const expectedLocalizedName = expect.arrayContaining([
282201
expect.objectContaining({
@@ -312,7 +231,7 @@ describe('builder', () => {
312231
{
313232
categoryId: 'category-1',
314233
orderHint: '0.4',
315-
__typename: 'CategoryOrderHint',
234+
__typename: 'CategoryOrderHintProductSearch',
316235
},
317236
]),
318237
categoriesRef: expect.arrayContaining([
@@ -339,13 +258,13 @@ describe('builder', () => {
339258
expect.objectContaining({
340259
locale: 'en',
341260
value: 'happy',
261+
__typename: 'LocalizedString',
342262
}),
343263
]),
344264
metaTitle: 'Happy Cow Milk',
345265
metaTitleAllLocales: expectedLocalizedName,
346266
name: 'Happy Cow Milk',
347267
nameAllLocales: expectedLocalizedName,
348-
priceMode: 'Embedded',
349268
productType: expect.objectContaining({
350269
__typename: 'ProductTypeDefinition',
351270
name: 'Milk Product Type',
@@ -384,18 +303,20 @@ describe('builder', () => {
384303
}),
385304
]),
386305
state: expect.objectContaining({
387-
id: 'product-type-id',
306+
id: 'state-id',
388307
__typename: 'State',
389308
}),
390309
stateRef: expect.objectContaining({
310+
id: 'state-id',
391311
typeId: 'state',
392312
__typename: 'Reference',
393313
}),
394314
taxCategory: expect.objectContaining({
395-
id: 'product-type-id',
315+
id: 'tax-category-id',
396316
__typename: 'TaxCategory',
397317
}),
398318
taxCategoryRef: expect.objectContaining({
319+
id: 'tax-category-id',
399320
typeId: 'tax-category',
400321
__typename: 'Reference',
401322
}),

models/product-projection/src/builder.ts

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

0 commit comments

Comments
 (0)