@@ -3,84 +3,88 @@ import { ValidationError, Filter, BaseRecord } from 'admin-bro'
3
3
4
4
import chai , { expect } from 'chai'
5
5
import sinonChai from 'sinon-chai'
6
+ import sinon from 'sinon'
6
7
7
- import Resource from './resource'
8
+ import Resource , { ModelType } from './resource'
8
9
import Property from './property'
9
10
10
11
chai . use ( sinonChai )
11
12
12
13
const config = require ( '../config/config' ) [ process . env . NODE_ENV as string ]
13
14
const db = require ( '../models/index.js' )
14
15
15
- describe ( 'Resource' , ( ) => {
16
+ describe ( 'Resource' , function ( ) {
17
+ let SequelizeModel : ModelType < any >
18
+ let resource : Resource
19
+
16
20
before ( function ( ) {
17
- this . SequelizeModel = db . sequelize . models . User
18
- this . resource = new Resource ( this . SequelizeModel )
21
+ SequelizeModel = db . sequelize . models . User
22
+ resource = new Resource ( SequelizeModel )
19
23
} )
20
24
21
25
afterEach ( async function ( ) {
22
- await this . SequelizeModel . destroy ( { where : { } } )
26
+ await SequelizeModel . destroy ( { where : { } } )
23
27
} )
24
28
25
29
describe ( '.isAdapterFor' , ( ) => {
26
30
it ( 'returns true when sequelize model is given' , function ( ) {
27
- expect ( Resource . isAdapterFor ( this . SequelizeModel ) ) . to . equal ( true )
31
+ expect ( Resource . isAdapterFor ( SequelizeModel ) ) . to . equal ( true )
28
32
} )
29
33
} )
30
34
31
35
describe ( '#database' , ( ) => {
32
36
it ( 'returns correct database name' , function ( ) {
33
- expect ( this . resource . databaseName ( ) ) . to . equal ( config . database )
37
+ expect ( resource . databaseName ( ) ) . to . equal ( config . database )
34
38
} )
35
39
} )
36
40
37
41
describe ( '#databaseType' , ( ) => {
38
42
it ( 'returns correct database' , function ( ) {
39
- expect ( this . resource . databaseType ( ) ) . to . equal ( config . dialect )
43
+ expect ( resource . databaseType ( ) ) . to . equal ( config . dialect )
40
44
} )
41
45
} )
42
46
43
47
describe ( '#name' , ( ) => {
44
48
it ( 'returns correct name' , function ( ) {
45
- expect ( this . resource . name ( ) ) . to . equal ( 'Users' )
49
+ expect ( resource . name ( ) ) . to . equal ( 'Users' )
46
50
} )
47
51
} )
48
52
49
53
describe ( '#id' , ( ) => {
50
54
it ( 'returns correct name' , function ( ) {
51
- expect ( this . resource . id ( ) ) . to . equal ( 'Users' )
55
+ expect ( resource . id ( ) ) . to . equal ( 'Users' )
52
56
} )
53
57
} )
54
58
55
59
describe ( '#properties' , ( ) => {
56
60
it ( 'returns all properties' , function ( ) {
57
61
const length = 8 // there are 8 properties in the User model (5 regular + __v and _id)
58
- expect ( this . resource . properties ( ) ) . to . have . lengthOf ( length )
62
+ expect ( resource . properties ( ) ) . to . have . lengthOf ( length )
59
63
} )
60
64
} )
61
65
62
66
describe ( '#property' , ( ) => {
63
67
it ( 'returns given property' , function ( ) {
64
- expect ( this . resource . property ( 'email' ) ) . to . be . an . instanceOf ( Property )
68
+ expect ( resource . property ( 'email' ) ) . to . be . an . instanceOf ( Property )
65
69
} )
66
70
67
71
it ( 'returns null when property doesn\'t exit' , function ( ) {
68
- expect ( this . resource . property ( 'some.imagine.property' ) ) . to . be . null
72
+ expect ( resource . property ( 'some.imagine.property' ) ) . to . be . null
69
73
} )
70
74
71
75
it ( 'returns nested property for array field' , function ( ) {
72
- const property = this . resource . property ( 'arrayed.1' )
76
+ const property = resource . property ( 'arrayed.1' )
73
77
74
78
expect ( property ) . to . be . an . instanceOf ( Property )
75
- expect ( property . type ( ) ) . to . equal ( 'string' )
79
+ expect ( property ? .type ( ) ) . to . equal ( 'string' )
76
80
} )
77
81
} )
78
82
79
83
describe ( '#findMany' , ( ) => {
80
84
it ( 'returns array of BaseRecords' , async function ( ) {
81
- const params = await this . resource . create ( { email :
'[email protected] ' } )
85
+ const params = await resource . create ( { email :
'[email protected] ' } )
82
86
83
- const records = await this . resource . findMany ( [ params . id ] )
87
+ const records = await resource . findMany ( [ params . id ] )
84
88
85
89
expect ( records ) . to . have . lengthOf ( 1 )
86
90
expect ( records [ 0 ] ) . to . be . instanceOf ( BaseRecord )
@@ -93,14 +97,14 @@ describe('Resource', () => {
93
97
gender : 'male' ,
94
98
95
99
}
96
- this . record = await this . resource . create ( this . params )
100
+ this . record = await resource . create ( this . params )
97
101
} )
98
102
99
103
it ( 'returns 1 BaseRecord when filtering on ENUMS' , async function ( ) {
100
104
const filter = new Filter ( {
101
105
gender : 'male' ,
102
- } , this . resource )
103
- const records = await this . resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
106
+ } , resource )
107
+ const records = await resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
104
108
105
109
expect ( records ) . to . have . lengthOf ( 1 )
106
110
expect ( records [ 0 ] ) . to . be . instanceOf ( BaseRecord )
@@ -110,18 +114,18 @@ describe('Resource', () => {
110
114
it ( 'returns 0 BaseRecord when filtering on ENUMS' , async function ( ) {
111
115
const filter = new Filter ( {
112
116
gender : 'female' ,
113
- } , this . resource )
114
- const records = await this . resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
117
+ } , resource )
118
+ const records = await resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
115
119
116
120
expect ( records ) . to . have . lengthOf ( 0 )
117
121
} )
118
122
119
123
it ( 'returns error when filtering on ENUMS with invalid value' , async function ( ) {
120
124
const filter = new Filter ( {
121
125
gender : 'XXX' ,
122
- } , this . resource )
126
+ } , resource )
123
127
try {
124
- await this . resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
128
+ await resource . find ( filter , { limit : 20 , offset : 0 , sort : { direction : 'asc' , sortBy : 'id' } } )
125
129
} catch ( error ) {
126
130
expect ( error . name ) . to . eq ( 'SequelizeDatabaseError' )
127
131
}
@@ -130,30 +134,30 @@ describe('Resource', () => {
130
134
131
135
describe ( '#count' , ( ) => {
132
136
it ( 'returns 0 when there are none elements' , async function ( ) {
133
- const count = await this . resource . count ( new Filter ( { } as any , { } as any ) )
137
+ const count = await resource . count ( new Filter ( { } as any , { } as any ) )
134
138
expect ( count ) . to . equal ( 0 )
135
139
} )
136
140
137
141
it ( 'returns given count without filters' , async function ( ) {
138
- await this . resource . create ( { email :
'[email protected] ' } )
139
- expect ( await this . resource . count ( new Filter ( { } as any , { } as any ) ) ) . to . equal ( 1 )
142
+ await resource . create ( { email :
'[email protected] ' } )
143
+ expect ( await resource . count ( new Filter ( { } as any , { } as any ) ) ) . to . equal ( 1 )
140
144
} )
141
145
142
146
it ( 'returns given count for given filters' , async function ( ) {
143
- await this . resource . create ( {
147
+ await resource . create ( {
144
148
firstName : 'john' ,
145
149
lastName : 'doe' ,
146
150
147
151
} )
148
- await this . resource . create ( {
152
+ await resource . create ( {
149
153
firstName : 'andrew' ,
150
154
lastName : 'golota' ,
151
155
152
156
} )
153
157
const filter = new Filter ( {
154
158
155
- } , this . resource )
156
- expect ( await this . resource . count ( filter ) ) . to . equal ( 1 )
159
+ } , resource )
160
+ expect ( await resource . count ( filter ) ) . to . equal ( 1 )
157
161
} )
158
162
} )
159
163
@@ -165,11 +169,11 @@ describe('Resource', () => {
165
169
lastName : 'doe' ,
166
170
167
171
}
168
- this . record = await this . resource . create ( this . params )
172
+ this . record = await resource . create ( this . params )
169
173
} )
170
174
171
175
it ( 'creates new user when data is valid' , async function ( ) {
172
- const newCount = await this . resource . count ( )
176
+ const newCount = await resource . count ( null as any )
173
177
expect ( newCount ) . to . equal ( 1 )
174
178
} )
175
179
@@ -189,7 +193,7 @@ describe('Resource', () => {
189
193
190
194
it ( 'throws validation error' , async function ( ) {
191
195
try {
192
- await this . resource . create ( this . params )
196
+ await resource . create ( this . params )
193
197
} catch ( error ) {
194
198
expect ( error ) . to . be . an . instanceOf ( ValidationError )
195
199
}
@@ -198,8 +202,8 @@ describe('Resource', () => {
198
202
199
203
context ( 'record with empty id field' , ( ) => {
200
204
beforeEach ( function ( ) {
201
- this . SequelizeModel = db . sequelize . models . Post
202
- this . resource = new Resource ( this . SequelizeModel )
205
+ SequelizeModel = db . sequelize . models . Post
206
+ resource = new Resource ( SequelizeModel )
203
207
} )
204
208
205
209
it ( 'creates record without an error' , async function ( ) {
@@ -209,28 +213,82 @@ describe('Resource', () => {
209
213
publishedAt : '2019-12-10 12:00' ,
210
214
userId : '' ,
211
215
}
212
- this . recordParams = await this . resource . create ( this . params )
216
+ this . recordParams = await resource . create ( this . params )
213
217
expect ( this . recordParams . userId ) . to . be . null
214
218
} )
215
219
} )
216
220
} )
217
221
218
222
describe ( '#update' , ( ) => {
219
223
beforeEach ( async function ( ) {
224
+ SequelizeModel = db . sequelize . models . User
225
+ resource = new Resource ( SequelizeModel )
220
226
this . params = {
221
227
firstName : 'john' ,
222
228
lastName : 'doe' ,
223
229
224
230
}
225
- this . record = await this . resource . create ( this . params )
231
+ this . record = await resource . create ( this . params )
226
232
} )
227
233
228
234
it ( 'updates the title' , async function ( ) {
229
235
this . newEmail = '[email protected] '
230
- this . record = await this . resource . update ( this . record . id , {
236
+ const params = await resource . update ( this . record . id , {
231
237
email : this . newEmail ,
232
238
} )
233
- expect ( this . record . email ) . to . equal ( this . newEmail )
239
+
240
+ expect ( params . email ) . to . equal ( this . newEmail )
241
+ } )
242
+
243
+ it ( 'calls update hooks' , async function ( ) {
244
+ const beforeUpdateSpy = sinon . spy ( )
245
+ const afterUpdateSpy = sinon . spy ( )
246
+ const beforeBulkUpdateSpy = sinon . spy ( )
247
+ SequelizeModel . addHook ( 'beforeUpdate' , beforeUpdateSpy )
248
+ SequelizeModel . addHook ( 'beforeBulkUpdate' , beforeBulkUpdateSpy )
249
+ SequelizeModel . addHook ( 'afterUpdate' , afterUpdateSpy )
250
+
251
+ await resource . update ( this . record . id , { firstName : 'jack' } )
252
+
253
+ expect ( beforeUpdateSpy ) . to . have . been . called
254
+ expect ( afterUpdateSpy ) . to . have . been . called
255
+ expect ( beforeBulkUpdateSpy ) . not . to . have . been . called
256
+ } )
257
+ } )
258
+
259
+ describe ( '#delete' , ( ) => {
260
+ beforeEach ( async function ( ) {
261
+ SequelizeModel = db . sequelize . models . User
262
+ resource = new Resource ( SequelizeModel )
263
+ this . params = {
264
+ firstName : 'john' ,
265
+ lastName : 'doe' ,
266
+
267
+ }
268
+ this . record = await resource . create ( this . params )
269
+ } )
270
+
271
+ it ( 'deletes the resource' , async function ( ) {
272
+ await resource . delete ( this . record . id )
273
+
274
+ const newRecord = await resource . findOne ( this . record . id )
275
+
276
+ expect ( newRecord ) . to . be . null
277
+ } )
278
+
279
+ it ( 'calls delete hooks' , async function ( ) {
280
+ const beforeDestroySpy = sinon . spy ( )
281
+ const afterDestroySpy = sinon . spy ( )
282
+ const beforeDestroyUpdateSpy = sinon . spy ( )
283
+ SequelizeModel . addHook ( 'beforeDestroy' , beforeDestroySpy )
284
+ SequelizeModel . addHook ( 'beforeBulkDestroy' , beforeDestroyUpdateSpy )
285
+ SequelizeModel . addHook ( 'afterDestroy' , afterDestroySpy )
286
+
287
+ await resource . delete ( this . record . id )
288
+
289
+ expect ( beforeDestroySpy ) . to . have . been . called
290
+ expect ( afterDestroySpy ) . to . have . been . called
291
+ expect ( beforeDestroyUpdateSpy ) . not . to . have . been . called
234
292
} )
235
293
} )
236
294
} )
0 commit comments