framework.spec.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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":{}}' }
  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. // TODO: deprecated, move to weex-js-runtime
  147. it('receiveTasks: fireEvent', (done) => {
  148. const id = String(Date.now() * Math.random())
  149. const instance = createInstance(id, `
  150. new Vue({
  151. data: {
  152. x: 'Hello'
  153. },
  154. methods: {
  155. update: function (e) {
  156. this.x = 'World'
  157. }
  158. },
  159. render: function (createElement) {
  160. return createElement('div', {}, [
  161. createElement('text', { attrs: { value: this.x }, on: { click: this.update }}, [])
  162. ])
  163. },
  164. el: "body"
  165. })
  166. `)
  167. expect(getRoot(instance)).toEqual({
  168. type: 'div',
  169. children: [{
  170. type: 'text',
  171. attr: { value: 'Hello' },
  172. event: ['click']
  173. }]
  174. })
  175. const textRef = framework.getRoot(id).children[0].ref
  176. framework.receiveTasks(id, [
  177. { method: 'fireEvent', args: [textRef, 'click'] }
  178. ])
  179. setTimeout(() => {
  180. expect(getRoot(instance)).toEqual({
  181. type: 'div',
  182. children: [{
  183. type: 'text',
  184. attr: { value: 'World' },
  185. event: ['click']
  186. }]
  187. })
  188. framework.destroyInstance(id)
  189. const result = framework.receiveTasks(id, [
  190. { method: 'fireEvent', args: [textRef, 'click'] }
  191. ])
  192. expect(result instanceof Error).toBe(true)
  193. done()
  194. })
  195. })
  196. it('vm.$getConfig', () => {
  197. const id = String(Date.now() * Math.random())
  198. global.WXEnvironment = {
  199. weexVersion: '0.10.0',
  200. platform: 'Node.js'
  201. }
  202. const instance = createInstance(id, `
  203. new Vue({
  204. render: function (createElement) {
  205. return createElement('div', {}, [
  206. createElement('text', { attrs: { value: JSON.stringify(this.$getConfig()) }}, [])
  207. ])
  208. },
  209. el: "body"
  210. })
  211. `, { bundleUrl: 'http://whatever.com/x.js' })
  212. expect(JSON.parse(getRoot(instance).children[0].attr.value)).toEqual({
  213. bundleUrl: 'http://whatever.com/x.js',
  214. env: {
  215. weexVersion: '0.10.0',
  216. platform: 'Node.js'
  217. }
  218. })
  219. delete global.WXEnvironment
  220. })
  221. it('registering global assets', () => {
  222. const id = String(Date.now() * Math.random())
  223. const instance = createInstance(id, `
  224. Vue.component('test', {
  225. render (h) {
  226. return h('div', 'Hello')
  227. }
  228. })
  229. new Vue({
  230. render (h) {
  231. return h('test')
  232. },
  233. el: 'body'
  234. })
  235. `)
  236. expect(getRoot(instance)).toEqual({
  237. type: 'div',
  238. children: [{ type: 'text', attr: { value: 'Hello' }}]
  239. })
  240. })
  241. it('adding prototype methods', () => {
  242. const id = String(Date.now() * Math.random())
  243. const instance = createInstance(id, `
  244. Vue.prototype.$test = () => 'Hello'
  245. const Test = {
  246. render (h) {
  247. return h('div', this.$test())
  248. }
  249. }
  250. new Vue({
  251. render (h) {
  252. return h(Test)
  253. },
  254. el: 'body'
  255. })
  256. `)
  257. expect(getRoot(instance)).toEqual({
  258. type: 'div',
  259. children: [{ type: 'text', attr: { value: 'Hello' }}]
  260. })
  261. })
  262. it('using global mixins', () => {
  263. const id = String(Date.now() * Math.random())
  264. const instance = createInstance(id, `
  265. Vue.mixin({
  266. created () {
  267. this.test = true
  268. }
  269. })
  270. const Test = {
  271. data: () => ({ test: false }),
  272. render (h) {
  273. return h('div', this.test ? 'Hello' : 'nope')
  274. }
  275. }
  276. new Vue({
  277. data: { test: false },
  278. render (h) {
  279. return this.test ? h(Test) : h('p')
  280. },
  281. el: 'body'
  282. })
  283. `)
  284. expect(getRoot(instance)).toEqual({
  285. type: 'div',
  286. children: [{ type: 'text', attr: { value: 'Hello' }}]
  287. })
  288. })
  289. })