2
0

functional.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Vue from 'vue'
  2. describe('Options functional', () => {
  3. it('should work', done => {
  4. const vm = new Vue({
  5. data: { test: 'foo' },
  6. template: '<div><wrap :msg="test">bar</wrap></div>',
  7. components: {
  8. wrap: {
  9. functional: true,
  10. props: ['msg'],
  11. render (h, { props, children }) {
  12. return h('div', null, [props.msg, ' '].concat(children()))
  13. }
  14. }
  15. }
  16. }).$mount()
  17. expect(vm.$el.innerHTML).toBe('<div>foo bar</div>')
  18. vm.test = 'qux'
  19. waitForUpdate(() => {
  20. expect(vm.$el.innerHTML).toBe('<div>qux bar</div>')
  21. }).then(done)
  22. })
  23. it('should support slots', () => {
  24. const vm = new Vue({
  25. data: { test: 'foo' },
  26. template: '<div><wrap><div slot="a">foo</div><div slot="b">bar</div></wrap></div>',
  27. components: {
  28. wrap: {
  29. functional: true,
  30. props: ['msg'],
  31. render (h, { slots }) {
  32. slots = slots()
  33. return h('div', null, [slots.b, slots.a])
  34. }
  35. }
  36. }
  37. }).$mount()
  38. expect(vm.$el.innerHTML).toBe('<div><div>bar</div><div>foo</div></div>')
  39. })
  40. it('should let vnode raw data pass through', done => {
  41. const onValid = jasmine.createSpy('valid')
  42. const vm = new Vue({
  43. data: { msg: 'hello' },
  44. template: `<div>
  45. <validate field="field1" @valid="onValid">
  46. <input type="text" v-model="msg">
  47. </validate>
  48. </div>`,
  49. components: {
  50. validate: {
  51. functional: true,
  52. props: ['field'],
  53. render (h, { props, children, data: { on } }) {
  54. props.child = children()[0]
  55. return h('validate-control', { props, on })
  56. }
  57. },
  58. 'validate-control': {
  59. props: ['field', 'child'],
  60. render () {
  61. return this.child
  62. },
  63. mounted () {
  64. this.$el.addEventListener('input', this.onInput)
  65. },
  66. destroyed () {
  67. this.$el.removeEventListener('input', this.onInput)
  68. },
  69. methods: {
  70. onInput (e) {
  71. const value = e.target.value
  72. if (this.validate(value)) {
  73. this.$emit('valid', this)
  74. }
  75. },
  76. // something validation logic here
  77. validate (val) {
  78. return val.length > 0
  79. }
  80. }
  81. }
  82. },
  83. methods: { onValid }
  84. }).$mount()
  85. document.body.appendChild(vm.$el)
  86. const input = vm.$el.querySelector('input')
  87. expect(onValid).not.toHaveBeenCalled()
  88. waitForUpdate(() => {
  89. input.value = 'foo'
  90. triggerEvent(input, 'input')
  91. }).then(() => {
  92. expect(onValid).toHaveBeenCalled()
  93. }).then(() => {
  94. document.body.removeChild(vm.$el)
  95. vm.$destroy()
  96. }).then(done)
  97. })
  98. })