Skip to content

Commit

Permalink
test(core): Add e2e tests for PR #1029
Browse files Browse the repository at this point in the history
Relates to #1029, relates to #1028
  • Loading branch information
michaelbromley committed Aug 18, 2021
1 parent a3aa3e2 commit bd32a6c
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions packages/core/e2e/stock-control.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
} from './graphql/shared-definitions';
import {
ADD_ITEM_TO_ORDER,
ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
ADD_PAYMENT,
GET_PRODUCT_WITH_STOCK_LEVEL,
SET_SHIPPING_ADDRESS,
Expand All @@ -63,6 +64,9 @@ describe('Stock control', () => {
paymentOptions: {
paymentMethodHandlers: [testSuccessfulPaymentMethod, twoStagePaymentMethod],
},
customFields: {
OrderLine: [{ name: 'customization', type: 'string', nullable: true }],
},
}),
);

Expand Down Expand Up @@ -924,6 +928,107 @@ describe('Stock control', () => {
});
});
});

// https://github.com/vendure-ecommerce/vendure/issues/1028
describe('OrderLines with same variant but different custom fields', () => {
let orderId: string;

it('correctly allocates stock', async () => {
await shopClient.asUserWithCredentials('[email protected]', 'test');

const product = await getProductWithStockMovement('T_2');
const [variant1, variant2, variant3] = product!.variants;

expect(variant2.stockAllocated).toBe(0);

await shopClient.query<AddItemToOrder.Mutation, any>(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
productVariantId: variant2.id,
quantity: 1,
customFields: {
customization: 'foo',
},
});
const { addItemToOrder } = await shopClient.query<AddItemToOrder.Mutation, any>(
ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS,
{
productVariantId: variant2.id,
quantity: 1,
customFields: {
customization: 'bar',
},
},
);

orderGuard.assertSuccess(addItemToOrder);
orderId = addItemToOrder.id;
// Assert that separate order lines have been created
expect(addItemToOrder.lines.length).toBe(2);

await shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(
SET_SHIPPING_ADDRESS,
{
input: {
streetLine1: '1 Test Street',
countryCode: 'GB',
} as CreateAddressInput,
},
);
await shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(
TRANSITION_TO_STATE,
{
state: 'ArrangingPayment',
},
);
const { addPaymentToOrder: order } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
>(ADD_PAYMENT, {
input: {
method: testSuccessfulPaymentMethod.code,
metadata: {},
} as PaymentInput,
});
orderGuard.assertSuccess(order);

const product2 = await getProductWithStockMovement('T_2');
const [variant1_2, variant2_2, variant3_2] = product2!.variants;

expect(variant2_2.stockAllocated).toBe(2);
});

it('correctly creates Sales', async () => {
const product = await getProductWithStockMovement('T_2');
const [variant1, variant2, variant3] = product!.variants;

expect(variant2.stockOnHand).toBe(3);

const { order } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
id: orderId,
});

await adminClient.query<CreateFulfillment.Mutation, CreateFulfillment.Variables>(
CREATE_FULFILLMENT,
{
input: {
lines: order?.lines.map(l => ({ orderLineId: l.id, quantity: l.quantity })) ?? [],
handler: {
code: manualFulfillmentHandler.code,
arguments: [
{ name: 'method', value: 'test method' },
{ name: 'trackingCode', value: 'ABC123' },
],
},
},
},
);

const product2 = await getProductWithStockMovement('T_2');
const [variant1_2, variant2_2, variant3_2] = product2!.variants;

expect(variant2_2.stockAllocated).toBe(0);
expect(variant2_2.stockOnHand).toBe(1);
});
});
});

const UPDATE_STOCK_ON_HAND = gql`
Expand Down

0 comments on commit bd32a6c

Please sign in to comment.