Skip to content

Commit

Permalink
fix(cart): adjust child models
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosCortizasCT committed Dec 9, 2024
1 parent 61fa16a commit 46902ab
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 18 deletions.
15 changes: 14 additions & 1 deletion models/cart/src/shipping-info/fields-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CentPrecisionMoney } from '@commercetools-test-data/commons';
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';
Expand Down Expand Up @@ -31,4 +34,14 @@ export const graphqlFieldsConfig: TModelFieldsConfig<TShippingInfoGraphql> = {
taxCategoryRef: null,
__typename: 'ShippingInfo',
},
postBuild: (model) => {
return {
shippingMethodRef: model.shippingMethod
? Reference.presets
.shippingMethodReference()
.id(model.shippingMethod.id)
.buildGraphql()
: model.shippingMethodRef,
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,63 @@ 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();
console.log('restExamplePreset', restExamplePreset);

expect(restExamplePreset).toEqual(
expect.objectContaining({
deliveries: expect.any(Array),
price: expect.objectContaining({
currencyCode: 'EUR',
}),
shipingMethod: expect.objectContaining({
shippingMethod: expect.objectContaining({
typeId: 'shipping-method',
}),
shippingMethodName: 'shipping-method-name',
shippingRate: expect.objectContaining({
price: expect.objectContaining({
currencyCode: 'EUR',
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',
}),
shipingMethod: expect.objectContaining({
shippingMethod: expect.objectContaining({
__typename: 'ShippingMethod',
}),
shippingMethodRef: expect.objectContaining({
typeId: 'shipping-method',
__typename: 'Reference',
}),
shippingMethodName: 'shipping-method-name',
shippingRate: expect.objectContaining({
__typename: 'ShippingRate',
Expand All @@ -50,6 +70,7 @@ describe('with all fields', () => {
taxRate: expect.objectContaining({
__typename: 'TaxRate',
}),
__typename: 'ShippingInfo',
})
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Money, Reference } from '@commercetools-test-data/commons';
import type { TBuilder } from '@commercetools-test-data/core';
import { ShippingRate } from '@commercetools-test-data/shipping-method';
import {
ShippingMethod,
ShippingRate,
} from '@commercetools-test-data/shipping-method';
import { TaxRate } from '@commercetools-test-data/tax-category';
import {
TaxedItemPriceGraphql,
Expand All @@ -15,18 +18,17 @@ const populatePreset = <T extends TShippingInfoRest | TShippingInfoGraphql>(
builder
.deliveries([])
.price(Money.presets.withCurrency('EUR'))
.shippingMethod(Reference.presets.shippingMethodReference())
.shippingMethodName('shipping-method-name')
.shippingRate(ShippingRate.random())
.taxCategory(Reference.presets.taxCategoryReference())
.taxRate(TaxRate.presets.withAllFields());

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

export const graphqlPreset = (): TBuilder<TShippingInfoGraphql> =>
populatePreset(ShippingInfoGraphql.random()).taxedPrice(
TaxedItemPriceGraphql.presets.withAllFields()
);
populatePreset(ShippingInfoGraphql.random())
.shippingMethod(ShippingMethod.random())
.taxedPrice(TaxedItemPriceGraphql.presets.withAllFields());
16 changes: 14 additions & 2 deletions models/cart/src/taxed-item-price/builders.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ describe('TaxedItemPrice Builder', () => {
currencyCode: expect.any(String),
centAmount: expect.any(Number),
}),
taxPortions: [],
taxPortions: expect.arrayContaining([
expect.objectContaining({
rate: expect.any(Number),
amount: expect.objectContaining({
currencyCode: expect.any(String),
centAmount: expect.any(Number),
}),
}),
]),
totalTax: null,
})
);
Expand All @@ -30,7 +38,11 @@ describe('TaxedItemPrice Builder', () => {
totalGross: expect.objectContaining({
__typename: 'Money',
}),
taxPortions: [],
taxPortions: expect.arrayContaining([
expect.objectContaining({
__typename: 'TaxPortion',
}),
]),
totalTax: null,
__typename: 'TaxedItemPrice',
})
Expand Down
5 changes: 2 additions & 3 deletions models/cart/src/taxed-item-price/fields-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ import type { TTaxedItemPriceGraphql, TTaxedItemPriceRest } from './types';
const commonFieldsConfig = {
totalNet: fake(() => Money.random()),
totalGross: fake(() => Money.random()),
taxPortions: [],
totalTax: null,
};

export const restFieldsConfig: TModelFieldsConfig<TTaxedItemPriceRest> = {
fields: {
...commonFieldsConfig,
taxPortions: fake(() => TaxPortionRest.random()),
taxPortions: fake(() => [TaxPortionRest.random()]),
},
};

export const graphqlFieldsConfig: TModelFieldsConfig<TTaxedItemPriceGraphql> = {
fields: {
...commonFieldsConfig,
taxPortions: fake(() => TaxPortionGraphql.random()),
taxPortions: fake(() => [TaxPortionGraphql.random()]),
__typename: 'TaxedItemPrice',
},
};

0 comments on commit 46902ab

Please sign in to comment.