Skip to content

Commit

Permalink
fix(core): Correctly handle adjustOrderLine with quantity 0
Browse files Browse the repository at this point in the history
Fixes #435
  • Loading branch information
michaelbromley committed Aug 20, 2020
1 parent cbfd499 commit 7381d3d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
37 changes: 30 additions & 7 deletions packages/core/e2e/shop-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import gql from 'graphql-tag';
import path from 'path';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';

import {
testErrorPaymentMethod,
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Shop orders', () => {
it('availableCountries returns enabled countries', async () => {
// disable Austria
const { countries } = await adminClient.query<GetCountryList.Query>(GET_COUNTRY_LIST, {});
const AT = countries.items.find((c) => c.code === 'AT')!;
const AT = countries.items.find(c => c.code === 'AT')!;
await adminClient.query<UpdateCountry.Mutation, UpdateCountry.Variables>(UPDATE_COUNTRY, {
input: {
id: AT.id,
Expand All @@ -114,7 +114,7 @@ describe('Shop orders', () => {

const result = await shopClient.query<GetAvailableCountries.Query>(GET_AVAILABLE_COUNTRIES);
expect(result.availableCountries.length).toBe(countries.items.length - 1);
expect(result.availableCountries.find((c) => c.id === AT.id)).toBeUndefined();
expect(result.availableCountries.find(c => c.id === AT.id)).toBeUndefined();
});

describe('ordering as anonymous user', () => {
Expand Down Expand Up @@ -212,6 +212,29 @@ describe('Shop orders', () => {
expect(adjustOrderLine!.lines[0].quantity).toBe(50);
});

it('adjustOrderLine with quantity 0 removes the line', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_3',
quantity: 3,
});
expect(addItemToOrder!.lines.length).toBe(2);
expect(addItemToOrder!.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);

const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: addItemToOrder?.lines[1].id!,
quantity: 0,
});

expect(adjustOrderLine!.lines.length).toBe(1);
expect(adjustOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
});

it(
'adjustOrderLine errors when going beyond orderItemsLimit',
assertThrowsWithMessage(async () => {
Expand Down Expand Up @@ -264,7 +287,7 @@ describe('Shop orders', () => {
quantity: 3,
});
expect(addItemToOrder!.lines.length).toBe(2);
expect(addItemToOrder!.lines.map((i) => i.productVariant.id)).toEqual(['T_1', 'T_3']);
expect(addItemToOrder!.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);

const { removeOrderLine } = await shopClient.query<
RemoveItemFromOrder.Mutation,
Expand All @@ -273,7 +296,7 @@ describe('Shop orders', () => {
orderLineId: firstOrderLineId,
});
expect(removeOrderLine!.lines.length).toBe(1);
expect(removeOrderLine!.lines.map((i) => i.productVariant.id)).toEqual(['T_3']);
expect(removeOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
});

it(
Expand Down Expand Up @@ -572,7 +595,7 @@ describe('Shop orders', () => {
quantity: 3,
});
expect(addItemToOrder!.lines.length).toBe(2);
expect(addItemToOrder!.lines.map((i) => i.productVariant.id)).toEqual(['T_1', 'T_3']);
expect(addItemToOrder!.lines.map(i => i.productVariant.id)).toEqual(['T_1', 'T_3']);

const { removeOrderLine } = await shopClient.query<
RemoveItemFromOrder.Mutation,
Expand All @@ -581,7 +604,7 @@ describe('Shop orders', () => {
orderLineId: firstOrderLineId,
});
expect(removeOrderLine!.lines.length).toBe(1);
expect(removeOrderLine!.lines.map((i) => i.productVariant.id)).toEqual(['T_3']);
expect(removeOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_3']);
});

it('nextOrderStates returns next valid states', async () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/api/resolvers/shop/shop-order.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ShopOrderResolver {
skip: 0,
take: 99999,
})
.then((data) => data.items);
.then(data => data.items);
}

@Query()
Expand Down Expand Up @@ -238,6 +238,9 @@ export class ShopOrderResolver {
@Ctx() ctx: RequestContext,
@Args() args: MutationAdjustOrderLineArgs,
): Promise<Order> {
if (args.quantity === 0) {
return this.removeOrderLine(ctx, { orderLineId: args.orderLineId });
}
const order = await this.getOrderFromContext(ctx, true);
return this.orderService.adjustOrderLine(
ctx,
Expand Down

0 comments on commit 7381d3d

Please sign in to comment.