Skip to content

Commit

Permalink
Fix some minor issues with different models (#627)
Browse files Browse the repository at this point in the history
* refactor: address several issues related to product models

* refactor(product): adjust product-data typescript type

* chore: changesets added

* refactor(product): remove unnecesary type property
  • Loading branch information
CarlosCortizasCT authored Aug 9, 2024
1 parent 5e4d863 commit 29ab14e
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-mirrors-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/store': patch
---

Fix the `Store` model generator as it was populating the `countries` attribute with an invalid value.
10 changes: 10 additions & 0 deletions .changeset/red-paws-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@commercetools-test-data/product': patch
---

Fixed `ProductVariant` model presets as they were using invalid values to populate the `attributes` property.

Fixed a couple of issues with the `Product` model:

- `searchKeywords` attribute was initialized as `null` where it needs to be an empty array.
- `masterVariant` attribute was not set as _buildable_ in the graphql transformer.
5 changes: 5 additions & 0 deletions .changeset/twenty-rocks-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/category': patch
---

Fixed the `Category` graphql transformer as it was not building the `name` and `description` properties which both are nested models of type `LocalizedString`.
17 changes: 17 additions & 0 deletions models/category/src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ describe('builder', () => {
Category.random().name(LocalizedString.random().en('Pants').de('Hosen')),
expect.objectContaining({
__typename: 'Category',
name: expect.arrayContaining([
expect.objectContaining({
__typename: 'LocalizedString',
locale: 'de',
value: 'Hosen',
}),
expect.objectContaining({
__typename: 'LocalizedString',
locale: 'en',
value: 'Pants',
}),
expect.objectContaining({
__typename: 'LocalizedString',
locale: 'fr',
value: expect.any(String),
}),
]),
nameAllLocales: expect.arrayContaining([
expect.objectContaining({
__typename: 'LocalizedString',
Expand Down
58 changes: 18 additions & 40 deletions models/category/src/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,30 @@ import { LocalizedString } from '@commercetools-test-data/commons';
import { Transformer } from '@commercetools-test-data/core';
import type { TCategory, TCategoryGraphql } from './types';

const buildFields: Array<keyof TCategory> = [
'createdBy',
'lastModifiedBy',
'name',
'slug',
'description',
'ancestors',
'parent',
'metaTitle',
'metaDescription',
'metaKeywords',
'custom',
'assets',
];

const transformers = {
default: Transformer<TCategory, TCategory>('default', {
buildFields: [
'createdBy',
'lastModifiedBy',
'name',
'slug',
'description',
'ancestors',
'parent',
'metaTitle',
'metaDescription',
'metaKeywords',
'custom',
'assets',
],
buildFields,
}),
rest: Transformer<TCategory, TCategory>('rest', {
buildFields: [
'createdBy',
'lastModifiedBy',
'name',
'slug',
'description',
'ancestors',
'parent',
'metaTitle',
'metaDescription',
'metaKeywords',
'custom',
'assets',
],
buildFields,
}),
graphql: Transformer<TCategory, TCategoryGraphql>('graphql', {
buildFields: [
'createdBy',
'lastModifiedBy',
'slug',
'ancestors',
'parent',
'metaTitle',
'metaDescription',
'metaKeywords',
'custom',
'assets',
],
buildFields,
addFields: ({ fields }) => {
const nameAllLocales = LocalizedString.toLocalizedField(fields.name);
const descriptionAllLocales = LocalizedString.toLocalizedField(
Expand Down
4 changes: 2 additions & 2 deletions models/product/src/product-catalog-data/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('builder', () => {
variant: null,
variants: expect.arrayContaining([]),
allVariants: expect.arrayContaining([]),
searchKeywords: null,
searchKeywords: expect.arrayContaining([]),
searchKeyword: expect.arrayContaining([]),
categoryOrderHints: expect.any(Object),
}),
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('builder', () => {
variant: null,
variants: expect.arrayContaining([]),
allVariants: expect.arrayContaining([]),
searchKeywords: null,
searchKeywords: expect.arrayContaining([]),
searchKeyword: expect.arrayContaining([]),
categoryOrderHints: expect.any(Object),
}),
Expand Down
2 changes: 1 addition & 1 deletion models/product/src/product-data/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('builder', () => {
variant: null,
variants: expect.arrayContaining([]),
allVariants: expect.arrayContaining([]),
searchKeywords: null,
searchKeywords: expect.arrayContaining([]),
searchKeyword: expect.arrayContaining([]),
categoryOrderHints: expect.any(Object),
})
Expand Down
2 changes: 1 addition & 1 deletion models/product/src/product-data/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const generator = Generator<TProductData>({
// TODO: Include random ProductVariant[] when available
allVariants: [],
// TODO: Include random SearchKeywords when available
searchKeywords: null,
searchKeywords: [],
// TODO: Include random SearchKeywords[] when available
searchKeyword: [],
skus: fake((f) => [`${f.lorem.word()}-${f.string.alphanumeric(3)}`]),
Expand Down
2 changes: 1 addition & 1 deletion models/product/src/product-data/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const transformers = {
},
}),
graphql: Transformer<TProductData, TProductDataGraphql>('graphql', {
buildFields: ['categories'],
buildFields: ['categories', 'masterVariant'],
replaceFields: ({ fields }) => {
const nameAllLocales = LocalizedString.toLocalizedField(fields.name);
const descriptionAllLocales = LocalizedString.toLocalizedField(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import * as Attribute from '../../attribute';
import ProductVariant from '../builder';
import { TProductVariantBuilder } from '../types';

const boringGenericMilkMasterVariant = (): TProductVariantBuilder =>
ProductVariant()
.key('boring-generic-milk-master-variant-key')
.sku('boring-generic-milk-master-variant-sku')
.attributes([
{
name: 'cow-name',
value: 'unknown',
},
])
.attributes([Attribute.random().name('cow-name').value('unknown')])
.id(1);

export default boringGenericMilkMasterVariant;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Attribute from '../../attribute';
import ProductVariant from '../builder';
import { TProductVariantBuilder } from '../types';

Expand All @@ -6,14 +7,8 @@ const happyCowMilkMasterVariant = (): TProductVariantBuilder =>
.key('happy-cow-master-variant-key')
.sku('happy-cow-master-variant-sku')
.attributes([
{
name: 'cow-name',
value: 'Buryonka',
},
{
name: 'lactose-free',
value: false,
},
Attribute.random().name('cow-name').value('Buryonka'),
Attribute.random().name('lactose-free').value(false),
])
.id(1);

Expand Down
2 changes: 1 addition & 1 deletion models/store/src/store/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const generator = Generator<TStore>({
key: fake((f) => f.lorem.slug()),
name: fake(() => LocalizedString.random()),
languages: [oneOf('en-US', 'de-DE', 'es-ES')],
countries: [fake((f) => f.location.countryCode())],
countries: [fake((f) => ({ code: f.location.countryCode() }))],
distributionChannels: [fake(() => Channel.random())],
supplyChannels: [fake(() => Channel.random())],
productSelections: [],
Expand Down
32 changes: 11 additions & 21 deletions models/store/src/store/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ import { LocalizedString, Reference } from '@commercetools-test-data/commons';
import { Transformer } from '@commercetools-test-data/core';
import type { TStore, TStoreGraphql } from './types';

const buildFields: Array<keyof TStore> = [
'name',
'createdBy',
'distributionChannels',
'lastModifiedBy',
'supplyChannels',
];

const transformers = {
default: Transformer<TStore, TStore>('default', {
buildFields: [
'name',
'createdBy',
'distributionChannels',
'lastModifiedBy',
'supplyChannels',
],
buildFields,
}),

rest: Transformer<TStore, TStore>('rest', {
buildFields: [
'name',
'createdBy',
'distributionChannels',
'lastModifiedBy',
'supplyChannels',
],
buildFields,
replaceFields: ({ fields }) => ({
...fields,
distributionChannels: [
Expand All @@ -33,13 +29,7 @@ const transformers = {
}),

graphql: Transformer<TStore, TStoreGraphql>('graphql', {
buildFields: [
'name',
'createdBy',
'distributionChannels',
'lastModifiedBy',
'supplyChannels',
],
buildFields,
addFields: ({ fields }) => ({
nameAllLocales: LocalizedString.toLocalizedField(fields.name),
distributionChannelsRef: fields.distributionChannels
Expand Down

0 comments on commit 29ab14e

Please sign in to comment.