2
0

optimizer.spec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { baseOptions } from 'entries/web-compiler'
  4. describe('optimizer', () => {
  5. it('simple', () => {
  6. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  7. optimize(ast, baseOptions)
  8. expect(ast.static).toBe(true) // h1
  9. expect(ast.staticRoot).toBe(true)
  10. expect(ast.children[0].static).toBe(true) // text node
  11. })
  12. it('interpolation', () => {
  13. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  14. optimize(ast, baseOptions)
  15. expect(ast.static).toBe(false) // h1
  16. expect(ast.children[0].static).toBe(false) // text node with interpolation
  17. })
  18. it('nested elements', () => {
  19. const ast = parse('<ul><li>hello</li><li>world</li></ul>', baseOptions)
  20. optimize(ast, baseOptions)
  21. // ul
  22. expect(ast.static).toBe(true)
  23. expect(ast.staticRoot).toBe(true)
  24. // li
  25. expect(ast.children[0].static).toBe(true) // first
  26. expect(ast.children[1].static).toBe(true) // second
  27. // text node inside li
  28. expect(ast.children[0].children[0].static).toBe(true) // first
  29. expect(ast.children[1].children[0].static).toBe(true) // second
  30. })
  31. it('nested complex elements', () => {
  32. const ast = parse('<ul><li>{{msg1}}</li><li>---</li><li>{{msg2}}</li></ul>', baseOptions)
  33. optimize(ast, baseOptions)
  34. // ul
  35. expect(ast.static).toBe(false) // ul
  36. // li
  37. expect(ast.children[0].static).toBe(false) // firts
  38. expect(ast.children[1].static).toBe(true) // second
  39. expect(ast.children[2].static).toBe(false) // third
  40. // text node inside li
  41. expect(ast.children[0].children[0].static).toBe(false) // first
  42. expect(ast.children[1].children[0].static).toBe(true) // second
  43. expect(ast.children[2].children[0].static).toBe(false) // third
  44. })
  45. it('v-if directive', () => {
  46. const ast = parse('<h1 id="section1" v-if="show">hello world</h1>', baseOptions)
  47. optimize(ast, baseOptions)
  48. expect(ast.static).toBe(false)
  49. expect(ast.children[0].static).toBe(true)
  50. })
  51. it('v-else directive', () => {
  52. const ast = parse('<div><p v-if="show">hello world</p><p v-else>foo bar</p></div>', baseOptions)
  53. optimize(ast, baseOptions)
  54. expect(ast.static).toBe(false)
  55. expect(ast.children[0].static).toBe(false)
  56. expect(ast.children[0].elseBlock.static).toBeUndefined()
  57. })
  58. it('v-pre directive', () => {
  59. const ast = parse('<ul v-pre><li>{{msg}}</li><li>world</li></ul>', baseOptions)
  60. optimize(ast, baseOptions)
  61. expect(ast.static).toBe(true)
  62. expect(ast.staticRoot).toBe(true)
  63. expect(ast.children[0].static).toBe(true)
  64. expect(ast.children[1].static).toBe(true)
  65. expect(ast.children[0].children[0].static).toBe(true)
  66. expect(ast.children[1].children[0].static).toBe(true)
  67. })
  68. it('v-for directive', () => {
  69. const ast = parse('<ul><li v-for="item in items">hello world {{$index}}</li></ul>', baseOptions)
  70. optimize(ast, baseOptions)
  71. // ul
  72. expect(ast.static).toBe(false)
  73. // li with v-for
  74. expect(ast.children[0].static).toBe(false)
  75. expect(ast.children[0].children[0].static).toBe(false)
  76. })
  77. it('v-once directive', () => {
  78. const ast = parse('<p v-once>{{msg}}</p>', baseOptions)
  79. optimize(ast, baseOptions)
  80. expect(ast.static).toBe(false) // p
  81. expect(ast.children[0].static).toBe(false) // text node
  82. })
  83. it('render tag', () => {
  84. const ast = parse('<render :method="onRender"><p>hello</p></render>', baseOptions)
  85. optimize(ast, baseOptions)
  86. expect(ast.static).toBe(false)
  87. expect(ast.children[0].static).toBe(true)
  88. expect(ast.children[0].children[0].static).toBe(true)
  89. })
  90. it('single slot', () => {
  91. const ast = parse('<slot>hello</slot>', baseOptions)
  92. optimize(ast, baseOptions)
  93. expect(ast.static).toBe(false) // slot
  94. expect(ast.children[0].static).toBe(true) // text node
  95. })
  96. it('named slot', () => {
  97. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  98. optimize(ast, baseOptions)
  99. expect(ast.static).toBe(false) // slot
  100. expect(ast.children[0].static).toBe(true) // text node
  101. })
  102. it('slot target', () => {
  103. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  104. optimize(ast, baseOptions)
  105. expect(ast.static).toBe(false) // slot
  106. expect(ast.children[0].static).toBe(true) // text node
  107. })
  108. it('component', () => {
  109. const ast = parse('<my-component></my-component>', baseOptions)
  110. optimize(ast, baseOptions)
  111. expect(ast.static).toBe(false) // component
  112. })
  113. it('component for inline-template', () => {
  114. const ast = parse('<my-component inline-template><p>hello world</p><p>{{msg}}</p></my-component>', baseOptions)
  115. optimize(ast, baseOptions)
  116. // component
  117. expect(ast.static).toBe(false) // component
  118. // p
  119. expect(ast.children[0].static).toBe(true) // first
  120. expect(ast.children[1].static).toBe(false) // second
  121. // text node inside p
  122. expect(ast.children[0].children[0].static).toBe(true) // first
  123. expect(ast.children[1].children[0].static).toBe(false) // second
  124. })
  125. it('class binding', () => {
  126. const ast = parse('<p :class="class1">hello world</p>', baseOptions)
  127. optimize(ast, baseOptions)
  128. expect(ast.static).toBe(false)
  129. expect(ast.children[0].static).toBe(true)
  130. })
  131. it('style binding', () => {
  132. const ast = parse('<p :style="error">{{msg}}</p>', baseOptions)
  133. optimize(ast, baseOptions)
  134. expect(ast.static).toBe(false)
  135. expect(ast.children[0].static).toBe(false)
  136. })
  137. it('transition', () => {
  138. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  139. optimize(ast, baseOptions)
  140. expect(ast.static).toBe(false)
  141. expect(ast.children[0].static).toBe(true)
  142. })
  143. it('v-bind directive', () => {
  144. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  145. optimize(ast, baseOptions)
  146. expect(ast.static).toBe(false)
  147. })
  148. it('v-ref directive', () => {
  149. const ast = parse('<p v-ref:foo>hello world</p>', baseOptions)
  150. optimize(ast, baseOptions)
  151. expect(ast.static).toBe(false)
  152. expect(ast.children[0].static).toBe(true)
  153. })
  154. it('v-on directive', () => {
  155. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  156. optimize(ast, baseOptions)
  157. expect(ast.static).toBe(false)
  158. })
  159. it('custom directive', () => {
  160. const ast = parse('<fom><input type="text" name="field1" :value="msg" v-validate:field1="required"></form>', baseOptions)
  161. optimize(ast, baseOptions)
  162. expect(ast.static).toBe(false)
  163. expect(ast.children[0].static).toBe(false)
  164. })
  165. it('not root ast', () => {
  166. const ast = null
  167. optimize(ast, baseOptions)
  168. expect(ast).toBe(null)
  169. })
  170. it('not specified isReservedTag option', () => {
  171. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  172. optimize(ast, {})
  173. expect(ast.static).toBe(false)
  174. })
  175. })