functional.spec.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 let vnode raw data pass through', done => {
  24. const onValid = jasmine.createSpy('valid')
  25. const vm = new Vue({
  26. data: { msg: 'hello' },
  27. template: `<div>
  28. <validate field="field1" @valid="onValid">
  29. <input type="text" v-model="msg">
  30. </validate>
  31. </div>`,
  32. components: {
  33. validate: {
  34. functional: true,
  35. props: ['field'],
  36. render (h, { props, children, data: { on } }) {
  37. props.child = children[0]
  38. return h('validate-control', { props, on })
  39. }
  40. },
  41. 'validate-control': {
  42. props: ['field', 'child'],
  43. render () {
  44. return this.child
  45. },
  46. mounted () {
  47. this.$el.addEventListener('input', this.onInput)
  48. },
  49. destroyed () {
  50. this.$el.removeEventListener('input', this.onInput)
  51. },
  52. methods: {
  53. onInput (e) {
  54. const value = e.target.value
  55. if (this.validate(value)) {
  56. this.$emit('valid', this)
  57. }
  58. },
  59. // something validation logic here
  60. validate (val) {
  61. return val.length > 0
  62. }
  63. }
  64. }
  65. },
  66. methods: { onValid }
  67. }).$mount()
  68. document.body.appendChild(vm.$el)
  69. const input = vm.$el.querySelector('input')
  70. expect(onValid).not.toHaveBeenCalled()
  71. waitForUpdate(() => {
  72. input.value = 'foo'
  73. triggerEvent(input, 'input')
  74. }).then(() => {
  75. expect(onValid).toHaveBeenCalled()
  76. }).then(() => {
  77. document.body.removeChild(vm.$el)
  78. vm.$destroy()
  79. }).then(done)
  80. })
  81. })