-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(common): migrating reference model * refactor(common): update reference model docs * chore: changesets added * fix(commons): adjust imports and exports
- Loading branch information
1 parent
59c602f
commit 608b432
Showing
313 changed files
with
1,365 additions
and
1,643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-test-data/product': patch | ||
--- | ||
|
||
Fix test assertion. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
models/commons/src/discounted-price/discounted-price-draft/fields-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.