1- import { GraphQLModel } from '@console /gramps' ;
1+ import { GraphQLModel } from '@gramps /gramps-express ' ;
22import Model from '../src/model' ;
33import Connector from '../src/connector' ;
44
@@ -16,18 +16,6 @@ const DATA_SOURCE_NAME = 'YourDataSource';
1616const connector = new Connector ( ) ;
1717const model = new Model ( { connector } ) ;
1818
19- // TODO: Update this error to match the error format returned by your endpoint.
20- const mockError = {
21- statusCode : 401 ,
22- error : {
23- description : 'test error' ,
24- error_code : 'TEST_ERROR' ,
25- } ,
26- options : {
27- uri : 'https://example.org/' ,
28- } ,
29- } ;
30-
3119describe ( `${ DATA_SOURCE_NAME } Model` , ( ) => {
3220 it ( 'inherits the GraphQLModel class' , ( ) => {
3321 expect ( model ) . toBeInstanceOf ( GraphQLModel ) ;
@@ -42,11 +30,11 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
4230 expect ( spy ) . toHaveBeenCalledWith ( '/data/1234' ) ;
4331 } ) ;
4432
45- it ( 'throws a BluemixGraphQLError if something goes wrong' , async ( ) => {
33+ it ( 'throws a GrampsError if something goes wrong' , async ( ) => {
4634 expect . assertions ( 1 ) ;
4735
4836 model . connector . get . mockImplementationOnce ( ( ) =>
49- Promise . reject ( mockError ) ,
37+ Promise . reject ( { no : 'good' } ) ,
5038 ) ;
5139
5240 try {
@@ -58,9 +46,17 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
5846 } ) ;
5947 } ) ;
6048
61- describe ( 'handleError()' , ( ) => {
62- it ( 'converts an error from the endpoint into a BluemixGraphQLError' , async ( ) => {
63- expect . assertions ( 6 ) ;
49+ describe ( 'throwError()' , ( ) => {
50+ // TODO: Update this error to match the error format returned by your endpoint.
51+ const mockError = {
52+ statusCode : 401 ,
53+ options : {
54+ uri : 'https://example.org/' ,
55+ } ,
56+ } ;
57+
58+ it ( 'converts an error from the endpoint into a GrampsError' , async ( ) => {
59+ expect . assertions ( 4 ) ;
6460
6561 /*
6662 * To simulate a failed call, we tell Jest to return a rejected Promise
@@ -74,14 +70,9 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
7470 // TODO: Update to use one of your model’s methods.
7571 await model . getById ( 1234 ) ;
7672 } catch ( error ) {
77- // Check that BluemixGraphQLError properly received the error detail.
73+ // Check that GrampsError properly received the error detail.
7874 expect ( error ) . toHaveProperty ( 'isBoom' , true ) ;
7975 expect ( error . output ) . toHaveProperty ( 'statusCode' , 401 ) ;
80- expect ( error . output . payload ) . toHaveProperty (
81- 'description' ,
82- 'test error' ,
83- ) ;
84- expect ( error . output . payload ) . toHaveProperty ( 'errorCode' , 'TEST_ERROR' ) ;
8576 expect ( error . output . payload ) . toHaveProperty (
8677 'targetEndpoint' ,
8778 'https://example.org/' ,
@@ -92,5 +83,21 @@ describe(`${DATA_SOURCE_NAME}Model`, () => {
9283 ) ;
9384 }
9485 } ) ;
86+
87+ it ( 'creates a default GrampsError if no custom error data is supplied' , async ( ) => {
88+ try {
89+ await model . throwError ( { } ) ;
90+ } catch ( error ) {
91+ expect ( error . output . statusCode ) . toBe ( 500 ) ;
92+ expect ( error . output . payload . errorCode ) . toBe (
93+ `${ DATA_SOURCE_NAME } Model_Error` ,
94+ ) ;
95+ expect ( error . output . payload . description ) . toBe ( 'Something went wrong.' ) ;
96+ expect ( error . output . payload . graphqlModel ) . toBe (
97+ `${ DATA_SOURCE_NAME } Model` ,
98+ ) ;
99+ expect ( error . output . payload . targetEndpoint ) . toBeNull ( ) ;
100+ }
101+ } ) ;
95102 } ) ;
96103} ) ;
0 commit comments