|
6 | 6 | import { assert } from 'chai'
|
7 | 7 | import Vue from 'vue'
|
8 | 8 | import Vuex from 'vuex'
|
| 9 | +import { ServiceState } from './types' |
9 | 10 | import { clearModels } from '../../src/service-module/global-models'
|
10 | 11 | import { clients } from '../../src/service-module/global-clients'
|
11 | 12 | import { feathersRestClient as feathers } from '../../test/fixtures/feathers-client'
|
@@ -337,4 +338,101 @@ describe('makeServicePlugin', function() {
|
337 | 338 | await todo.remove()
|
338 | 339 | assert(globalRemovedCalled, 'global removed handler called')
|
339 | 340 | })
|
| 341 | + |
| 342 | + it('allow handleEvents handlers to return extracted event data', async function() { |
| 343 | + const serverAlias = 'default' |
| 344 | + |
| 345 | + const { makeServicePlugin, BaseModel } = feathersVuex(feathers, { |
| 346 | + idField: '_id', |
| 347 | + serverAlias, |
| 348 | + handleEvents: { |
| 349 | + created(e) { |
| 350 | + return [true, e.myCreatedPropWithActualData] |
| 351 | + }, |
| 352 | + updated(e) { |
| 353 | + return [true, e.myUpdatedPropWithActualData] |
| 354 | + }, |
| 355 | + patched(e) { |
| 356 | + return [true, e.myPatchedPropWithActualData] |
| 357 | + }, |
| 358 | + removed(e) { |
| 359 | + return [true, e.myRemovedPropWithActualData] |
| 360 | + } |
| 361 | + } |
| 362 | + }) |
| 363 | + |
| 364 | + const servicePath = 'todos' |
| 365 | + class Todo extends BaseModel { |
| 366 | + public static modelName = 'Todo' |
| 367 | + public static servicePath = servicePath |
| 368 | + } |
| 369 | + |
| 370 | + const todosService = feathers.service(servicePath) |
| 371 | + const todosPlugin = makeServicePlugin({ |
| 372 | + servicePath, |
| 373 | + Model: Todo, |
| 374 | + service: todosService |
| 375 | + }) |
| 376 | + |
| 377 | + const store = new Vuex.Store<{ todos: ServiceState }>({ |
| 378 | + plugins: [todosPlugin] |
| 379 | + }) |
| 380 | + const { keyedById } = store.state.todos |
| 381 | + |
| 382 | + let createdData = null |
| 383 | + let updatedData = null |
| 384 | + let patchedData = null |
| 385 | + let removedData = null |
| 386 | + Todo.on('created', e => (createdData = e)) |
| 387 | + Todo.on('updated', e => (updatedData = e)) |
| 388 | + Todo.on('patched', e => (patchedData = e)) |
| 389 | + Todo.on('removed', e => (removedData = e)) |
| 390 | + |
| 391 | + assert(Object.keys(keyedById).length === 0, 'no todos in store') |
| 392 | + |
| 393 | + todosService.emit('created', { |
| 394 | + context: 'foo', |
| 395 | + myCreatedPropWithActualData: { _id: 42, text: '' } |
| 396 | + }) |
| 397 | + assert(keyedById[42], 'todo added to store') |
| 398 | + assert(keyedById[42].text === '', 'todo string is empty') |
| 399 | + assert(createdData, "Model's created event fired") |
| 400 | + assert( |
| 401 | + createdData.context === 'foo' && createdData.myCreatedPropWithActualData, |
| 402 | + "Model's created event got all event data" |
| 403 | + ) |
| 404 | + |
| 405 | + todosService.emit('updated', { |
| 406 | + context: 'bar', |
| 407 | + myUpdatedPropWithActualData: { _id: 42, text: 'updated' } |
| 408 | + }) |
| 409 | + assert(keyedById[42].text === 'updated', 'todo was updated') |
| 410 | + assert(updatedData, "Model's updated event fired") |
| 411 | + assert( |
| 412 | + updatedData.context === 'bar' && updatedData.myUpdatedPropWithActualData, |
| 413 | + "Model's updated event got all event data" |
| 414 | + ) |
| 415 | + |
| 416 | + todosService.emit('patched', { |
| 417 | + context: 'baz', |
| 418 | + myPatchedPropWithActualData: { _id: 42, text: 'patched' } |
| 419 | + }) |
| 420 | + assert(keyedById[42].text === 'patched', 'todo was patched') |
| 421 | + assert(patchedData, "Model's patched event fired") |
| 422 | + assert( |
| 423 | + patchedData.context === 'baz' && patchedData.myPatchedPropWithActualData, |
| 424 | + "Model's patched event got all event data" |
| 425 | + ) |
| 426 | + |
| 427 | + todosService.emit('removed', { |
| 428 | + context: 'spam', |
| 429 | + myRemovedPropWithActualData: { _id: 42 } |
| 430 | + }) |
| 431 | + assert(Object.keys(keyedById).length === 0, 'todo removed from store') |
| 432 | + assert(removedData, "Model's removed event fired") |
| 433 | + assert( |
| 434 | + removedData.context === 'spam' && removedData.myRemovedPropWithActualData, |
| 435 | + "Model's removed event got all event data" |
| 436 | + ) |
| 437 | + }) |
340 | 438 | })
|
0 commit comments