cases.spec.js 8.3 KB

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