events.spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { getRoot, fireEvent, compileAndStringify, compileAndExecute } from '../helpers/index'
  2. describe('generate events', () => {
  3. it('should be bound and fired for native component', (done) => {
  4. compileAndExecute(`
  5. <div @click="foo">
  6. <text>Hello {{x}}</text>
  7. </div>
  8. `, `
  9. data: { x: 'World' },
  10. methods: {
  11. foo: function () {
  12. this.x = 'Weex'
  13. }
  14. }
  15. `).then(instance => {
  16. expect(getRoot(instance)).toEqual({
  17. type: 'div',
  18. event: ['click'],
  19. children: [{
  20. type: 'text',
  21. attr: { value: 'Hello World' }
  22. }]
  23. })
  24. fireEvent(instance, '_root', 'click')
  25. return instance
  26. }).then(instance => {
  27. expect(getRoot(instance)).toEqual({
  28. type: 'div',
  29. event: ['click'],
  30. children: [{
  31. type: 'text',
  32. attr: { value: 'Hello Weex' }
  33. }]
  34. })
  35. done()
  36. })
  37. })
  38. it('should be bound and fired by custom component', (done) => {
  39. const { render, staticRenderFns } = compileAndStringify(`<text>Hello {{x}}</text>`)
  40. compileAndExecute(`
  41. <div>
  42. <text>Hello {{x}}</text>
  43. <sub @click="foo" @click.native="bar"></sub>
  44. </div>
  45. `, `
  46. data: { x: 'World' },
  47. components: {
  48. sub: {
  49. data: function () {
  50. return { x: 'Sub' }
  51. },
  52. render: ${render},
  53. staticRenderFns: ${staticRenderFns},
  54. created: function () {
  55. this.$emit('click')
  56. }
  57. }
  58. },
  59. methods: {
  60. foo: function () {
  61. this.x = 'Foo'
  62. },
  63. bar: function () {
  64. this.x = 'Bar'
  65. }
  66. }
  67. `).then(instance => {
  68. expect(getRoot(instance)).toEqual({
  69. type: 'div',
  70. children: [{
  71. type: 'text',
  72. attr: { value: 'Hello Foo' }
  73. }, {
  74. type: 'text',
  75. event: ['click'],
  76. attr: { value: 'Hello Sub' }
  77. }]
  78. })
  79. fireEvent(instance, instance.document.body.children[1].ref, 'click')
  80. return instance
  81. }).then(instance => {
  82. expect(getRoot(instance)).toEqual({
  83. type: 'div',
  84. children: [{
  85. type: 'text',
  86. attr: { value: 'Hello Bar' }
  87. }, {
  88. type: 'text',
  89. event: ['click'],
  90. attr: { value: 'Hello Sub' }
  91. }]
  92. })
  93. done()
  94. })
  95. })
  96. })