@@ -2,8 +2,6 @@ import { expect, sinon, fetchMock } from '../test-helper';
2
2
import { Author , Book , Genre } from '../fixtures' ;
3
3
import uuid from '../../src/util/uuid' ;
4
4
5
- let serverResponse ;
6
-
7
5
const resetMocks = function ( ) {
8
6
fetchMock . restore ( ) ;
9
7
@@ -61,15 +59,31 @@ const resetMocks = function() {
61
59
}
62
60
}
63
61
}
62
+ } ,
63
+ {
64
+ code : 'unprocessable_entity' ,
65
+ status : '422' ,
66
+ title : 'Validation Error' ,
67
+ detail : 'base some error' ,
68
+ meta : {
69
+ relationship : {
70
+ name : 'books' ,
71
+ type : 'books' ,
72
+ [ 'temp-id' ] : 'abc1' ,
73
+ relationship : {
74
+ name : 'genre' ,
75
+ type : 'genres' ,
76
+ id : '1' ,
77
+ attribute : 'base' ,
78
+ message : 'some error'
79
+ }
80
+ }
81
+ }
64
82
}
65
83
]
66
84
}
67
85
}
68
86
} ) ;
69
-
70
- fetchMock . post ( 'http://example.com/api/v1/people' , function ( url , payload ) {
71
- return serverResponse ;
72
- } ) ;
73
87
}
74
88
75
89
let instance ;
@@ -97,19 +111,50 @@ describe('validations', function() {
97
111
uuid . generate [ 'restore' ] ( ) ;
98
112
} ) ;
99
113
100
- // todo on next save, remove errs
101
114
it ( 'applies errors to the instance' , function ( done ) {
102
115
instance . save ( { with : { books : 'genre' } } ) . then ( ( success ) => {
103
116
expect ( instance . isPersisted ( ) ) . to . eq ( false ) ;
104
117
expect ( success ) . to . eq ( false ) ;
105
118
expect ( instance . errors ) . to . deep . equal ( {
106
- first_name : 'cannot be blank' ,
107
- last_name : 'cannot be blank'
119
+ firstName : 'cannot be blank' ,
120
+ lastName : 'cannot be blank'
108
121
} ) ;
109
122
done ( ) ;
110
123
} ) ;
111
124
} ) ;
112
125
126
+ describe ( 'when camelizeKeys is false' , function ( ) {
127
+ beforeEach ( function ( ) {
128
+ instance . klass . camelizeKeys = false
129
+ } ) ;
130
+
131
+ afterEach ( function ( ) {
132
+ instance . klass . camelizeKeys = true
133
+ } ) ;
134
+
135
+ it ( 'does not camelize the error keys' , function ( ) {
136
+ instance . save ( { with : { books : 'genre' } } ) . then ( ( success ) => {
137
+ expect ( instance . errors ) . to . deep . equal ( {
138
+ first_name : 'cannot be blank' ,
139
+ last_name : 'cannot be blank'
140
+ } ) ;
141
+ } ) ;
142
+ } ) ;
143
+ } ) ;
144
+
145
+ it ( 'clears errors on save' , function ( done ) {
146
+ fetchMock . restore ( )
147
+ fetchMock . mock ( {
148
+ matcher : '*' ,
149
+ response : { data : { id : '1' , type : 'employees' } }
150
+ } ) ;
151
+ instance . errors = { foo : 'bar' }
152
+ instance . save ( ) . then ( ( ) => {
153
+ expect ( instance . errors ) . to . deep . eq ( { } )
154
+ done ( )
155
+ } ) ;
156
+ } ) ;
157
+
113
158
it ( 'instantiates a new error object instance after save' , function ( done ) {
114
159
let originalErrors = instance . errors = { foo : 'bar' } ;
115
160
let result = instance . save ( { with : { books : 'genre' } } ) ;
@@ -151,8 +196,11 @@ describe('validations', function() {
151
196
instance . save ( { with : { books : 'genre' } } ) . then ( ( success ) => {
152
197
expect ( instance . isPersisted ( ) ) . to . eq ( false ) ;
153
198
expect ( success ) . to . eq ( false ) ;
199
+
200
+ // note we're validating multiple properties
154
201
expect ( instance . books [ 0 ] . genre . errors ) . to . deep . equal ( {
155
202
name : 'cannot be blank' ,
203
+ base : 'some error'
156
204
} ) ;
157
205
done ( ) ;
158
206
} ) ;
0 commit comments