@@ -40,15 +40,33 @@ describe('redaxios', () => {
4040 expect ( res . data ) . toEqual ( 'some example content' ) ;
4141 } ) ;
4242
43- it ( 'should request JSON' , async ( ) => {
44- const req = axios . get ( jsonExample , {
45- responseType : 'json'
43+ describe ( 'responseType' , ( ) => {
44+ it ( 'should default to JSON' , async ( ) => {
45+ const res = await axios . get ( jsonExample ) ;
46+ expect ( res . data ) . toEqual ( { hello : 'world' } ) ;
47+ } ) ;
48+
49+ it ( 'should force JSON for responseType:json' , async ( ) => {
50+ const res = await axios . get ( jsonExample , {
51+ responseType : 'json'
52+ } ) ;
53+ expect ( res . data ) . toEqual ( { hello : 'world' } ) ;
54+ } ) ;
55+
56+ it ( 'should fall back to null for failed JSON parse' , async ( ) => {
57+ const res = await axios . get ( textExample , {
58+ responseType : 'json'
59+ } ) ;
60+ expect ( res . data ) . toEqual ( undefined ) ;
61+ } ) ;
62+
63+ it ( 'should still parse JSON when responseType:text' , async ( ) => {
64+ // this is just how axios works
65+ const res = await axios . get ( jsonExample , {
66+ responseType : 'text'
67+ } ) ;
68+ expect ( res . data ) . toEqual ( { hello : 'world' } ) ;
4669 } ) ;
47- expect ( req ) . toBeInstanceOf ( Promise ) ;
48- const res = await req ;
49- expect ( res ) . toBeInstanceOf ( Object ) ;
50- expect ( res . status ) . toEqual ( 200 ) ;
51- expect ( res . data ) . toEqual ( { hello : 'world' } ) ;
5270 } ) ;
5371
5472 it ( 'response should be parsed json' , async ( ) => {
@@ -227,7 +245,7 @@ describe('redaxios', () => {
227245 const res = await req ;
228246 expect ( res ) . toBeInstanceOf ( Object ) ;
229247 expect ( res . status ) . toEqual ( 200 ) ;
230- expect ( JSON . parse ( res . data ) ) . toEqual ( { hello : 'world' } ) ;
248+ expect ( res . data ) . toEqual ( { hello : 'world' } ) ;
231249 } ) ;
232250
233251 it ( 'should support params and paramsSerializer options' , async ( ) => {
0 commit comments