renderFn.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { ShapeFlags } from '@vue/shared'
  2. import Vue from '@vue/compat'
  3. import { createComponentInstance } from '../../runtime-core/src/component'
  4. import { setCurrentRenderingInstance } from '../../runtime-core/src/componentRenderContext'
  5. import type { DirectiveBinding } from '../../runtime-core/src/directives'
  6. import { createVNode } from '../../runtime-core/src/vnode'
  7. import {
  8. DeprecationTypes,
  9. deprecationData,
  10. toggleDeprecationWarning,
  11. } from '../../runtime-core/src/compat/compatConfig'
  12. import { compatH as h } from '../../runtime-core/src/compat/renderFn'
  13. beforeEach(() => {
  14. toggleDeprecationWarning(false)
  15. Vue.configureCompat({
  16. MODE: 2,
  17. GLOBAL_MOUNT: 'suppress-warning',
  18. })
  19. })
  20. afterEach(() => {
  21. toggleDeprecationWarning(false)
  22. Vue.configureCompat({ MODE: 3 })
  23. })
  24. describe('compat: render function', () => {
  25. const mockDir = {}
  26. const mockChildComp = {}
  27. const mockComponent = {
  28. directives: {
  29. mockDir,
  30. },
  31. components: {
  32. foo: mockChildComp,
  33. },
  34. }
  35. const mockInstance = createComponentInstance(
  36. createVNode(mockComponent),
  37. null,
  38. null,
  39. )
  40. beforeEach(() => {
  41. setCurrentRenderingInstance(mockInstance)
  42. })
  43. afterEach(() => {
  44. setCurrentRenderingInstance(null)
  45. })
  46. test('string component lookup', () => {
  47. expect(h('foo')).toMatchObject({
  48. type: mockChildComp,
  49. })
  50. })
  51. test('class / style / attrs / domProps / props', () => {
  52. expect(
  53. h('div', {
  54. class: 'foo',
  55. style: { color: 'red' },
  56. attrs: {
  57. id: 'foo',
  58. },
  59. domProps: {
  60. innerHTML: 'hi',
  61. },
  62. props: {
  63. myProp: 'foo',
  64. },
  65. }),
  66. ).toMatchObject({
  67. props: {
  68. class: 'foo',
  69. style: { color: 'red' },
  70. id: 'foo',
  71. innerHTML: 'hi',
  72. myProp: 'foo',
  73. },
  74. })
  75. })
  76. test('staticClass + class', () => {
  77. expect(
  78. h('div', {
  79. class: { foo: true },
  80. staticClass: 'bar',
  81. }),
  82. ).toMatchObject({
  83. props: {
  84. class: 'bar foo',
  85. },
  86. })
  87. })
  88. test('staticStyle + style', () => {
  89. expect(
  90. h('div', {
  91. style: { color: 'red' },
  92. staticStyle: { fontSize: '14px' },
  93. }),
  94. ).toMatchObject({
  95. props: {
  96. style: {
  97. color: 'red',
  98. fontSize: '14px',
  99. },
  100. },
  101. })
  102. })
  103. test('on / nativeOn', () => {
  104. const fn = () => {}
  105. expect(
  106. h('div', {
  107. on: {
  108. click: fn,
  109. fooBar: fn,
  110. },
  111. nativeOn: {
  112. click: fn,
  113. 'bar-baz': fn,
  114. },
  115. }),
  116. ).toMatchObject({
  117. props: {
  118. onClick: fn,
  119. onClickNative: fn,
  120. onFooBar: fn,
  121. 'onBar-bazNative': fn,
  122. },
  123. })
  124. })
  125. test('v2 legacy event prefixes', () => {
  126. const fn = () => {}
  127. expect(
  128. h('div', {
  129. on: {
  130. '&click': fn,
  131. '~keyup': fn,
  132. '!touchend': fn,
  133. },
  134. }),
  135. ).toMatchObject({
  136. props: {
  137. onClickPassive: fn,
  138. onKeyupOnce: fn,
  139. onTouchendCapture: fn,
  140. },
  141. })
  142. })
  143. test('directives', () => {
  144. expect(
  145. h('div', {
  146. directives: [
  147. {
  148. name: 'mock-dir',
  149. value: '2',
  150. // expression: '1 + 1',
  151. arg: 'foo',
  152. modifiers: {
  153. bar: true,
  154. },
  155. },
  156. ],
  157. }),
  158. ).toMatchObject({
  159. dirs: [
  160. {
  161. dir: mockDir,
  162. instance: mockInstance.proxy,
  163. value: '2',
  164. oldValue: void 0,
  165. arg: 'foo',
  166. modifiers: {
  167. bar: true,
  168. },
  169. },
  170. ] as DirectiveBinding[],
  171. })
  172. })
  173. test('scopedSlots', () => {
  174. const scopedSlots = {
  175. default() {},
  176. }
  177. const vnode = h(mockComponent, {
  178. scopedSlots,
  179. })
  180. expect(vnode).toMatchObject({
  181. children: scopedSlots,
  182. })
  183. expect('scopedSlots' in vnode.props!).toBe(false)
  184. expect(vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN).toBeTruthy()
  185. })
  186. test('legacy named slot', () => {
  187. const vnode = h(mockComponent, [
  188. 'text',
  189. h('div', { slot: 'foo' }, 'one'),
  190. h('div', { slot: 'bar' }, 'two'),
  191. h('div', { slot: 'foo' }, 'three'),
  192. h('div', 'four'),
  193. ])
  194. expect(vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN).toBeTruthy()
  195. const slots = vnode.children as any
  196. // default
  197. expect(slots.default()).toMatchObject(['text', { children: 'four' }])
  198. expect(slots.foo()).toMatchObject([
  199. { children: 'one' },
  200. { children: 'three' },
  201. ])
  202. expect(slots.bar()).toMatchObject([{ children: 'two' }])
  203. })
  204. test('in component usage', () => {
  205. toggleDeprecationWarning(true)
  206. const vm = new Vue({
  207. render(h: any) {
  208. return h(
  209. 'div',
  210. {
  211. class: 'foo',
  212. attrs: { id: 'bar' },
  213. },
  214. 'hello',
  215. )
  216. },
  217. }).$mount()
  218. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  219. expect(vm.$el.outerHTML).toBe(`<div class="foo" id="bar">hello</div>`)
  220. expect(
  221. deprecationData[DeprecationTypes.RENDER_FUNCTION].message,
  222. ).toHaveBeenWarned()
  223. })
  224. test('should detect v3 compiled render fn', () => {
  225. const vm = new Vue({
  226. data() {
  227. return {
  228. a: 'hello',
  229. }
  230. },
  231. // check is arg length based
  232. render(c: any, _c: any) {
  233. return createVNode('div', null, c.a)
  234. },
  235. }).$mount()
  236. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  237. expect(vm.$el.outerHTML).toBe(`<div>hello</div>`)
  238. })
  239. })