model-component.spec.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Vue from 'vue'
  2. describe('Directive v-model component', () => {
  3. it('should work', done => {
  4. const vm = new Vue({
  5. data: {
  6. msg: 'hello'
  7. },
  8. template: `
  9. <div>
  10. <p>{{ msg }}</p>
  11. <validate v-model="msg">
  12. <input type="text">
  13. </validate>
  14. </div>
  15. `,
  16. components: {
  17. validate: {
  18. template: '<div><slot></slot></div>',
  19. props: ['value'],
  20. methods: {
  21. onInput (e) {
  22. // something validate ...
  23. this.$emit('input', e.target.value)
  24. }
  25. },
  26. mounted () {
  27. this.$el.addEventListener('input', this.onInput)
  28. },
  29. destroyed () {
  30. this.$el.removeEventListener('input', this.onInput)
  31. }
  32. }
  33. }
  34. }).$mount()
  35. document.body.appendChild(vm.$el)
  36. waitForUpdate(() => {
  37. expect('v-model is not supported on element type').not.toHaveBeenWarned()
  38. const input = vm.$el.querySelector('input')
  39. input.value = 'world'
  40. triggerEvent(input, 'input')
  41. }).then(() => {
  42. expect(vm.msg).toBe('world')
  43. }).then(() => {
  44. document.body.removeChild(vm.$el)
  45. vm.$destroy()
  46. }).then(done)
  47. })
  48. })