cases.spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import {
  2. readFile,
  3. readObject,
  4. compileVue,
  5. compileWithDeps,
  6. createInstance,
  7. addTaskHook,
  8. resetTaskHook,
  9. getRoot,
  10. getEvents,
  11. fireEvent
  12. } from '../helpers'
  13. // Create one-off render test case
  14. function createRenderTestCase (name) {
  15. const source = readFile(`${name}.vue`)
  16. const target = readObject(`${name}.vdom.js`)
  17. return done => {
  18. compileVue(source).then(code => {
  19. const id = String(Date.now() * Math.random())
  20. const instance = createInstance(id, code)
  21. setTimeout(() => {
  22. expect(getRoot(instance)).toEqual(target)
  23. instance.$destroy()
  24. done()
  25. }, 50)
  26. }).catch(done.fail)
  27. }
  28. }
  29. // Create event test case, will trigger the first bind event
  30. function createEventTestCase (name) {
  31. const source = readFile(`${name}.vue`)
  32. const before = readObject(`${name}.before.vdom.js`)
  33. const after = readObject(`${name}.after.vdom.js`)
  34. return done => {
  35. compileVue(source).then(code => {
  36. const id = String(Date.now() * Math.random())
  37. const instance = createInstance(id, code)
  38. setTimeout(() => {
  39. expect(getRoot(instance)).toEqual(before)
  40. const event = getEvents(instance)[0]
  41. fireEvent(instance, event.ref, event.type, {})
  42. setTimeout(() => {
  43. expect(getRoot(instance)).toEqual(after)
  44. instance.$destroy()
  45. done()
  46. }, 50)
  47. }, 50)
  48. }).catch(done.fail)
  49. }
  50. }
  51. describe('Usage', () => {
  52. describe('render', () => {
  53. it('sample', createRenderTestCase('render/sample'))
  54. it('class', createRenderTestCase('render/class'))
  55. })
  56. describe('event', () => {
  57. it('click', createEventTestCase('event/click'))
  58. })
  59. describe('recycle-list', () => {
  60. it('text node', createRenderTestCase('recycle-list/text-node'))
  61. it('attributes', createRenderTestCase('recycle-list/attrs'))
  62. // it('class name', createRenderTestCase('recycle-list/classname'))
  63. it('inline style', createRenderTestCase('recycle-list/inline-style'))
  64. it('v-if', createRenderTestCase('recycle-list/v-if'))
  65. it('v-else', createRenderTestCase('recycle-list/v-else'))
  66. it('v-else-if', createRenderTestCase('recycle-list/v-else-if'))
  67. it('v-for', createRenderTestCase('recycle-list/v-for'))
  68. it('v-for-iterator', createRenderTestCase('recycle-list/v-for-iterator'))
  69. it('v-on', createRenderTestCase('recycle-list/v-on'))
  70. it('v-on-inline', createRenderTestCase('recycle-list/v-on-inline'))
  71. it('v-once', createRenderTestCase('recycle-list/v-once'))
  72. it('stateless component', done => {
  73. compileWithDeps('recycle-list/components/stateless.vue', [{
  74. name: 'banner',
  75. path: 'recycle-list/components/banner.vue'
  76. }]).then(code => {
  77. const id = String(Date.now() * Math.random())
  78. const instance = createInstance(id, code)
  79. setTimeout(() => {
  80. const target = readObject('recycle-list/components/stateless.vdom.js')
  81. expect(getRoot(instance)).toEqual(target)
  82. instance.$destroy()
  83. done()
  84. }, 50)
  85. }).catch(done.fail)
  86. })
  87. it('stateless component with props', done => {
  88. compileWithDeps('recycle-list/components/stateless-with-props.vue', [{
  89. name: 'poster',
  90. path: 'recycle-list/components/poster.vue'
  91. }]).then(code => {
  92. const id = String(Date.now() * Math.random())
  93. const instance = createInstance(id, code)
  94. setTimeout(() => {
  95. const target = readObject('recycle-list/components/stateless-with-props.vdom.js')
  96. expect(getRoot(instance)).toEqual(target)
  97. instance.$destroy()
  98. done()
  99. }, 50)
  100. }).catch(done.fail)
  101. })
  102. it('multi stateless components', done => {
  103. compileWithDeps('recycle-list/components/stateless-multi-components.vue', [{
  104. name: 'banner',
  105. path: 'recycle-list/components/banner.vue'
  106. }, {
  107. name: 'poster',
  108. path: 'recycle-list/components/poster.vue'
  109. }, {
  110. name: 'footer',
  111. path: 'recycle-list/components/footer.vue'
  112. }]).then(code => {
  113. const id = String(Date.now() * Math.random())
  114. const instance = createInstance(id, code)
  115. setTimeout(() => {
  116. const target = readObject('recycle-list/components/stateless-multi-components.vdom.js')
  117. expect(getRoot(instance)).toEqual(target)
  118. instance.$destroy()
  119. done()
  120. }, 50)
  121. }).catch(done.fail)
  122. })
  123. it('stateful component', done => {
  124. const tasks = []
  125. addTaskHook((_, task) => tasks.push(task))
  126. compileWithDeps('recycle-list/components/stateful.vue', [{
  127. name: 'counter',
  128. path: 'recycle-list/components/counter.vue'
  129. }]).then(code => {
  130. const id = String(Date.now() * Math.random())
  131. const instance = createInstance(id, code)
  132. // expect(tasks.length).toEqual(3)
  133. setTimeout(() => {
  134. // check the render results
  135. const target = readObject('recycle-list/components/stateful.vdom.js')
  136. expect(getRoot(instance)).toEqual(target)
  137. tasks.length = 0
  138. // // trigger component hooks
  139. // instance.$triggerHook(
  140. // 2, // cid of the virtual component template
  141. // 'create', // lifecycle hook name
  142. // // arguments for the callback
  143. // [
  144. // 'x-1', // componentId of the virtual component
  145. // { start: 3 } // propsData of the virtual component
  146. // ]
  147. // )
  148. // instance.$triggerHook(2, 'create', ['x-2', { start: 11 }])
  149. // // the state (_data) of the virtual component should be sent to native
  150. // expect(tasks.length).toEqual(2)
  151. // expect(tasks[0].method).toEqual('updateComponentData')
  152. // expect(tasks[0].args).toEqual(['x-1', { count: 6 }, ''])
  153. // expect(tasks[1].method).toEqual('updateComponentData')
  154. // expect(tasks[1].args).toEqual(['x-2', { count: 22 }, ''])
  155. // instance.$triggerHook('x-1', 'attach')
  156. // instance.$triggerHook('x-2', 'attach')
  157. // tasks.length = 0
  158. // // simulate a click event
  159. // // the event will be caught by the virtual component template and
  160. // // should be dispatched to virtual component according to the componentId
  161. // const event = getEvents(instance)[0]
  162. // fireEvent(instance, event.ref, 'click', { componentId: 'x-1' })
  163. setTimeout(() => {
  164. // expect(tasks.length).toEqual(1)
  165. // expect(tasks[0].method).toEqual('updateComponentData')
  166. // expect(tasks[0].args).toEqual([{ count: 7 }])
  167. instance.$destroy()
  168. resetTaskHook()
  169. done()
  170. })
  171. }, 50)
  172. }).catch(done.fail)
  173. })
  174. // it('component lifecycle', done => {
  175. // global.__lifecycles = []
  176. // compileWithDeps('recycle-list/components/stateful-lifecycle.vue', [{
  177. // name: 'lifecycle',
  178. // path: 'recycle-list/components/lifecycle.vue'
  179. // }]).then(code => {
  180. // const id = String(Date.now() * Math.random())
  181. // const instance = createInstance(id, code)
  182. // setTimeout(() => {
  183. // const target = readObject('recycle-list/components/stateful-lifecycle.vdom.js')
  184. // expect(getRoot(instance)).toEqual(target)
  185. // instance.$triggerHook(2, 'create', ['y-1'])
  186. // instance.$triggerHook('y-1', 'attach')
  187. // instance.$triggerHook('y-1', 'detach')
  188. // expect(global.__lifecycles).toEqual([
  189. // 'beforeCreate undefined',
  190. // 'created 0',
  191. // 'beforeMount 1',
  192. // 'mounted 1',
  193. // 'beforeUpdate 2',
  194. // 'updated 2',
  195. // 'beforeDestroy 2',
  196. // 'destroyed 2'
  197. // ])
  198. // delete global.__lifecycles
  199. // instance.$destroy()
  200. // done()
  201. // }, 50)
  202. // }).catch(done.fail)
  203. // })
  204. it('stateful component with v-model', done => {
  205. compileWithDeps('recycle-list/components/stateful-v-model.vue', [{
  206. name: 'editor',
  207. path: 'recycle-list/components/editor.vue'
  208. }]).then(code => {
  209. const id = String(Date.now() * Math.random())
  210. const instance = createInstance(id, code)
  211. setTimeout(() => {
  212. const target = readObject('recycle-list/components/stateful-v-model.vdom.js')
  213. expect(getRoot(instance)).toEqual(target)
  214. instance.$destroy()
  215. done()
  216. }, 50)
  217. }).catch(done.fail)
  218. })
  219. })
  220. })