@@ -4,85 +4,93 @@ import { tempId } from "../../src/util/temp-id"
4
4
import { JSORMBase , ModelRecord } from "../../src/model"
5
5
import { ValidationError } from "../../src/validation-errors"
6
6
7
+ const mockErrors = {
8
+ firstName : {
9
+ code : "unprocessable_entity" ,
10
+ status : "422" ,
11
+ title : "Validation Error" ,
12
+ detail : "First Name cannot be blank" ,
13
+ meta : { attribute : "first_name" , message : "cannot be blank" }
14
+ } ,
15
+ lastName : {
16
+ code : "unprocessable_entity" ,
17
+ status : "422" ,
18
+ title : "Validation Error" ,
19
+ detail : "Last Name cannot be blank" ,
20
+ meta : { attribute : "last-name" , message : "cannot be blank" }
21
+ } ,
22
+ bookTitle : {
23
+ code : "unprocessable_entity" ,
24
+ status : "422" ,
25
+ title : "Validation Error" ,
26
+ detail : "Title cannot be blank" ,
27
+ meta : {
28
+ relationship : {
29
+ name : "books" ,
30
+ type : "books" ,
31
+ [ "temp-id" ] : "abc1" ,
32
+ attribute : "title" ,
33
+ message : "cannot be blank"
34
+ }
35
+ }
36
+ } ,
37
+ bookGenreName : {
38
+ code : "unprocessable_entity" ,
39
+ status : "422" ,
40
+ title : "Validation Error" ,
41
+ detail : "Name cannot be blank" ,
42
+ meta : {
43
+ relationship : {
44
+ name : "books" ,
45
+ type : "books" ,
46
+ [ "temp-id" ] : "abc1" ,
47
+ relationship : {
48
+ name : "genre" ,
49
+ type : "genres" ,
50
+ id : "1" ,
51
+ attribute : "name" ,
52
+ message : "cannot be blank"
53
+ }
54
+ }
55
+ }
56
+ } ,
57
+ bookGenreBase : {
58
+ code : "unprocessable_entity" ,
59
+ status : "422" ,
60
+ title : "Validation Error" ,
61
+ detail : "base some error" ,
62
+ meta : {
63
+ relationship : {
64
+ name : "books" ,
65
+ type : "books" ,
66
+ [ "temp-id" ] : "abc1" ,
67
+ relationship : {
68
+ name : "genre" ,
69
+ type : "genres" ,
70
+ id : "1" ,
71
+ attribute : "base" ,
72
+ message : "some error"
73
+ }
74
+ }
75
+ }
76
+ }
77
+ } as any
78
+
7
79
const resetMocks = ( ) => {
8
80
fetchMock . restore ( )
9
81
82
+ let errors = [ ]
83
+
84
+ for ( let key in mockErrors ) {
85
+ errors . push ( mockErrors [ key ] )
86
+ }
87
+
10
88
fetchMock . mock ( {
11
89
matcher : "*" ,
12
90
response : {
13
91
status : 422 ,
14
92
body : {
15
- errors : [
16
- {
17
- code : "unprocessable_entity" ,
18
- status : "422" ,
19
- title : "Validation Error" ,
20
- detail : "First Name cannot be blank" ,
21
- meta : { attribute : "first_name" , message : "cannot be blank" }
22
- } ,
23
- {
24
- code : "unprocessable_entity" ,
25
- status : "422" ,
26
- title : "Validation Error" ,
27
- detail : "Last Name cannot be blank" ,
28
- meta : { attribute : "last-name" , message : "cannot be blank" }
29
- } ,
30
- {
31
- code : "unprocessable_entity" ,
32
- status : "422" ,
33
- title : "Validation Error" ,
34
- detail : "Title cannot be blank" ,
35
- meta : {
36
- relationship : {
37
- name : "books" ,
38
- type : "books" ,
39
- [ "temp-id" ] : "abc1" ,
40
- attribute : "title" ,
41
- message : "cannot be blank"
42
- }
43
- }
44
- } ,
45
- {
46
- code : "unprocessable_entity" ,
47
- status : "422" ,
48
- title : "Validation Error" ,
49
- detail : "Name cannot be blank" ,
50
- meta : {
51
- relationship : {
52
- name : "books" ,
53
- type : "books" ,
54
- [ "temp-id" ] : "abc1" ,
55
- relationship : {
56
- name : "genre" ,
57
- type : "genres" ,
58
- id : "1" ,
59
- attribute : "name" ,
60
- message : "cannot be blank"
61
- }
62
- }
63
- }
64
- } ,
65
- {
66
- code : "unprocessable_entity" ,
67
- status : "422" ,
68
- title : "Validation Error" ,
69
- detail : "base some error" ,
70
- meta : {
71
- relationship : {
72
- name : "books" ,
73
- type : "books" ,
74
- [ "temp-id" ] : "abc1" ,
75
- relationship : {
76
- name : "genre" ,
77
- type : "genres" ,
78
- id : "1" ,
79
- attribute : "base" ,
80
- message : "some error"
81
- }
82
- }
83
- }
84
- }
85
- ]
93
+ errors
86
94
}
87
95
}
88
96
} )
@@ -120,18 +128,20 @@ describe("validations", () => {
120
128
expect ( isSuccess ) . to . eq ( false )
121
129
expect ( instance . errors ) . to . deep . equal ( {
122
130
firstName : {
123
- attribute : "firstName " ,
131
+ attribute : "first_name " ,
124
132
code : "unprocessable_entity" ,
125
133
fullMessage : "First Name cannot be blank" ,
126
134
message : "cannot be blank" ,
127
- title : "Validation Error"
135
+ title : "Validation Error" ,
136
+ rawPayload : mockErrors . firstName ,
128
137
} ,
129
138
lastName : {
130
- attribute : "lastName " ,
139
+ attribute : "last-name " ,
131
140
code : "unprocessable_entity" ,
132
141
fullMessage : "Last Name cannot be blank" ,
133
142
message : "cannot be blank" ,
134
- title : "Validation Error"
143
+ title : "Validation Error" ,
144
+ rawPayload : mockErrors . lastName ,
135
145
}
136
146
} )
137
147
} )
@@ -154,14 +164,16 @@ describe("validations", () => {
154
164
code : "unprocessable_entity" ,
155
165
fullMessage : "First Name cannot be blank" ,
156
166
message : "cannot be blank" ,
157
- title : "Validation Error"
167
+ title : "Validation Error" ,
168
+ rawPayload : mockErrors . firstName ,
158
169
} ,
159
170
last_name : {
160
- attribute : "last_name " ,
171
+ attribute : "last-name " ,
161
172
code : "unprocessable_entity" ,
162
173
fullMessage : "Last Name cannot be blank" ,
163
174
message : "cannot be blank" ,
164
- title : "Validation Error"
175
+ title : "Validation Error" ,
176
+ rawPayload : mockErrors . lastName ,
165
177
}
166
178
} )
167
179
} )
@@ -199,7 +211,8 @@ describe("validations", () => {
199
211
attribute : "nilly" ,
200
212
code : "required" ,
201
213
message : "is required" ,
202
- fullMessage : "Nilly is required"
214
+ fullMessage : "Nilly is required" ,
215
+ rawPayload : { foo : 'bar' }
203
216
}
204
217
}
205
218
@@ -233,7 +246,8 @@ describe("validations", () => {
233
246
attribute : "title" ,
234
247
code : "unprocessable_entity" ,
235
248
message : "cannot be blank" ,
236
- fullMessage : "Title cannot be blank"
249
+ fullMessage : "Title cannot be blank" ,
250
+ rawPayload : mockErrors . bookTitle ,
237
251
}
238
252
} )
239
253
} )
@@ -251,14 +265,16 @@ describe("validations", () => {
251
265
attribute : "name" ,
252
266
code : "unprocessable_entity" ,
253
267
fullMessage : "Name cannot be blank" ,
254
- message : "cannot be blank"
268
+ message : "cannot be blank" ,
269
+ rawPayload : mockErrors . bookGenreName ,
255
270
} ,
256
271
base : {
257
272
title : "Validation Error" ,
258
273
attribute : "base" ,
259
274
code : "unprocessable_entity" ,
260
275
fullMessage : "some error" ,
261
- message : "some error"
276
+ message : "some error" ,
277
+ rawPayload : mockErrors . bookGenreBase ,
262
278
}
263
279
} )
264
280
} )
0 commit comments