compile_spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var dirParser = require('../../../../src/parse/directive')
  4. var merge = require('../../../../src/util/merge-option')
  5. var compile = require('../../../../src/compile/compile')
  6. if (_.inBrowser) {
  7. describe('Compile', function () {
  8. var vm, el, data
  9. beforeEach(function () {
  10. // We mock vms here so we can assert what the generated
  11. // linker functions do.
  12. el = document.createElement('div')
  13. data = {}
  14. vm = {
  15. _bindDir: jasmine.createSpy(),
  16. $set: jasmine.createSpy(),
  17. $eval: function (value) {
  18. return data[value]
  19. },
  20. $interpolate: function (value) {
  21. value = value.replace(/\{|\}/g, '')
  22. return data[value]
  23. }
  24. }
  25. spyOn(vm, '$eval').and.callThrough()
  26. spyOn(vm, '$interpolate').and.callThrough()
  27. })
  28. it('normal directives', function () {
  29. var defA = { priority: 1 }
  30. var defB = { priority: 2 }
  31. var descriptorA = dirParser.parse('a')[0]
  32. var descriptorB = dirParser.parse('b')[0]
  33. var options = merge(Vue.options, {
  34. directives: {
  35. a: defA,
  36. b: defB
  37. }
  38. })
  39. el.innerHTML = '<p v-a="a" v-b="b"></p><div v-b="b"></div>'
  40. var linker = compile(el, options)
  41. expect(typeof linker).toBe('function')
  42. // should remove attributes
  43. expect(el.firstChild.attributes.length).toBe(0)
  44. expect(el.lastChild.attributes.length).toBe(0)
  45. linker(vm, el)
  46. expect(vm._bindDir.calls.count()).toBe(3)
  47. expect(vm._bindDir).toHaveBeenCalledWith('a', el.firstChild, descriptorA, defA)
  48. expect(vm._bindDir).toHaveBeenCalledWith('b', el.firstChild, descriptorB, defB)
  49. expect(vm._bindDir).toHaveBeenCalledWith('b', el.lastChild, descriptorB, defB)
  50. // check the priority sorting
  51. // the "b" on the firstNode should be called first!
  52. expect(vm._bindDir.calls.argsFor(0)[0]).toBe('b')
  53. })
  54. it('terminal directives', function () {
  55. // body...
  56. })
  57. it('text interpolation', function () {
  58. // body...
  59. })
  60. it('attribute interpolation', function () {
  61. // body...
  62. })
  63. it('param attributes', function () {
  64. // body...
  65. })
  66. it('DocumentFragment', function () {
  67. // body...
  68. })
  69. it('template elements', function () {
  70. // body...
  71. })
  72. })
  73. }