render.spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import Vue from 'vue'
  2. describe('Render', () => {
  3. it('render with basic usage', () => {
  4. const vm = new Vue({
  5. template: `<div><render :method="onRender" :args="'hello'"></render></div>`,
  6. methods: {
  7. onRender (args) { return args }
  8. }
  9. }).$mount()
  10. expect(vm.$el.tagName).toBe('DIV')
  11. expect(vm.$el.innerHTML).toBe('hello')
  12. })
  13. it('should render with $createElement', () => {
  14. const vm = new Vue({
  15. template: `<div><render :method="onRender" :args="message"></render></div>`,
  16. data: { message: 'hello world' },
  17. methods: {
  18. onRender (args) {
  19. const h = this.$createElement
  20. return h('div', { class: 'message' }, [
  21. h('p', {}, [args])
  22. ])
  23. }
  24. }
  25. }).$mount()
  26. expect(vm.$el.childNodes[0].tagName).toBe('DIV')
  27. expect(vm.$el.childNodes[0].classList.contains('message')).toBe(true)
  28. expect(vm.$el.childNodes[0].childNodes[0].tagName).toBe('P')
  29. expect(vm.$el.childNodes[0].childNodes[0].textContent).toBe('hello world')
  30. })
  31. it('should render with inline elements', () => {
  32. const vm = new Vue({
  33. template: `
  34. <render :method="onRender" :args="message">
  35. <ul>
  36. <li v-for="n in 5"></li>
  37. </ul>
  38. </render>
  39. `,
  40. data: { message: 'hello world' },
  41. methods: {
  42. onRender (args, children) {
  43. const ul = children[0]
  44. ul.children.forEach((li, i) => {
  45. li.data = { staticClass: `class${i}` }
  46. })
  47. return ul
  48. }
  49. }
  50. }).$mount()
  51. const ul = vm.$el
  52. expect(ul.tagName).toBe('UL')
  53. for (let i = 0; i < ul.children.length; i++) {
  54. const li = ul.children[i]
  55. expect(li.tagName).toBe('LI')
  56. expect(li.classList.contains(`class${i}`)).toBe(true)
  57. }
  58. })
  59. it('should render component', done => {
  60. const modal = {
  61. template: `
  62. <div class="modal-container">
  63. <div class="modal-header">
  64. <h1 class="modal-title">{{title}}</h1>
  65. </div>
  66. <div class="modal-body">
  67. <slot name="body">default body</slot>
  68. </div>
  69. <div class="modal-footer">
  70. <button class="modal-action-close" @click="$emit('close')">close</div>
  71. </div>
  72. </div>
  73. `,
  74. props: {
  75. title: {
  76. type: String, default: 'title1'
  77. }
  78. }
  79. }
  80. const vm = new Vue({
  81. template: `<div><render :method="onRenderModal" :args="title"></render></div>`,
  82. data: {
  83. shown: true,
  84. title: 'hello modal'
  85. },
  86. components: { modal },
  87. methods: {
  88. onRenderModal (title) {
  89. return this.$createElement('modal', {
  90. props: { title: title },
  91. on: { close: this.onCloseModal },
  92. directives: [{ name: 'show', value: this.shown }]
  93. })
  94. },
  95. onCloseModal () { this.shown = false }
  96. }
  97. }).$mount()
  98. expect(vm.$el.querySelector('.modal-title').textContent).toBe(vm.title)
  99. vm.$el.querySelector('.modal-action-close').click()
  100. waitForUpdate(() => {
  101. expect(vm.shown).toBe(false)
  102. }).then(() => {
  103. expect(vm.$el.querySelector('.modal-container').style.display).toBe('none')
  104. }).then(done)
  105. })
  106. it('should warn no method', () => {
  107. new Vue({
  108. template: '<render></render>'
  109. }).$mount()
  110. expect('method attribute is required on <render>').toHaveBeenWarned()
  111. })
  112. it('should warn method/arg usage without v-bind', () => {
  113. new Vue({
  114. template: '<render method="a"></render>'
  115. }).$mount()
  116. expect('<render> method should use a dynamic binding').toHaveBeenWarned()
  117. })
  118. it('should warn non dynamic args', () => {
  119. new Vue({
  120. template: '<render :method="a" args="b"></render>',
  121. methods: {
  122. a: () => {}
  123. }
  124. }).$mount()
  125. expect('<render> args should use a dynamic binding').toHaveBeenWarned()
  126. })
  127. })