template.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Vue from 'vue'
  2. describe('Options template', () => {
  3. let el
  4. beforeEach(() => {
  5. el = document.createElement('script')
  6. el.type = 'x-template'
  7. el.id = 'app'
  8. el.innerHTML = '<p>{{message}}</p>'
  9. document.body.appendChild(el)
  10. })
  11. afterEach(() => {
  12. document.body.removeChild(el)
  13. })
  14. it('basic usage', () => {
  15. const vm = new Vue({
  16. template: '<div>{{message}}</div>',
  17. data: { message: 'hello world' }
  18. }).$mount()
  19. expect(vm.$el.tagName).toBe('DIV')
  20. expect(vm.$el.textContent).toBe(vm.message)
  21. })
  22. it('id reference', () => {
  23. const vm = new Vue({
  24. template: '#app',
  25. data: { message: 'hello world' }
  26. }).$mount()
  27. expect(vm.$el.tagName).toBe('P')
  28. expect(vm.$el.textContent).toBe(vm.message)
  29. })
  30. it('DOM element', () => {
  31. const elm = document.createElement('p')
  32. elm.innerHTML = '<p>{{message}}</p>'
  33. const vm = new Vue({
  34. template: elm,
  35. data: { message: 'hello world' }
  36. }).$mount()
  37. expect(vm.$el.tagName).toBe('P')
  38. expect(vm.$el.textContent).toBe(vm.message)
  39. })
  40. it('invalid template', () => {
  41. new Vue({
  42. template: Vue,
  43. data: { message: 'hello world' }
  44. }).$mount()
  45. expect('invalid template option').toHaveBeenWarned()
  46. })
  47. it('warn error in generated function', () => {
  48. new Vue({
  49. template:
  50. '<div v-if="!@"><span>{{ a"" }}</span><span>{{ do + 1 }}</span></div>'
  51. }).$mount()
  52. expect('Error compiling template').toHaveBeenWarned()
  53. expect('Raw expression: v-if="!@"').toHaveBeenWarned()
  54. expect('Raw expression: {{ a"" }}').toHaveBeenWarned()
  55. expect(
  56. 'avoid using JavaScript keyword as property name: "do"'
  57. ).toHaveBeenWarned()
  58. })
  59. it('should not warn $ prefixed keywords', () => {
  60. new Vue({
  61. template: `<div @click="$delete(foo, 'bar')"></div>`
  62. }).$mount()
  63. expect(
  64. 'avoid using JavaScript keyword as property name'
  65. ).not.toHaveBeenWarned()
  66. })
  67. it('warn error in generated function (v-for)', () => {
  68. new Vue({
  69. template: '<div><div v-for="(1, 2) in a----"></div></div>'
  70. }).$mount()
  71. expect('Error compiling template').toHaveBeenWarned()
  72. expect('invalid v-for alias "1"').toHaveBeenWarned()
  73. expect('invalid v-for iterator "2"').toHaveBeenWarned()
  74. expect('Raw expression: v-for="(1, 2) in a----"').toHaveBeenWarned()
  75. })
  76. it('warn error in generated function (v-on)', () => {
  77. new Vue({
  78. template: `<div @click="delete('Delete')"></div>`,
  79. methods: { delete: function () {} }
  80. }).$mount()
  81. expect('Error compiling template').toHaveBeenWarned()
  82. expect(
  83. `avoid using JavaScript unary operator as property name: "delete()" in expression @click="delete('Delete')"`
  84. ).toHaveBeenWarned()
  85. })
  86. })