@@ -2,52 +2,42 @@ import { ApolloError } from '@apollo/client/core';
2
2
import { GraphQLError } from 'graphql' ;
3
3
4
4
import {
5
+ DaffCartNotFoundError ,
5
6
DaffInvalidCouponCodeError ,
6
7
DaffProductOutOfStockError ,
8
+ DaffUnauthorizedForCartError ,
7
9
} from '@daffodil/cart/driver' ;
8
10
11
+ import { MagentoCartGraphQlErrorCode } from './codes' ;
9
12
import { transformCartMagentoError } from './transform' ;
10
13
11
14
describe ( '@daffodil/cart/driver/magento | transformCartMagentoError' , ( ) => {
12
15
let apolloError : ApolloError ;
13
16
let graphQlError : GraphQLError ;
14
17
let transformedError : Error ;
15
18
16
- describe ( 'when the GraphQL error is an invalid coupon code error' , ( ) => {
17
- beforeEach ( ( ) => {
18
- graphQlError = new GraphQLError ( 'The coupon code isn\'t valid. Verify the code and try again.' ) ;
19
+ it ( 'should transform error codes correctly' , ( ) => {
20
+ const errors = [
21
+ { message : 'The coupon code isn\'t valid. Verify the code and try again.' , category : undefined , type : DaffInvalidCouponCodeError } ,
22
+ { message : 'There are no source items with the in stock status' , category : undefined , type : DaffProductOutOfStockError } ,
23
+ { message : 'Could not find a cart with ID "asdasdasd"' , category : MagentoCartGraphQlErrorCode . CART_NOT_FOUND , type : DaffCartNotFoundError } ,
24
+ { message : 'Could not find a cart with ID "asdasdasd"' , category : undefined , type : DaffCartNotFoundError } ,
25
+ { message : 'Cart does not contain products' , category : undefined , type : Error } ,
26
+ { message : 'The current customer isn\'t authorized' , category : undefined , type : DaffUnauthorizedForCartError } ,
27
+ ] ;
28
+
29
+ errors . forEach ( ( el ) => {
30
+ graphQlError = new GraphQLError ( el . message ) ;
31
+ if ( el . category ) {
32
+ graphQlError . extensions . category = el . category ;
33
+ }
19
34
apolloError = new ApolloError ( {
20
35
graphQLErrors : [ graphQlError ] ,
21
36
} ) ;
22
-
23
- transformedError = transformCartMagentoError ( apolloError ) ;
24
- } ) ;
25
-
26
- it ( 'should return a DaffInvalidCouponCodeError' , ( ) => {
27
- expect ( transformedError ) . toEqual ( jasmine . any ( DaffInvalidCouponCodeError ) ) ;
28
- } ) ;
29
-
30
- it ( 'should return an error containing the GraphQL error message' , ( ) => {
31
- expect ( transformedError . message ) . toContain ( graphQlError . message ) ;
32
- } ) ;
33
- } ) ;
34
-
35
- describe ( 'when the GraphQL error is a no source items with in stock status error' , ( ) => {
36
- beforeEach ( ( ) => {
37
- graphQlError = new GraphQLError ( 'There are no source items with the in stock status' ) ;
38
- apolloError = new ApolloError ( {
39
- graphQLErrors : [ graphQlError ] ,
40
- } ) ;
41
-
42
37
transformedError = transformCartMagentoError ( apolloError ) ;
43
- } ) ;
44
-
45
- it ( 'should return a DaffProductOutOfStockError' , ( ) => {
46
- expect ( transformedError ) . toEqual ( jasmine . any ( DaffProductOutOfStockError ) ) ;
47
- } ) ;
48
38
49
- it ( 'should return an error containing the GraphQL error message' , ( ) => {
50
- expect ( transformedError . message ) . toContain ( graphQlError . message ) ;
39
+ expect ( transformedError ) . toEqual ( jasmine . any ( el . type ) ) ;
40
+ expect ( transformedError . message ) . toContain ( el . message ) ;
51
41
} ) ;
52
42
} ) ;
53
43
} ) ;
0 commit comments