11import { createApiError } from 'core/api/index' ;
22import { clearError , setError } from 'core/actions/errors' ;
3+ import { API_ERROR_SIGNATURE_EXPIRED , ERROR_UNKNOWN } from 'core/constants' ;
34import errors , { initialState } from 'core/reducers/errors' ;
45
56export function createFakeApiError ( { fieldErrors = { } , nonFieldErrors } = { } ) {
@@ -23,15 +24,6 @@ describe('errors reducer', () => {
2324 assert . deepEqual ( errors ( undefined , { type : 'UNRELATED' } ) , initialState ) ;
2425 } ) ;
2526
26- it ( 'stores a simple error' , ( ) => {
27- const error = new Error ( 'some message' ) ;
28- const action = setError ( { id : 'some-id' , error } ) ;
29- const state = errors ( undefined , action ) ;
30- assert . deepEqual ( state [ action . payload . id ] , {
31- messages : [ 'An unexpected error occurred' ] ,
32- } ) ;
33- } ) ;
34-
3527 it ( 'handles API object responses' , ( ) => {
3628 const message = 'Authentication credentials were not provided.' ;
3729 const error = createApiError ( {
@@ -41,7 +33,10 @@ describe('errors reducer', () => {
4133 } ) ;
4234 const action = setError ( { id : 'some-id' , error } ) ;
4335 const state = errors ( undefined , action ) ;
44- assert . deepEqual ( state [ action . payload . id ] , { messages : [ message ] } ) ;
36+ assert . deepEqual ( state [ action . payload . id ] , {
37+ code : ERROR_UNKNOWN ,
38+ messages : [ message ] ,
39+ } ) ;
4540 } ) ;
4641
4742 it ( 'preserves existing errors' , ( ) => {
@@ -85,11 +80,13 @@ describe('errors reducer', () => {
8580 assert . equal ( state . action2 . messages [ 0 ] , 'action2' ) ;
8681 } ) ;
8782
88- it ( 'creates a default error message ' , ( ) => {
83+ it ( 'stores a generic error' , ( ) => {
8984 const action = setError ( { id : 'action1' , error : new Error ( 'any message' ) } ) ;
9085 const state = errors ( undefined , action ) ;
91- assert . deepEqual ( state [ action . payload . id ] . messages ,
92- [ 'An unexpected error occurred' ] ) ;
86+ assert . deepEqual ( state [ action . payload . id ] , {
87+ code : ERROR_UNKNOWN ,
88+ messages : [ ] ,
89+ } ) ;
9390 } ) ;
9491
9592 it ( 'gets non_field_errors from API error response' , ( ) => {
@@ -123,12 +120,41 @@ describe('errors reducer', () => {
123120 assert . include ( messages , 'password: sorry, it cannot be 1234' ) ;
124121 } ) ;
125122
126- it ( 'handles API responses without any messages' , ( ) => {
123+ it ( 'stores API responses when they do not have messages' , ( ) => {
127124 // This API error has no messages (hopefully this won't ever happen).
128125 const action = setError ( { id : 'some-id' , error : createFakeApiError ( ) } ) ;
129126 const state = errors ( undefined , action ) ;
130127 assert . deepEqual ( state [ action . payload . id ] , {
131- messages : [ 'An unexpected error occurred' ] ,
128+ code : ERROR_UNKNOWN ,
129+ messages : [ ] ,
130+ } ) ;
131+ } ) ;
132+
133+ it ( 'adds an error code' , ( ) => {
134+ const error = createApiError ( {
135+ response : { status : 401 } ,
136+ apiURL : 'https://some/api/endpoint' ,
137+ jsonResponse : {
138+ code : API_ERROR_SIGNATURE_EXPIRED ,
139+ detail : 'Any message about an expired signature.' ,
140+ } ,
132141 } ) ;
142+ const action = setError ( { id : 'some-id' , error } ) ;
143+ const state = errors ( undefined , action ) ;
144+ assert . equal ( state [ action . payload . id ] . code , API_ERROR_SIGNATURE_EXPIRED ) ;
145+ } ) ;
146+
147+ it ( 'does not turn an error code into a message' , ( ) => {
148+ const error = createApiError ( {
149+ response : { status : 401 } ,
150+ apiURL : 'https://some/api/endpoint' ,
151+ jsonResponse : {
152+ code : API_ERROR_SIGNATURE_EXPIRED ,
153+ detail : 'Some message.' ,
154+ } ,
155+ } ) ;
156+ const action = setError ( { id : 'some-id' , error } ) ;
157+ const state = errors ( undefined , action ) ;
158+ assert . deepEqual ( state [ action . payload . id ] . messages , [ 'Some message.' ] ) ;
133159 } ) ;
134160} ) ;
0 commit comments