@@ -138,4 +138,203 @@ describe('makeServicePlugin', function() {
138
138
assert ( models [ serverAlias ] [ Todo . name ] === Todo )
139
139
assert ( Todo . store === store )
140
140
} )
141
+
142
+ it ( 'allows service specific handleEvents' , async function ( ) {
143
+ // feathers.use('todos', new TodosService())
144
+ const serverAlias = 'default'
145
+ const { makeServicePlugin, BaseModel } = feathersVuex ( feathers , {
146
+ idField : '_id' ,
147
+ serverAlias
148
+ } )
149
+
150
+ const servicePath = 'todos'
151
+ class Todo extends BaseModel {
152
+ public static modelName = 'Todo'
153
+ public static servicePath = servicePath
154
+ }
155
+
156
+ let createdCalled = false
157
+ let updatedCalled = false
158
+ let patchedCalled = false
159
+ let removedCalled = false
160
+ const todosPlugin = makeServicePlugin ( {
161
+ servicePath,
162
+ Model : Todo ,
163
+ service : feathers . service ( servicePath ) ,
164
+ handleEvents : {
165
+ created ( ) {
166
+ createdCalled = true
167
+ return true
168
+ } ,
169
+ updated ( ) {
170
+ updatedCalled = true
171
+ return true
172
+ } ,
173
+ patched ( ) {
174
+ patchedCalled = true
175
+ return true
176
+ } ,
177
+ removed ( ) {
178
+ removedCalled = true
179
+ return true
180
+ }
181
+ }
182
+ } )
183
+
184
+ const store = new Vuex . Store ( {
185
+ plugins : [ todosPlugin ]
186
+ } )
187
+
188
+ const todo = new Todo ( )
189
+
190
+ // Fake server call
191
+ feathers . service ( 'todos' ) . hooks ( {
192
+ before : {
193
+ create : [
194
+ context => {
195
+ delete context . data . __id
196
+ delete context . data . __isTemp
197
+ } ,
198
+ context => {
199
+ context . result = { _id : 24 , ...context . data }
200
+ return context
201
+ }
202
+ ] ,
203
+ update : [
204
+ context => {
205
+ context . result = { ...context . data }
206
+ return context
207
+ }
208
+ ] ,
209
+ patch : [
210
+ context => {
211
+ context . result = { ...context . data }
212
+ return context
213
+ }
214
+ ] ,
215
+ remove : [
216
+ context => {
217
+ context . result = { ...todo }
218
+ return context
219
+ }
220
+ ]
221
+ }
222
+ } )
223
+
224
+ await todo . create ( )
225
+ assert ( createdCalled , 'created handler called' )
226
+
227
+ await todo . update ( )
228
+ assert ( updatedCalled , 'updated handler called' )
229
+
230
+ await todo . patch ( )
231
+ assert ( patchedCalled , 'patched handler called' )
232
+
233
+ await todo . remove ( )
234
+ assert ( removedCalled , 'removed handler called' )
235
+ } )
236
+
237
+ it ( 'fall back to globalOptions handleEvents if service specific handleEvents handler is missing' , async function ( ) {
238
+ // feathers.use('todos', new TodosService())
239
+ const serverAlias = 'default'
240
+
241
+ let globalCreatedCalled = false
242
+ let globalUpdatedCalled = false
243
+ let globalPatchedCalled = false
244
+ let globalRemovedCalled = false
245
+ const { makeServicePlugin, BaseModel } = feathersVuex ( feathers , {
246
+ idField : '_id' ,
247
+ serverAlias,
248
+ handleEvents : {
249
+ created ( ) {
250
+ globalCreatedCalled = true
251
+ return true
252
+ } ,
253
+ updated ( ) {
254
+ globalUpdatedCalled = true
255
+ return true
256
+ } ,
257
+ patched ( ) {
258
+ globalPatchedCalled = true
259
+ return true
260
+ } ,
261
+ removed ( ) {
262
+ globalRemovedCalled = true
263
+ return true
264
+ }
265
+ }
266
+ } )
267
+
268
+ const servicePath = 'todos'
269
+ class Todo extends BaseModel {
270
+ public static modelName = 'Todo'
271
+ public static servicePath = servicePath
272
+ }
273
+
274
+ let specificUpdatedCalled = false
275
+ const todosPlugin = makeServicePlugin ( {
276
+ servicePath,
277
+ Model : Todo ,
278
+ service : feathers . service ( servicePath ) ,
279
+ handleEvents : {
280
+ updated ( ) {
281
+ specificUpdatedCalled = true
282
+ return true
283
+ }
284
+ }
285
+ } )
286
+
287
+ const store = new Vuex . Store ( {
288
+ plugins : [ todosPlugin ]
289
+ } )
290
+
291
+ const todo = new Todo ( )
292
+
293
+ // Fake server call
294
+ feathers . service ( 'todos' ) . hooks ( {
295
+ before : {
296
+ create : [
297
+ context => {
298
+ delete context . data . __id
299
+ delete context . data . __isTemp
300
+ } ,
301
+ context => {
302
+ context . result = { _id : 24 , ...context . data }
303
+ return context
304
+ }
305
+ ] ,
306
+ update : [
307
+ context => {
308
+ context . result = { ...context . data }
309
+ return context
310
+ }
311
+ ] ,
312
+ patch : [
313
+ context => {
314
+ context . result = { ...context . data }
315
+ return context
316
+ }
317
+ ] ,
318
+ remove : [
319
+ context => {
320
+ context . result = { ...todo }
321
+ return context
322
+ }
323
+ ]
324
+ }
325
+ } )
326
+
327
+ await todo . create ( )
328
+ assert ( globalCreatedCalled , 'global created handler called' )
329
+
330
+ await todo . update ( )
331
+ assert ( specificUpdatedCalled , 'specific updated handler called' )
332
+ assert ( ! globalUpdatedCalled , 'global updated handler NOT called' )
333
+
334
+ await todo . patch ( )
335
+ assert ( globalPatchedCalled , 'global patched handler called' )
336
+
337
+ await todo . remove ( )
338
+ assert ( globalRemovedCalled , 'global removed handler called' )
339
+ } )
141
340
} )
0 commit comments