Skip to content

Commit

Permalink
PRC-3862: Add new models for CartDiscountPatternTarget, CountOnLineIt…
Browse files Browse the repository at this point in the history
…emUnits, CountOnCustomLineItemUnits (#744)

* feat(cart-discount): add drafts builders

* feat(cart-discount): adapt pattern target models

* test(): update tests

* chore(cart-discount): update package.json

* chore(cart-discount): update graphql-types dependency specification

* refactor(cart-discount): simplify type imports and update draft types

* chore(cart-discount): move lodash types to dependencies and clean up package.json
  • Loading branch information
nima-ct authored Jan 23, 2025
1 parent 828be81 commit 6df7aac
Show file tree
Hide file tree
Showing 40 changed files with 794 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-parents-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/cart-discount': minor
---

Introducing the new CartDiscountPatternTarget, CountOnLineItemUnits and CountOnCustomLineItemUnitsInput models for cart discount
5 changes: 4 additions & 1 deletion models/cart-discount/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
"@commercetools-test-data/commons": "10.11.2",
"@commercetools-test-data/core": "10.11.2",
"@commercetools-test-data/customer-group": "10.11.2",
"@commercetools-test-data/graphql-types": "10.11.2",
"@commercetools-test-data/product-type": "10.11.2",
"@commercetools-test-data/utils": "10.11.2",
"@commercetools/platform-sdk": "7.25.1",
"@faker-js/faker": "^9.0.0"
"@faker-js/faker": "^9.0.0",
"@types/lodash": "^4.17.14",
"lodash": "^4.17.21"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
CartDiscountPatternTargetRest,
CartDiscountPatternTargetGraphql,
} from './index';

describe('CartDiscountPatternTarget Builder', () => {
it('should build properties for the REST representation', () => {
const restModel = CartDiscountPatternTargetRest.random().build();

expect(restModel).toEqual(
expect.objectContaining({
type: 'pattern',
maxOccurrence: expect.toBeOneOf([expect.any(Number), null]),
selectionMode: expect.any(String),
targetPattern: expect.arrayContaining([
expect.objectContaining({
type: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
excludeCount: expect.any(Number),
}),
]),
triggerPattern: expect.arrayContaining([
expect.objectContaining({
type: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
excludeCount: expect.any(Number),
}),
]),
})
);
});

it('should build properties for the GraphQL representation', () => {
const graphqlModel = CartDiscountPatternTargetGraphql.random().build();

expect(graphqlModel).toEqual(
expect.objectContaining({
__typename: 'CartDiscountPatternTarget',
type: 'pattern',
maxOccurrence: expect.toBeOneOf([expect.any(Number), null]),
selectionMode: expect.any(String),
targetPattern: expect.arrayContaining([
expect.objectContaining({
__typename: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
type: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
excludeCount: expect.any(Number),
}),
]),
triggerPattern: expect.arrayContaining([
expect.objectContaining({
__typename: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
type: expect.toBeOneOf([
'CountOnLineItemUnits',
'CountOnCustomLineItemUnits',
]),
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
}),
]),
})
);
});
});
25 changes: 25 additions & 0 deletions models/cart-discount/src/cart-discount-pattern-target/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createSpecializedBuilder } from '@commercetools-test-data/core';
import { restFieldsConfig, graphqlFieldsConfig } from './fields-config';
import type {
TCreateCartDiscountPatternTargetBuilder,
TCartDiscountPatternTargetGraphql,
TCartDiscountPatternTargetRest,
} from './types';

export const RestModelBuilder: TCreateCartDiscountPatternTargetBuilder<
TCartDiscountPatternTargetRest
> = () =>
createSpecializedBuilder({
name: 'CartDiscountPatternTargetRestBuilder',
type: 'rest',
modelFieldsConfig: restFieldsConfig,
});

export const GraphqlModelBuilder: TCreateCartDiscountPatternTargetBuilder<
TCartDiscountPatternTargetGraphql
> = () =>
createSpecializedBuilder({
name: 'CartDiscountPatternTargetGraphqlBuilder',
type: 'graphql',
modelFieldsConfig: graphqlFieldsConfig,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { fake, type TModelFieldsConfig } from '@commercetools-test-data/core';
import { TCtpSelectionMode } from '@commercetools-test-data/graphql-types';
import omit from 'lodash/omit';
import {
CountOnCustomLineItemUnitsGraphql,
CountOnCustomLineItemUnitsRest,
} from '../count-on-custom-line-item-units';
import {
CountOnLineItemUnitsGraphql,
CountOnLineItemUnitsRest,
} from '../count-on-line-item-units';
import type {
TCartDiscountPatternTargetGraphql,
TCartDiscountPatternTargetRest,
} from './types';

const commonFieldsConfig = {
type: 'pattern',
maxOccurrence: fake((f) =>
f.helpers.arrayElement([null, 1, f.number.int({ min: 2, max: 10 })])
),
selectionMode: fake((f) =>
f.helpers.arrayElement(Object.values(TCtpSelectionMode))
),
};

export const restFieldsConfig: TModelFieldsConfig<TCartDiscountPatternTargetRest> =
{
fields: {
...commonFieldsConfig,
targetPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsRest.random(),
CountOnCustomLineItemUnitsRest.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
triggerPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsRest.random(),
CountOnCustomLineItemUnitsRest.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
},
};

export const graphqlFieldsConfig: TModelFieldsConfig<TCartDiscountPatternTargetGraphql> =
{
fields: {
...commonFieldsConfig,
targetPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsGraphql.random(),
CountOnCustomLineItemUnitsGraphql.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
triggerPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsGraphql.random(),
CountOnCustomLineItemUnitsGraphql.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
__typename: 'CartDiscountPatternTarget',
},
postBuild: (fields) => ({
...fields,
triggerPattern: fields.triggerPattern.map((pattern) =>
omit(pattern, 'excludeCount')
),
}),
};
14 changes: 14 additions & 0 deletions models/cart-discount/src/cart-discount-pattern-target/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RestModelBuilder, GraphqlModelBuilder } from './builders';
import * as CartDiscountPatternTargetPresets from './presets';
export * from './src/cart-discount-pattern-target-draft';
export * from './types';

export const CartDiscountPatternTargetRest = {
random: RestModelBuilder,
presets: CartDiscountPatternTargetPresets.restPresets,
};

export const CartDiscountPatternTargetGraphql = {
random: GraphqlModelBuilder,
presets: CartDiscountPatternTargetPresets.graphqlPresets,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const restPresets = {};
export const graphqlPresets = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CartDiscountPatternTargetDraftGraphql } from './index';

describe('CartDiscountPatternTargetDraft Builder', () => {
it('should build properties for the GraphQL representation', () => {
const graphqlModel = CartDiscountPatternTargetDraftGraphql.random().build();

expect(graphqlModel).toEqual(
expect.objectContaining({
maxOccurrence: expect.toBeOneOf([expect.any(Number), null]),
selectionMode: expect.any(String),
targetPattern: expect.arrayContaining([
expect.objectContaining({
excludeCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
minCount: expect.any(Number),
predicate: expect.any(String),
}),
]),
triggerPattern: expect.arrayContaining([
expect.objectContaining({
excludeCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
minCount: expect.any(Number),
predicate: expect.any(String),
}),
]),
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createSpecializedBuilder } from '@commercetools-test-data/core';
import { graphqlFieldsConfig } from './fields-config';
import type {
TCreateCartDiscountPatternTargetDraftBuilder,
TCartDiscountPatternTargetDraftGraphql,
} from './types';

export const GraphqlModelBuilder: TCreateCartDiscountPatternTargetDraftBuilder<
TCartDiscountPatternTargetDraftGraphql
> = () =>
createSpecializedBuilder({
name: 'CartDiscountPatternTargetDraftGraphqlBuilder',
type: 'graphql',
modelFieldsConfig: graphqlFieldsConfig,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { fake, type TModelFieldsConfig } from '@commercetools-test-data/core';
import { TCtpSelectionMode } from '@commercetools-test-data/graphql-types';
import { CountOnCustomLineItemUnitsDraftGraphql } from '../../../count-on-custom-line-item-units';
import { CountOnLineItemUnitsDraftGraphql } from '../../../count-on-line-item-units';

import type { TCartDiscountPatternTargetDraftGraphql } from './types';

export const graphqlFieldsConfig: TModelFieldsConfig<TCartDiscountPatternTargetDraftGraphql> =
{
fields: {
maxOccurrence: fake((f) =>
f.helpers.arrayElement([null, 1, f.number.int({ min: 2, max: 10 })])
),
selectionMode: fake((f) =>
f.helpers.arrayElement(Object.values(TCtpSelectionMode))
),
targetPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsDraftGraphql.random(),
CountOnCustomLineItemUnitsDraftGraphql.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
triggerPattern: fake((f) =>
f.helpers.multiple(
() =>
f.helpers.arrayElement([
CountOnLineItemUnitsDraftGraphql.random(),
CountOnCustomLineItemUnitsDraftGraphql.random(),
]),
{ count: { min: 1, max: 8 } }
)
),
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { GraphqlModelBuilder } from './builders';
import * as CartDiscountPatternTargetDraftPresets from './presets';

export const CartDiscountPatternTargetDraftGraphql = {
random: GraphqlModelBuilder,
presets: CartDiscountPatternTargetDraftPresets.graphqlPresets,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const restPresets = {};
export const graphqlPresets = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { TBuilder } from '@commercetools-test-data/core';
import { TCtpCartDiscountPatternTargetInput } from '@commercetools-test-data/graphql-types';

export type TCartDiscountPatternTargetDraftGraphql =
TCtpCartDiscountPatternTargetInput;

export type TCreateCartDiscountPatternTargetDraftBuilder<
TModel extends TCartDiscountPatternTargetDraftGraphql,
> = () => TBuilder<TModel>;
18 changes: 18 additions & 0 deletions models/cart-discount/src/cart-discount-pattern-target/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CartDiscountPatternTarget } from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';
import {
TCtpCartDiscountPatternTarget,
TCtpCartDiscountPatternTargetInput,
} from '@commercetools-test-data/graphql-types';

export type TCartDiscountPatternTargetRest = CartDiscountPatternTarget;
export type TCartDiscountPatternTargetGraphql = TCtpCartDiscountPatternTarget;
export type TCartDiscountPatternTargetDraftGraphql =
TCtpCartDiscountPatternTargetInput;

export type TCreateCartDiscountPatternTargetBuilder<
TModel extends
| TCartDiscountPatternTargetRest
| TCartDiscountPatternTargetGraphql
| TCartDiscountPatternTargetDraftGraphql,
> = () => TBuilder<TModel>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
CountOnCustomLineItemUnitsRest,
CountOnCustomLineItemUnitsGraphql,
} from './index';

describe('CountOnCustomLineItemUnits Builder', () => {
it('should build properties for the REST representation', () => {
const restModel = CountOnCustomLineItemUnitsRest.random().build();

expect(restModel).toEqual(
expect.objectContaining({
type: 'CountOnCustomLineItemUnits',
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
excludeCount: expect.any(Number),
})
);
});

it('should build properties for the GraphQL representation', () => {
const graphqlModel = CountOnCustomLineItemUnitsGraphql.random().build();

expect(graphqlModel).toEqual(
expect.objectContaining({
__typename: 'CountOnCustomLineItemUnits',
type: 'CountOnCustomLineItemUnits',
predicate: expect.any(String),
minCount: expect.any(Number),
maxCount: expect.toBeOneOf([expect.any(Number), null]),
excludeCount: expect.any(Number),
})
);
});
});
Loading

0 comments on commit 6df7aac

Please sign in to comment.