@@ -6,14 +6,28 @@ import APIError from '../src/error';
66
77const matcher = '*' ;
88
9- const ResponseGood = new Response (
9+ const Response200 = new Response (
1010 JSON . stringify ( { sample : 'data' } ) ,
1111 {
1212 status : 200
1313 }
1414) ;
1515
16- const ResponseBad = new Response (
16+ const Response201 = new Response (
17+ null ,
18+ {
19+ status : 201
20+ }
21+ ) ;
22+
23+ const Response400 = new Response (
24+ JSON . stringify ( { sample : 'not found' } ) ,
25+ {
26+ status : 400
27+ }
28+ ) ;
29+
30+ const Response500 = new Response (
1731 null ,
1832 {
1933 status : 500
@@ -36,16 +50,37 @@ test.serial('default params', async t => {
3650} ) ;
3751
3852test . serial ( '200 reponse' , async t => {
39- fetchMock . get ( matcher , ResponseGood ) ;
53+ fetchMock . get ( matcher , Response200 ) ;
4054 const result = await callAPIMethod ( ) ;
4155
4256 t . deepEqual ( result , { sample : 'data' } , 'correct response body' ) ;
4357
4458 fetchMock . restore ( ) ;
4559} ) ;
4660
61+ test . serial ( '201 reponse' , async t => {
62+ fetchMock . get ( matcher , Response201 ) ;
63+ const result = await callAPIMethod ( ) ;
64+
65+ t . is ( result , null , 'incorrect response body' ) ;
66+
67+ fetchMock . restore ( ) ;
68+ } ) ;
69+
70+ test . serial ( '400 reponse' , async t => {
71+ fetchMock . get ( matcher , Response400 ) ;
72+
73+ const result = await callAPIMethod ( ) ;
74+
75+ t . true ( result instanceof Error , 'instance of Error' ) ;
76+ t . true ( result instanceof APIError , 'instance of APIError' ) ;
77+ t . deepEqual ( result . body , { sample : 'not found' } , 'correct error body' ) ;
78+
79+ fetchMock . restore ( ) ;
80+ } ) ;
81+
4782test . serial ( '500 reponse' , async t => {
48- fetchMock . get ( matcher , ResponseBad ) ;
83+ fetchMock . get ( matcher , Response500 ) ;
4984
5085 const result = await callAPIMethod ( ) ;
5186
@@ -56,7 +91,7 @@ test.serial('500 reponse', async t => {
5691} ) ;
5792
5893test . serial ( 'fetch options' , async t => {
59- fetchMock . post ( matcher , ResponseGood ) ;
94+ fetchMock . post ( matcher , Response200 ) ;
6095
6196 const APINamespace = 'rest-api' ;
6297 const namespace = 'user' ;
@@ -70,7 +105,7 @@ test.serial('fetch options', async t => {
70105 } ,
71106 method : 'text'
72107 } ;
73- const spyResponseGood = sinon . spy ( ResponseGood , 'text' ) ;
108+ const spyResponse200 = sinon . spy ( Response200 , 'text' ) ;
74109
75110 fetchMock . post ( matcher , { } ) ;
76111
@@ -83,7 +118,7 @@ test.serial('fetch options', async t => {
83118 method : 'POST' ,
84119 mode : 'cors'
85120 } , 'correct options' ) ;
86- t . true ( spyResponseGood . calledOnce ) ;
121+ t . true ( spyResponse200 . calledOnce ) ;
87122
88123 const newMethodOptions = {
89124 ...methodOptions ,
@@ -101,12 +136,12 @@ test.serial('fetch options', async t => {
101136 body : newMethodOptions . body
102137 } , 'correct options' ) ;
103138
104- spyResponseGood . restore ( ) ;
139+ spyResponse200 . restore ( ) ;
105140 fetchMock . restore ( ) ;
106141} ) ;
107142
108143test . serial ( 'unsupported Response method' , async t => {
109- fetchMock . get ( matcher , ResponseGood ) ;
144+ fetchMock . get ( matcher , Response200 ) ;
110145
111146 const APINamespace = 'rest-api' ;
112147 const namespace = 'user' ;
0 commit comments