Skip to content

Commit

Permalink
[FEC-49] ShippingInfo model (#726)
Browse files Browse the repository at this point in the history
* feat(cart): added shipping-info child model

* feat(commons): new dicounted line models

* refactor(cart): add new children models

* refactor(tax-category): add missing preset

* fix(cart): adjust child models

* fix(common): fix discounted-line-item-portion model

* refactor(commons): docs improved

* ci: changesets added
  • Loading branch information
CarlosCortizasCT authored Dec 12, 2024
1 parent f3dfbaf commit 3afc6db
Show file tree
Hide file tree
Showing 55 changed files with 1,195 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-eagles-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/tax-category': minor
---

Added a new preset called `withAllFields` to the model.
9 changes: 9 additions & 0 deletions .changeset/eight-keys-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@commercetools-test-data/cart': minor
---

Added new child models used within the `Cart` one:

- `TaxedItemPrice`
- `TaxPortion`
- `ShippingInfo`
9 changes: 9 additions & 0 deletions .changeset/tame-swans-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@commercetools-test-data/commons': minor
---

Added three new test data models:

- `DicountedLineItemPortion`
- `DicountedLineItemPortionDraft`
- `DiscountedLitemItemPrice`
41 changes: 41 additions & 0 deletions models/cart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,44 @@ const emptyLineItemDraft = LineItemDraft.presets
.empty()
.build<TLineItemDraft>();
```

## TaxPortion

```ts
import {
TaxPortionRest,
TaxPortionDraftRest,
TaxPortionGraphql,
TaxPortionDraftGraphql,
} from '@commercetools-test-data/cart';

const restTaxPortion = TaxPortionRest.random().build();
const graphqlTaxPortion = TaxPortionGraphql.random().build();

const restTaxPortionDraft = TaxPortionDraftRest.random().build();
const graphqlTaxPortionDraft = TaxPortionDraftGraphql.random().build();
```

## TaxedItemPrice

```ts
import {
TaxedItemPriceRest,
TaxedItemPriceGraphql,
} from '@commercetools-test-data/cart';

const restTaxedItemPrice = TaxedItemPriceRest.random().build();
const graphqlTaxedItemPrice = TaxedItemPriceGraphql.random().build();
```

## ShippingInfo

```ts
import {
ShippingInfoRest,
ShippingInfoGraphql,
} from '@commercetools-test-data/cart';

const restShippingInfo = ShippingInfoRest.random().build();
const graphqlShippingInfo = ShippingInfoGraphql.random().build();
```
1 change: 1 addition & 0 deletions models/cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@commercetools-test-data/core": "10.10.1",
"@commercetools-test-data/customer": "10.10.1",
"@commercetools-test-data/discount-code": "10.10.1",
"@commercetools-test-data/graphql-types": "10.10.1",
"@commercetools-test-data/product": "10.10.1",
"@commercetools-test-data/shipping-method": "10.10.1",
"@commercetools-test-data/store": "10.10.1",
Expand Down
4 changes: 4 additions & 0 deletions models/cart/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export * as CustomLineItem from './custom-line-item';
export * as CustomLineItemDraft from './custom-line-item/custom-line-item-draft';
export * as LineItem from './line-item';
export * as LineItemDraft from './line-item/line-item-draft';

export * from './shipping-info';
export * from './tax-portion';
export * from './taxed-item-price';
57 changes: 57 additions & 0 deletions models/cart/src/shipping-info/builders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ShippingInfoRest, ShippingInfoGraphql } from './index';

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

expect(restModel).toEqual(
expect.objectContaining({
deliveries: [],
discountedPrice: null,
price: expect.objectContaining({
type: 'centPrecision',
}),
shippingMethod: null,
shippingMethodName: expect.any(String),
shippingMethodState: expect.stringMatching(
/DoesNotMatchCart|MatchesCart/
),
shippingRate: expect.objectContaining({
price: expect.objectContaining({
type: 'centPrecision',
}),
}),
taxCategory: null,
taxRate: null,
taxedPrice: null,
})
);
});
it('should build properties for the GraphQL representation', () => {
const graphqlModel = ShippingInfoGraphql.random().build();

expect(graphqlModel).toEqual(
expect.objectContaining({
deliveries: [],
discountedPrice: null,
price: expect.objectContaining({
__typename: 'Money',
}),
shippingMethod: null,
shippingMethodName: expect.any(String),
shippingMethodState: expect.stringMatching(
/DoesNotMatchCart|MatchesCart/
),
shippingRate: expect.objectContaining({
__typename: 'ShippingRate',
}),
taxCategory: null,
taxRate: null,
taxedPrice: null,
shippingMethodRef: null,
taxCategoryRef: null,
__typename: 'ShippingInfo',
})
);
});
});
25 changes: 25 additions & 0 deletions models/cart/src/shipping-info/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 {
TCreateShippingInfoBuilder,
TShippingInfoGraphql,
TShippingInfoRest,
} from './types';

export const RestModelBuilder: TCreateShippingInfoBuilder<
TShippingInfoRest
> = () =>
createSpecializedBuilder({
name: 'ShippingInfoRestBuilder',
type: 'rest',
modelFieldsConfig: restFieldsConfig,
});

export const GraphqlModelBuilder: TCreateShippingInfoBuilder<
TShippingInfoGraphql
> = () =>
createSpecializedBuilder({
name: 'ShippingInfoGraphqlBuilder',
type: 'graphql',
modelFieldsConfig: graphqlFieldsConfig,
});
47 changes: 47 additions & 0 deletions models/cart/src/shipping-info/fields-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
CentPrecisionMoney,
Reference,
} from '@commercetools-test-data/commons';
import { fake, type TModelFieldsConfig } from '@commercetools-test-data/core';
import { ShippingRate } from '@commercetools-test-data/shipping-method';
import type { TShippingInfoGraphql, TShippingInfoRest } from './types';

const commonFieldsConfig = {
deliveries: [],
discountedPrice: null,
price: fake(() => CentPrecisionMoney.random()),
shippingMethod: null,
shippingMethodName: fake((f) => f.lorem.words(2)),
shippingMethodState: fake((f) =>
f.helpers.arrayElement(['DoesNotMatchCart', 'MatchesCart'])
),
shippingRate: fake(() => ShippingRate.random()),
taxCategory: null,
taxRate: null,
taxedPrice: null,
};

export const restFieldsConfig: TModelFieldsConfig<TShippingInfoRest> = {
fields: {
...commonFieldsConfig,
},
};

export const graphqlFieldsConfig: TModelFieldsConfig<TShippingInfoGraphql> = {
fields: {
...commonFieldsConfig,
shippingMethodRef: null,
taxCategoryRef: null,
__typename: 'ShippingInfo',
},
postBuild: (model) => {
return {
shippingMethodRef: model.shippingMethod
? Reference.presets
.shippingMethodReference()
.id(model.shippingMethod.id)
.buildGraphql()
: model.shippingMethodRef,
};
},
};
13 changes: 13 additions & 0 deletions models/cart/src/shipping-info/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RestModelBuilder, GraphqlModelBuilder } from './builders';
import * as ShippingInfoPresets from './presets';
export * from './types';

export const ShippingInfoRest = {
random: RestModelBuilder,
presets: ShippingInfoPresets.restPresets,
};

export const ShippingInfoGraphql = {
random: GraphqlModelBuilder,
presets: ShippingInfoPresets.graphqlPresets,
};
2 changes: 2 additions & 0 deletions models/cart/src/shipping-info/presets/index.ts
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,77 @@
import * as withAllPresets from './with-all-fields-presets';

describe('with all fields', () => {
it(`should create the expected object preset - rest`, () => {
const restExamplePreset = withAllPresets.restPreset().build();

expect(restExamplePreset).toEqual(
expect.objectContaining({
deliveries: expect.any(Array),
price: expect.objectContaining({
currencyCode: 'EUR',
}),
shippingMethod: expect.objectContaining({
typeId: 'shipping-method',
}),
shippingMethodName: 'shipping-method-name',
shippingRate: expect.objectContaining({
price: expect.objectContaining({
currencyCode: expect.any(String),
}),
}),
taxCategory: expect.objectContaining({
typeId: 'tax-category',
}),
taxRate: expect.objectContaining({
id: expect.any(String),
name: expect.any(String),
amount: expect.any(Number),
}),
taxedPrice: expect.objectContaining({
totalNet: expect.objectContaining({
currencyCode: 'EUR',
}),
totalGross: expect.objectContaining({
currencyCode: 'EUR',
}),
totalTax: expect.objectContaining({
currencyCode: 'EUR',
}),
taxPortions: expect.any(Array),
}),
})
);
});

it(`should create the expected object preset - graphql`, () => {
const graphqlExamplePreset = withAllPresets.graphqlPreset().build();

expect(graphqlExamplePreset).toEqual(
expect.objectContaining({
deliveries: expect.any(Array),
price: expect.objectContaining({
currencyCode: 'EUR',
__typename: 'Money',
}),
shippingMethod: expect.objectContaining({
__typename: 'ShippingMethod',
}),
shippingMethodRef: expect.objectContaining({
typeId: 'shipping-method',
__typename: 'Reference',
}),
shippingMethodName: 'shipping-method-name',
shippingRate: expect.objectContaining({
__typename: 'ShippingRate',
}),
taxCategory: expect.objectContaining({
typeId: 'tax-category',
}),
taxRate: expect.objectContaining({
__typename: 'TaxRate',
}),
__typename: 'ShippingInfo',
})
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Money, Reference } from '@commercetools-test-data/commons';
import type { TBuilder } from '@commercetools-test-data/core';
import {
ShippingMethod,
ShippingRate,
} from '@commercetools-test-data/shipping-method';
import { TaxRate } from '@commercetools-test-data/tax-category';
import {
TaxedItemPriceGraphql,
TaxedItemPriceRest,
} from '../../../taxed-item-price';
import { ShippingInfoGraphql, ShippingInfoRest } from '../../index';
import { TShippingInfoGraphql, TShippingInfoRest } from '../../types';

const populatePreset = <T extends TShippingInfoRest | TShippingInfoGraphql>(
builder: TBuilder<T>
): TBuilder<T> =>
builder
.deliveries([])
.price(Money.presets.withCurrency('EUR'))
.shippingMethodName('shipping-method-name')
.shippingRate(ShippingRate.random())
.taxCategory(Reference.presets.taxCategoryReference())
.taxRate(TaxRate.presets.withAllFields());

export const restPreset = (): TBuilder<TShippingInfoRest> =>
populatePreset(ShippingInfoRest.random())
.shippingMethod(Reference.presets.shippingMethodReference())
.taxedPrice(TaxedItemPriceRest.presets.withAllFields());

export const graphqlPreset = (): TBuilder<TShippingInfoGraphql> =>
populatePreset(ShippingInfoGraphql.random())
.shippingMethod(ShippingMethod.random())
.taxedPrice(TaxedItemPriceGraphql.presets.withAllFields());
11 changes: 11 additions & 0 deletions models/cart/src/shipping-info/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ShippingInfo } from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';
import { TCtpShippingInfo } from '@commercetools-test-data/graphql-types';

export type TShippingInfoRest = ShippingInfo;

export type TShippingInfoGraphql = TCtpShippingInfo;

export type TCreateShippingInfoBuilder<
TModel extends TShippingInfoRest | TShippingInfoGraphql,
> = () => TBuilder<TModel>;
35 changes: 35 additions & 0 deletions models/cart/src/tax-portion/builders.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TaxPortionRest, TaxPortionGraphql } from './index';

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

expect(restModel).toEqual(
expect.objectContaining({
name: null,
rate: expect.any(Number),
amount: expect.objectContaining({
currencyCode: expect.any(String),
centAmount: expect.any(Number),
}),
})
);
});

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

expect(graphqlModel).toEqual(
expect.objectContaining({
name: null,
rate: expect.any(Number),
amount: expect.objectContaining({
currencyCode: expect.any(String),
centAmount: expect.any(Number),
__typename: 'Money',
}),
__typename: 'TaxPortion',
})
);
});
});
Loading

0 comments on commit 3afc6db

Please sign in to comment.