Skip to content

Commit

Permalink
feat(cart): transform Magento driver errors (#2449)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed May 24, 2023
1 parent 9891874 commit 229d4d0
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 6 deletions.
24 changes: 24 additions & 0 deletions libs/cart/driver/magento/src/cart-billing-address.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartBillingAddressService',
});

describe('get | getting the billing address', () => {
describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.get(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(getBillingAddress([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should call the transformer with the correct argument', done => {
service.get(cartId).subscribe(() => {
expect(magentoBillingAddressTransformerSpy.transform).toHaveBeenCalledWith(jasmine.objectContaining(mockMagentoBillingAddress));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class DaffMagentoCartBillingAddressService implements DaffCartBillingAddr
})
: null,
),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand Down
49 changes: 48 additions & 1 deletion libs/cart/driver/magento/src/cart-item.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ describe('@daffodil/cart/driver/magento | CartItemService', () => {
});

describe('list | getting a list of cart items', () => {
describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.list(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(listCartItems([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

describe('when the products response contains nully values', () => {
beforeEach(() => {
mockListCartItemResponse = {
Expand Down Expand Up @@ -353,7 +377,6 @@ describe('@daffodil/cart/driver/magento | CartItemService', () => {
});
});


describe('update | updates a cart item', () => {
describe('when the call to the Magento API is successful', () => {
let qty;
Expand Down Expand Up @@ -483,6 +506,30 @@ describe('@daffodil/cart/driver/magento | CartItemService', () => {
});

describe('delete | removes a cart item', () => {
describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.delete(cartId, mockDaffCartItem.id).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(removeCartItem([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

beforeEach(() => {
mockMagentoCart.items = [];
mockDaffCart.items = [];
Expand Down
2 changes: 2 additions & 0 deletions libs/cart/driver/magento/src/cart-item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class DaffMagentoCartItemService implements DaffCartItemServiceInterface
}).pipe(
map(result => result.data.cart.items.filter(item => !!item)),
map(items => items.map(item => transformMagentoCartItem(daffTransformMagentoCartItem(item), item, this.cartItemTransforms))),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand Down Expand Up @@ -132,6 +133,7 @@ export class DaffMagentoCartItemService implements DaffCartItemServiceInterface
fetchPolicy: 'network-only',
}).pipe(
map(result => this.cartTransformer.transform(result.data.removeItemFromCart.cart)),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand Down
27 changes: 27 additions & 0 deletions libs/cart/driver/magento/src/cart-payment-methods.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
ApolloTestingModule,
APOLLO_TESTING_CACHE,
} from 'apollo-angular/testing';
import { GraphQLError } from 'graphql';
import { catchError } from 'rxjs';

import { DaffCartPaymentMethod } from '@daffodil/cart';
import {
Expand All @@ -20,6 +22,7 @@ import { schema } from '@daffodil/driver/magento';
import { DaffMagentoCartPaymentMethodsService } from './cart-payment-methods.service';
import { listPaymentMethods } from './queries/public_api';


describe('@daffodil/cart/driver/magento | CartPaymentMethodsService', () => {
let service: DaffMagentoCartPaymentMethodsService;
let controller: ApolloTestingController;
Expand Down Expand Up @@ -94,6 +97,30 @@ describe('@daffodil/cart/driver/magento | CartPaymentMethodsService', () => {
mockMagentoPaymentMethod.code = method;
});

describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.list(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(listPaymentMethods([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should call the transformer with the correct argument', done => {
service.list(cartId).subscribe(() => {
expect(magentoCartPaymentTransformerSpy).toHaveBeenCalledWith(mockMagentoPaymentMethod);
Expand Down
12 changes: 10 additions & 2 deletions libs/cart/driver/magento/src/cart-payment-methods.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import {
} from '@angular/core';
import { Apollo } from 'apollo-angular';
import { DocumentNode } from 'graphql';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
Observable,
throwError,
} from 'rxjs';
import {
catchError,
map,
} from 'rxjs/operators';

import {
DaffCartPaymentMethod,
DaffCart,
} from '@daffodil/cart';
import { DaffCartPaymentMethodsServiceInterface } from '@daffodil/cart/driver';

import { transformCartMagentoError } from './errors/transform';
import { DAFF_CART_MAGENTO_EXTRA_CART_FRAGMENTS } from './injection-tokens/public_api';
import { listPaymentMethods } from './queries/public_api';
import { MagentoListPaymentMethodsResponse } from './queries/responses/list-payment-methods';
Expand Down Expand Up @@ -40,6 +47,7 @@ export class DaffMagentoCartPaymentMethodsService implements DaffCartPaymentMeth
fetchPolicy: 'network-only',
}).pipe(
map(result => result.data.cart.available_payment_methods.map(item => this.paymentTransformer.transform(item))),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}
}
72 changes: 72 additions & 0 deletions libs/cart/driver/magento/src/cart-payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,30 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartPaymentService', () =>
});

describe('get | getting the selected payment method', () => {
describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.get(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(getSelectedPaymentMethod([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should call the transformer with the correct argument', done => {
service.get(cartId).subscribe(() => {
expect(magentoBillingRateTransformerSpy.transform).toHaveBeenCalledWith(jasmine.objectContaining(mockMagentoCartPaymentMethod));
Expand Down Expand Up @@ -238,6 +262,30 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartPaymentService', () =>
mockDaffCartPaymentMethod.method = method;
});

describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.update(cartId, mockDaffCartPaymentMethod).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(setSelectedPaymentMethod([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should return the correct value', done => {
service.update(cartId, mockDaffCartPaymentMethod).subscribe(result => {
expect(result.payment.method).toEqual(method);
Expand Down Expand Up @@ -379,6 +427,30 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartPaymentService', () =>
mockDaffCart.payment = null;
});

describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.remove(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(setSelectedPaymentMethod([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should return void', done => {
service.remove(cartId).subscribe(result => {
expect(result).toBeUndefined();
Expand Down
3 changes: 3 additions & 0 deletions libs/cart/driver/magento/src/cart-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class DaffMagentoCartPaymentService implements DaffCartPaymentServiceInte
fetchPolicy: 'network-only',
}).pipe(
map(result => this.paymentTransformer.transform(result.data.cart.selected_payment_method)),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand All @@ -86,6 +87,7 @@ export class DaffMagentoCartPaymentService implements DaffCartPaymentServiceInte
fetchPolicy: 'network-only',
}).pipe(
map(result => this.cartTransformer.transform(result.data.setPaymentMethodOnCart.cart)),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand All @@ -109,6 +111,7 @@ export class DaffMagentoCartPaymentService implements DaffCartPaymentServiceInte
fetchPolicy: 'network-only',
}).pipe(
map(() => undefined),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand Down
24 changes: 24 additions & 0 deletions libs/cart/driver/magento/src/cart-shipping-address.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ describe('@daffodil/cart/driver/magento | DaffMagentoCartShippingAddressService'
});

describe('get | getting the shipping address', () => {
describe('when the call to the Magento API is unsuccessful', () => {
it('should throw an Error', done => {
service.get(cartId).pipe(
catchError(err => {
expect(err).toEqual(jasmine.any(Error));
done();
return [];
}),
).subscribe();

const op = controller.expectOne(addTypenameToDocument(getShippingAddress([])));

op.graphqlErrors([new GraphQLError(
'Can\'t find a cart with that ID.',
null,
null,
null,
null,
null,
{ category: 'graphql-no-such-entity' },
)]);
});
});

it('should call the transformer with the correct argument', done => {
service.get(cartId).subscribe(() => {
// can't check for all args because available_shipping_methods and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class DaffMagentoCartShippingAddressService implements DaffCartShippingAd
})
: null,
),
catchError(error => throwError(() => transformCartMagentoError(error))),
);
}

Expand Down
Loading

0 comments on commit 229d4d0

Please sign in to comment.