framework.spec.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import * as framework from '../../../packages/weex-vue-framework'
  2. import { getRoot, createInstance } from '../helpers/index'
  3. describe('framework APIs', () => {
  4. it('createInstance', () => {
  5. const id = String(Date.now() * Math.random())
  6. const instance = createInstance(id, `
  7. new Vue({
  8. render: function (createElement) {
  9. return createElement('div', {}, [
  10. createElement('text', { attrs: { value: 'Hello' }}, [])
  11. ])
  12. },
  13. el: "body"
  14. })
  15. `)
  16. expect(getRoot(instance)).toEqual({
  17. type: 'div',
  18. children: [{ type: 'text', attr: { value: 'Hello' }}]
  19. })
  20. })
  21. it('createInstance with config', () => {
  22. const id = String(Date.now() * Math.random())
  23. const instance = createInstance(id, `
  24. new Vue({
  25. render: function (createElement) {
  26. return createElement('div', {}, [
  27. createElement('text', { attrs: { value: JSON.stringify(this.$getConfig()) }}, [])
  28. ])
  29. },
  30. el: "body"
  31. })
  32. `, { bundleUrl: 'http://example.com/', a: 1, b: 2 })
  33. expect(getRoot(instance)).toEqual({
  34. type: 'div',
  35. children: [{
  36. type: 'text',
  37. attr: { value: '{"bundleUrl":"http://example.com/","a":1,"b":2,"env":{},"bundleType":"Vue"}' }
  38. }]
  39. })
  40. })
  41. it('createInstance with external data', () => {
  42. const id = String(Date.now() * Math.random())
  43. const instance = createInstance(id, `
  44. new Vue({
  45. data: {
  46. a: 1,
  47. b: 2
  48. },
  49. render: function (createElement) {
  50. return createElement('div', {}, [
  51. createElement('text', { attrs: { value: this.a + '-' + this.b }}, [])
  52. ])
  53. },
  54. el: "body"
  55. })
  56. `, undefined, { a: 111 })
  57. expect(getRoot(instance)).toEqual({
  58. type: 'div',
  59. children: [{ type: 'text', attr: { value: '111-2' }}]
  60. })
  61. })
  62. it('destroyInstance', (done) => {
  63. const id = String(Date.now() * Math.random())
  64. const instance = createInstance(id, `
  65. new Vue({
  66. data: {
  67. x: 'Hello'
  68. },
  69. render: function (createElement) {
  70. return createElement('div', {}, [
  71. createElement('text', { attrs: { value: this.x }}, [])
  72. ])
  73. },
  74. el: "body"
  75. })
  76. `)
  77. expect(getRoot(instance)).toEqual({
  78. type: 'div',
  79. children: [{ type: 'text', attr: { value: 'Hello' }}]
  80. })
  81. instance.$destroy()
  82. setTimeout(() => {
  83. expect(instance.document).toBeUndefined()
  84. expect(instance.app).toBeUndefined()
  85. done()
  86. }, 0)
  87. })
  88. it('refreshInstance', (done) => {
  89. const id = String(Date.now() * Math.random())
  90. const instance = createInstance(id, `
  91. new Vue({
  92. data: {
  93. x: 'Hello'
  94. },
  95. render: function (createElement) {
  96. return createElement('div', {}, [
  97. createElement('text', { attrs: { value: this.x }}, [])
  98. ])
  99. },
  100. el: "body"
  101. })
  102. `)
  103. expect(getRoot(instance)).toEqual({
  104. type: 'div',
  105. children: [{ type: 'text', attr: { value: 'Hello' }}]
  106. })
  107. instance.$refresh({ x: 'World' })
  108. setTimeout(() => {
  109. expect(getRoot(instance)).toEqual({
  110. type: 'div',
  111. children: [{ type: 'text', attr: { value: 'World' }}]
  112. })
  113. instance.$destroy()
  114. const result = instance.$refresh({ x: 'World' })
  115. expect(result instanceof Error).toBe(true)
  116. done()
  117. })
  118. })
  119. it('getRoot', () => {
  120. const id = String(Date.now() * Math.random())
  121. const instance = createInstance(id, `
  122. new Vue({
  123. data: {
  124. x: 'Hello'
  125. },
  126. render: function (createElement) {
  127. return createElement('div', {}, [
  128. createElement('text', { attrs: { value: this.x }}, [])
  129. ])
  130. },
  131. el: "body"
  132. })
  133. `)
  134. let root = framework.getRoot(id)
  135. expect(root.ref).toEqual('_root')
  136. expect(root.type).toEqual('div')
  137. expect(root.children.length).toEqual(1)
  138. expect(root.children[0].type).toEqual('text')
  139. expect(root.children[0].attr).toEqual({ value: 'Hello' })
  140. framework.destroyInstance(instance.id)
  141. root = framework.getRoot(instance.id)
  142. expect(root instanceof Error).toBe(true)
  143. expect(root).toMatch(/getRoot/)
  144. expect(root).toMatch(/not found/)
  145. })
  146. it('vm.$getConfig', () => {
  147. const id = String(Date.now() * Math.random())
  148. global.WXEnvironment = {
  149. weexVersion: '0.10.0',
  150. platform: 'Node.js'
  151. }
  152. const instance = createInstance(id, `
  153. new Vue({
  154. render: function (createElement) {
  155. return createElement('div', {}, [
  156. createElement('text', { attrs: { value: JSON.stringify(this.$getConfig()) }}, [])
  157. ])
  158. },
  159. el: "body"
  160. })
  161. `, { bundleUrl: 'http://whatever.com/x.js' })
  162. expect(JSON.parse(getRoot(instance).children[0].attr.value)).toEqual({
  163. bundleUrl: 'http://whatever.com/x.js',
  164. bundleType: 'Vue',
  165. env: {
  166. weexVersion: '0.10.0',
  167. platform: 'Node.js'
  168. }
  169. })
  170. delete global.WXEnvironment
  171. })
  172. it('registering global assets', () => {
  173. const id = String(Date.now() * Math.random())
  174. const instance = createInstance(id, `
  175. Vue.component('test', {
  176. render (h) {
  177. return h('div', 'Hello')
  178. }
  179. })
  180. new Vue({
  181. render (h) {
  182. return h('test')
  183. },
  184. el: 'body'
  185. })
  186. `)
  187. expect(getRoot(instance)).toEqual({
  188. type: 'div',
  189. children: [{ type: 'text', attr: { value: 'Hello' }}]
  190. })
  191. })
  192. it('adding prototype methods', () => {
  193. const id = String(Date.now() * Math.random())
  194. const instance = createInstance(id, `
  195. Vue.prototype.$test = () => 'Hello'
  196. const Test = {
  197. render (h) {
  198. return h('div', this.$test())
  199. }
  200. }
  201. new Vue({
  202. render (h) {
  203. return h(Test)
  204. },
  205. el: 'body'
  206. })
  207. `)
  208. expect(getRoot(instance)).toEqual({
  209. type: 'div',
  210. children: [{ type: 'text', attr: { value: 'Hello' }}]
  211. })
  212. })
  213. it('using global mixins', () => {
  214. const id = String(Date.now() * Math.random())
  215. const instance = createInstance(id, `
  216. Vue.mixin({
  217. created () {
  218. this.test = true
  219. }
  220. })
  221. const Test = {
  222. data: () => ({ test: false }),
  223. render (h) {
  224. return h('div', this.test ? 'Hello' : 'nope')
  225. }
  226. }
  227. new Vue({
  228. data: { test: false },
  229. render (h) {
  230. return this.test ? h(Test) : h('p')
  231. },
  232. el: 'body'
  233. })
  234. `)
  235. expect(getRoot(instance)).toEqual({
  236. type: 'div',
  237. children: [{ type: 'text', attr: { value: 'Hello' }}]
  238. })
  239. })
  240. })