model-component.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Vue from 'vue'
  2. describe('Directive v-model component', () => {
  3. it('should work', done => {
  4. const spy = jasmine.createSpy()
  5. const vm = new Vue({
  6. data: {
  7. msg: ['hello']
  8. },
  9. watch: {
  10. msg: spy
  11. },
  12. template: `
  13. <div>
  14. <p>{{ msg }}</p>
  15. <validate v-model="msg[0]">
  16. <input type="text">
  17. </validate>
  18. </div>
  19. `,
  20. components: {
  21. validate: {
  22. template: '<div><slot></slot></div>',
  23. props: ['value'],
  24. methods: {
  25. onInput (e) {
  26. // something validate ...
  27. this.$emit('input', e.target.value)
  28. }
  29. },
  30. mounted () {
  31. this.$el.addEventListener('input', this.onInput)
  32. },
  33. destroyed () {
  34. this.$el.removeEventListener('input', this.onInput)
  35. }
  36. }
  37. }
  38. }).$mount()
  39. document.body.appendChild(vm.$el)
  40. waitForUpdate(() => {
  41. expect('v-model is not supported on element type').not.toHaveBeenWarned()
  42. const input = vm.$el.querySelector('input')
  43. input.value = 'world'
  44. triggerEvent(input, 'input')
  45. }).then(() => {
  46. expect(vm.msg).toEqual(['world'])
  47. expect(spy).toHaveBeenCalled()
  48. }).then(() => {
  49. document.body.removeChild(vm.$el)
  50. vm.$destroy()
  51. }).then(done)
  52. })
  53. it('modifier: .lazy', () => {
  54. const vm = new Vue({
  55. template: `<div><my-input ref="input" v-model.lazy="text"></my-input></div>`,
  56. data: { text: 'foo' },
  57. components: {
  58. 'my-input': {
  59. template: '<input>'
  60. }
  61. }
  62. }).$mount()
  63. expect(vm.text).toBe('foo')
  64. vm.$refs.input.$emit('input', 'bar')
  65. expect(vm.text).toBe('foo')
  66. vm.$refs.input.$emit('change', 'bar')
  67. expect(vm.text).toBe('bar')
  68. })
  69. it('modifier: .number', () => {
  70. const vm = new Vue({
  71. template: `<div><my-input ref="input" v-model.number="text"></my-input></div>`,
  72. data: { text: 'foo' },
  73. components: {
  74. 'my-input': {
  75. template: '<input>'
  76. }
  77. }
  78. }).$mount()
  79. expect(vm.text).toBe('foo')
  80. vm.$refs.input.$emit('input', 'bar')
  81. expect(vm.text).toBe('bar')
  82. vm.$refs.input.$emit('input', '123')
  83. expect(vm.text).toBe(123)
  84. })
  85. it('modifier: .trim', () => {
  86. const vm = new Vue({
  87. template: `<div><my-input ref="input" v-model.trim="text"></my-input></div>`,
  88. data: { text: 'foo' },
  89. components: {
  90. 'my-input': {
  91. template: '<input>'
  92. }
  93. }
  94. }).$mount()
  95. expect(vm.text).toBe('foo')
  96. vm.$refs.input.$emit('input', ' bar ')
  97. expect(vm.text).toBe('bar')
  98. vm.$refs.input.$emit('input', ' foo o ')
  99. expect(vm.text).toBe('foo o')
  100. })
  101. })