framework.spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { getRoot, createInstance } from '../helpers/index'
  2. describe('framework APIs', () => {
  3. it('createInstance', () => {
  4. const id = String(Date.now() * Math.random())
  5. const instance = createInstance(id, `
  6. new Vue({
  7. render: function (createElement) {
  8. return createElement('div', {}, [
  9. createElement('text', { attrs: { value: 'Hello' }}, [])
  10. ])
  11. },
  12. el: "body"
  13. })
  14. `)
  15. expect(getRoot(instance)).toEqual({
  16. type: 'div',
  17. children: [{ type: 'text', attr: { value: 'Hello' }}]
  18. })
  19. })
  20. it('createInstance with config', () => {
  21. const id = String(Date.now() * Math.random())
  22. const instance = createInstance(id, `
  23. new Vue({
  24. render: function (createElement) {
  25. return createElement('div', {}, [
  26. createElement('text', { attrs: { value: JSON.stringify(weex.config) }}, [])
  27. ])
  28. },
  29. el: "body"
  30. })
  31. `, { bundleType: 'Vue', bundleUrl: 'http://example.com/', a: 1, b: 2 })
  32. expect(getRoot(instance)).toEqual({
  33. type: 'div',
  34. children: [{
  35. type: 'text',
  36. attr: { value: '{"bundleType":"Vue","bundleUrl":"http://example.com/","a":1,"b":2,"env":{}}' }
  37. }]
  38. })
  39. })
  40. it('createInstance with external data', () => {
  41. const id = String(Date.now() * Math.random())
  42. const instance = createInstance(id, `
  43. new Vue({
  44. data: {
  45. a: 1,
  46. b: 2
  47. },
  48. render: function (createElement) {
  49. return createElement('div', {}, [
  50. createElement('text', { attrs: { value: this.a + '-' + this.b }}, [])
  51. ])
  52. },
  53. el: "body"
  54. })
  55. `, undefined, { a: 111 })
  56. expect(getRoot(instance)).toEqual({
  57. type: 'div',
  58. children: [{ type: 'text', attr: { value: '111-2' }}]
  59. })
  60. })
  61. it('destroyInstance', (done) => {
  62. const id = String(Date.now() * Math.random())
  63. const instance = createInstance(id, `
  64. new Vue({
  65. data: {
  66. x: 'Hello'
  67. },
  68. render: function (createElement) {
  69. return createElement('div', {}, [
  70. createElement('text', { attrs: { value: this.x }}, [])
  71. ])
  72. },
  73. el: "body"
  74. })
  75. `)
  76. expect(getRoot(instance)).toEqual({
  77. type: 'div',
  78. children: [{ type: 'text', attr: { value: 'Hello' }}]
  79. })
  80. instance.$destroy()
  81. setTimeout(() => {
  82. expect(instance.document).toBeUndefined()
  83. expect(instance.app).toBeUndefined()
  84. done()
  85. }, 0)
  86. })
  87. it('refreshInstance', (done) => {
  88. const id = String(Date.now() * Math.random())
  89. const instance = createInstance(id, `
  90. new Vue({
  91. data: {
  92. x: 'Hello'
  93. },
  94. render: function (createElement) {
  95. return createElement('div', {}, [
  96. createElement('text', { attrs: { value: this.x }}, [])
  97. ])
  98. },
  99. el: "body"
  100. })
  101. `)
  102. expect(getRoot(instance)).toEqual({
  103. type: 'div',
  104. children: [{ type: 'text', attr: { value: 'Hello' }}]
  105. })
  106. instance.$refresh({ x: 'World' })
  107. setTimeout(() => {
  108. expect(getRoot(instance)).toEqual({
  109. type: 'div',
  110. children: [{ type: 'text', attr: { value: 'World' }}]
  111. })
  112. instance.$destroy()
  113. const result = instance.$refresh({ x: 'World' })
  114. expect(result instanceof Error).toBe(true)
  115. done()
  116. })
  117. })
  118. it('registering global assets', () => {
  119. const id = String(Date.now() * Math.random())
  120. const instance = createInstance(id, `
  121. Vue.component('test', {
  122. render (h) {
  123. return h('div', 'Hello')
  124. }
  125. })
  126. new Vue({
  127. render (h) {
  128. return h('test')
  129. },
  130. el: 'body'
  131. })
  132. `)
  133. expect(getRoot(instance)).toEqual({
  134. type: 'div',
  135. children: [{ type: 'text', attr: { value: 'Hello' }}]
  136. })
  137. })
  138. it('adding prototype methods', () => {
  139. const id = String(Date.now() * Math.random())
  140. const instance = createInstance(id, `
  141. Vue.prototype.$test = () => 'Hello'
  142. const Test = {
  143. render (h) {
  144. return h('div', this.$test())
  145. }
  146. }
  147. new Vue({
  148. render (h) {
  149. return h(Test)
  150. },
  151. el: 'body'
  152. })
  153. `)
  154. expect(getRoot(instance)).toEqual({
  155. type: 'div',
  156. children: [{ type: 'text', attr: { value: 'Hello' }}]
  157. })
  158. })
  159. it('using global mixins', () => {
  160. const id = String(Date.now() * Math.random())
  161. const instance = createInstance(id, `
  162. Vue.mixin({
  163. created () {
  164. this.test = true
  165. }
  166. })
  167. const Test = {
  168. data: () => ({ test: false }),
  169. render (h) {
  170. return h('div', this.test ? 'Hello' : 'nope')
  171. }
  172. }
  173. new Vue({
  174. data: { test: false },
  175. render (h) {
  176. return this.test ? h(Test) : h('p')
  177. },
  178. el: 'body'
  179. })
  180. `)
  181. expect(getRoot(instance)).toEqual({
  182. type: 'div',
  183. children: [{ type: 'text', attr: { value: 'Hello' }}]
  184. })
  185. })
  186. })