vdomInterop.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. import {
  2. KeepAlive,
  3. createVNode,
  4. defineComponent,
  5. h,
  6. nextTick,
  7. onActivated,
  8. onBeforeMount,
  9. onDeactivated,
  10. onMounted,
  11. onUnmounted,
  12. ref,
  13. renderSlot,
  14. toDisplayString,
  15. useModel,
  16. } from '@vue/runtime-dom'
  17. import { makeInteropRender } from './_utils'
  18. import {
  19. applyTextModel,
  20. applyVShow,
  21. child,
  22. createComponent,
  23. defineVaporAsyncComponent,
  24. defineVaporComponent,
  25. renderEffect,
  26. setText,
  27. template,
  28. } from '../src'
  29. const define = makeInteropRender()
  30. describe('vdomInterop', () => {
  31. describe('props', () => {
  32. test('should work if props are not provided', () => {
  33. const VaporChild = defineVaporComponent({
  34. props: {
  35. msg: String,
  36. },
  37. setup(_, { attrs }) {
  38. return [document.createTextNode(attrs.class || 'foo')]
  39. },
  40. })
  41. const { html } = define({
  42. setup() {
  43. return () => h(VaporChild as any)
  44. },
  45. }).render()
  46. expect(html()).toBe('foo')
  47. })
  48. })
  49. describe('v-model', () => {
  50. test('basic work', async () => {
  51. const VaporChild = defineVaporComponent({
  52. props: {
  53. modelValue: {},
  54. modelModifiers: {},
  55. },
  56. emits: ['update:modelValue'],
  57. setup(__props) {
  58. const modelValue = useModel(__props, 'modelValue')
  59. const n0 = template('<h1> </h1>')() as any
  60. const n1 = template('<input>')() as any
  61. const x0 = child(n0) as any
  62. applyTextModel(
  63. n1,
  64. () => modelValue.value,
  65. _value => (modelValue.value = _value),
  66. )
  67. renderEffect(() => setText(x0, toDisplayString(modelValue.value)))
  68. return [n0, n1]
  69. },
  70. })
  71. const { html, host } = define({
  72. setup() {
  73. const msg = ref('foo')
  74. return () =>
  75. h(VaporChild as any, {
  76. modelValue: msg.value,
  77. 'onUpdate:modelValue': (value: string) => {
  78. msg.value = value
  79. },
  80. })
  81. },
  82. }).render()
  83. expect(html()).toBe('<h1>foo</h1><input>')
  84. const inputEl = host.querySelector('input')!
  85. inputEl.value = 'bar'
  86. inputEl.dispatchEvent(new Event('input'))
  87. await nextTick()
  88. expect(html()).toBe('<h1>bar</h1><input>')
  89. })
  90. })
  91. describe('emit', () => {
  92. test('emit from vapor child to vdom parent', () => {
  93. const VaporChild = defineVaporComponent({
  94. emits: ['click'],
  95. setup(_, { emit }) {
  96. emit('click')
  97. return []
  98. },
  99. })
  100. const fn = vi.fn()
  101. define({
  102. setup() {
  103. return () => h(VaporChild as any, { onClick: fn })
  104. },
  105. }).render()
  106. // fn should be called once
  107. expect(fn).toHaveBeenCalledTimes(1)
  108. })
  109. })
  110. describe('v-show', () => {
  111. test('apply v-show to vdom child', async () => {
  112. const VDomChild = {
  113. setup() {
  114. return () => h('div')
  115. },
  116. }
  117. const show = ref(false)
  118. const VaporChild = defineVaporComponent({
  119. setup() {
  120. const n1 = createComponent(VDomChild as any)
  121. applyVShow(n1, () => show.value)
  122. return n1
  123. },
  124. })
  125. const { html } = define({
  126. setup() {
  127. return () => h(VaporChild as any)
  128. },
  129. }).render()
  130. expect(html()).toBe('<div style="display: none;"></div>')
  131. show.value = true
  132. await nextTick()
  133. expect(html()).toBe('<div style=""></div>')
  134. })
  135. })
  136. describe('slots', () => {
  137. test('basic', () => {
  138. const VDomChild = defineComponent({
  139. setup(_, { slots }) {
  140. return () => renderSlot(slots, 'default')
  141. },
  142. })
  143. const VaporChild = defineVaporComponent({
  144. setup() {
  145. return createComponent(
  146. VDomChild as any,
  147. null,
  148. {
  149. default: () => document.createTextNode('default slot'),
  150. },
  151. true,
  152. )
  153. },
  154. })
  155. const { html } = define({
  156. setup() {
  157. return () => h(VaporChild as any)
  158. },
  159. }).render()
  160. expect(html()).toBe('default slot')
  161. })
  162. test('functional slot', () => {
  163. const VDomChild = defineComponent({
  164. setup(_, { slots }) {
  165. return () => createVNode(slots.default!)
  166. },
  167. })
  168. const VaporChild = defineVaporComponent({
  169. setup() {
  170. return createComponent(
  171. VDomChild as any,
  172. null,
  173. {
  174. default: () => document.createTextNode('default slot'),
  175. },
  176. true,
  177. )
  178. },
  179. })
  180. const { html } = define({
  181. setup() {
  182. return () => h(VaporChild as any)
  183. },
  184. }).render()
  185. expect(html()).toBe('default slot')
  186. })
  187. })
  188. describe.todo('provide', () => {})
  189. describe.todo('inject', () => {})
  190. describe.todo('template ref', () => {})
  191. describe.todo('dynamic component', () => {})
  192. describe('attribute fallthrough', () => {
  193. it('should fallthrough attrs to vdom child', () => {
  194. const VDomChild = defineComponent({
  195. setup() {
  196. return () => h('div')
  197. },
  198. })
  199. const VaporChild = defineVaporComponent({
  200. setup() {
  201. return createComponent(
  202. VDomChild as any,
  203. { foo: () => 'vapor foo' },
  204. null,
  205. true,
  206. )
  207. },
  208. })
  209. const { html } = define({
  210. setup() {
  211. return () => h(VaporChild as any, { foo: 'foo', bar: 'bar' })
  212. },
  213. }).render()
  214. expect(html()).toBe('<div foo="foo" bar="bar"></div>')
  215. })
  216. it('should not fallthrough emit handlers to vdom child', () => {
  217. const VDomChild = defineComponent({
  218. emits: ['click'],
  219. setup(_, { emit }) {
  220. return () => h('button', { onClick: () => emit('click') }, 'click me')
  221. },
  222. })
  223. const fn = vi.fn()
  224. const VaporChild = defineVaporComponent({
  225. emits: ['click'],
  226. setup() {
  227. return createComponent(
  228. VDomChild as any,
  229. { onClick: () => fn },
  230. null,
  231. true,
  232. )
  233. },
  234. })
  235. const { host, html } = define({
  236. setup() {
  237. return () => h(VaporChild as any)
  238. },
  239. }).render()
  240. expect(html()).toBe('<button>click me</button>')
  241. const button = host.querySelector('button')!
  242. button.dispatchEvent(new Event('click'))
  243. // fn should be called once
  244. expect(fn).toHaveBeenCalledTimes(1)
  245. })
  246. })
  247. describe('async component', () => {
  248. const duration = 5
  249. test('render vapor async component', async () => {
  250. const VdomChild = {
  251. setup() {
  252. return () => h('div', 'foo')
  253. },
  254. }
  255. const VaporAsyncChild = defineVaporAsyncComponent({
  256. loader: () => {
  257. return new Promise(r => {
  258. setTimeout(() => {
  259. r(VdomChild as any)
  260. }, duration)
  261. })
  262. },
  263. loadingComponent: () => h('span', 'loading...'),
  264. })
  265. const { html } = define({
  266. setup() {
  267. return () => h(VaporAsyncChild as any)
  268. },
  269. }).render()
  270. expect(html()).toBe('<span>loading...</span><!--async component-->')
  271. await new Promise(r => setTimeout(r, duration))
  272. await nextTick()
  273. expect(html()).toBe('<div>foo</div><!--async component-->')
  274. })
  275. })
  276. describe('keepalive', () => {
  277. function assertHookCalls(
  278. hooks: {
  279. beforeMount: any
  280. mounted: any
  281. activated: any
  282. deactivated: any
  283. unmounted: any
  284. },
  285. callCounts: number[],
  286. ) {
  287. expect([
  288. hooks.beforeMount.mock.calls.length,
  289. hooks.mounted.mock.calls.length,
  290. hooks.activated.mock.calls.length,
  291. hooks.deactivated.mock.calls.length,
  292. hooks.unmounted.mock.calls.length,
  293. ]).toEqual(callCounts)
  294. }
  295. let hooks: any
  296. beforeEach(() => {
  297. hooks = {
  298. beforeMount: vi.fn(),
  299. mounted: vi.fn(),
  300. activated: vi.fn(),
  301. deactivated: vi.fn(),
  302. unmounted: vi.fn(),
  303. }
  304. })
  305. test('render vapor component', async () => {
  306. const VaporChild = defineVaporComponent({
  307. setup() {
  308. const msg = ref('vapor')
  309. onBeforeMount(() => hooks.beforeMount())
  310. onMounted(() => hooks.mounted())
  311. onActivated(() => hooks.activated())
  312. onDeactivated(() => hooks.deactivated())
  313. onUnmounted(() => hooks.unmounted())
  314. const n0 = template('<input type="text">', true)() as any
  315. applyTextModel(
  316. n0,
  317. () => msg.value,
  318. _value => (msg.value = _value),
  319. )
  320. return n0
  321. },
  322. })
  323. const show = ref(true)
  324. const toggle = ref(true)
  325. const { html, host } = define({
  326. setup() {
  327. return () =>
  328. show.value
  329. ? h(KeepAlive, null, {
  330. default: () => (toggle.value ? h(VaporChild as any) : null),
  331. })
  332. : null
  333. },
  334. }).render()
  335. expect(html()).toBe('<input type="text">')
  336. let inputEl = host.firstChild as HTMLInputElement
  337. expect(inputEl.value).toBe('vapor')
  338. assertHookCalls(hooks, [1, 1, 1, 0, 0])
  339. // change input value
  340. inputEl.value = 'changed'
  341. inputEl.dispatchEvent(new Event('input'))
  342. await nextTick()
  343. // deactivate
  344. toggle.value = false
  345. await nextTick()
  346. expect(html()).toBe('<!---->')
  347. assertHookCalls(hooks, [1, 1, 1, 1, 0])
  348. // activate
  349. toggle.value = true
  350. await nextTick()
  351. expect(html()).toBe('<input type="text">')
  352. inputEl = host.firstChild as HTMLInputElement
  353. expect(inputEl.value).toBe('changed')
  354. assertHookCalls(hooks, [1, 1, 2, 1, 0])
  355. // unmount keepalive
  356. show.value = false
  357. await nextTick()
  358. expect(html()).toBe('<!---->')
  359. assertHookCalls(hooks, [1, 1, 2, 2, 1])
  360. // mount keepalive
  361. show.value = true
  362. await nextTick()
  363. inputEl = host.firstChild as HTMLInputElement
  364. expect(inputEl.value).toBe('vapor')
  365. assertHookCalls(hooks, [2, 2, 3, 2, 1])
  366. })
  367. })
  368. })