componentSlots.spec.ts 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
  2. import {
  3. child,
  4. createComponent,
  5. createFor,
  6. createForSlots,
  7. createIf,
  8. createSlot,
  9. createVaporApp,
  10. defineVaporComponent,
  11. insert,
  12. prepend,
  13. renderEffect,
  14. setInsertionState,
  15. template,
  16. vaporInteropPlugin,
  17. withVaporCtx,
  18. } from '../src'
  19. import {
  20. type Ref,
  21. createApp,
  22. createSlots,
  23. currentInstance,
  24. h,
  25. nextTick,
  26. ref,
  27. renderSlot,
  28. toDisplayString,
  29. } from '@vue/runtime-dom'
  30. import { makeRender } from './_utils'
  31. import type { DynamicSlot } from '../src/componentSlots'
  32. import { setElementText, setText } from '../src/dom/prop'
  33. const define = makeRender<any>()
  34. function renderWithSlots(slots: any): any {
  35. let instance: any
  36. const Comp = defineVaporComponent({
  37. setup() {
  38. const t0 = template('<div></div>')
  39. const n0 = t0()
  40. instance = currentInstance
  41. return n0
  42. },
  43. })
  44. const { render } = define({
  45. render() {
  46. return createComponent(Comp, {}, slots)
  47. },
  48. })
  49. render()
  50. return instance
  51. }
  52. describe('component: slots', () => {
  53. test('initSlots: instance.slots should be set correctly', () => {
  54. const { slots } = renderWithSlots({
  55. default: () => template('<span></span>')(),
  56. })
  57. expect(slots.default()).toMatchObject(document.createElement('span'))
  58. })
  59. test('updateSlots: instance.slots should be updated correctly', async () => {
  60. const flag1 = ref(true)
  61. let instance: any
  62. const Child = () => {
  63. instance = currentInstance
  64. return template('child')()
  65. }
  66. const { render } = define({
  67. render() {
  68. return createComponent(
  69. Child,
  70. {},
  71. {
  72. $: [
  73. () =>
  74. flag1.value
  75. ? { name: 'one', fn: () => template('<span></span>')() }
  76. : { name: 'two', fn: () => template('<div></div>')() },
  77. ],
  78. },
  79. )
  80. },
  81. })
  82. render()
  83. expect(instance.slots).toHaveProperty('one')
  84. expect(instance.slots).not.toHaveProperty('two')
  85. flag1.value = false
  86. await nextTick()
  87. expect(instance.slots).not.toHaveProperty('one')
  88. expect(instance.slots).toHaveProperty('two')
  89. })
  90. test('should work with createFlorSlots', async () => {
  91. const loop = ref([1, 2, 3])
  92. let instance: any
  93. const Child = () => {
  94. instance = currentInstance
  95. return template('child')()
  96. }
  97. const { render } = define({
  98. setup() {
  99. return createComponent(Child, null, {
  100. $: [
  101. () =>
  102. createForSlots(loop.value, (item, i) => ({
  103. name: item,
  104. fn: () => template(item + i)(),
  105. })),
  106. ],
  107. })
  108. },
  109. })
  110. render()
  111. expect(instance.slots).toHaveProperty('1')
  112. expect(instance.slots).toHaveProperty('2')
  113. expect(instance.slots).toHaveProperty('3')
  114. loop.value.push(4)
  115. await nextTick()
  116. expect(instance.slots).toHaveProperty('4')
  117. loop.value.shift()
  118. await nextTick()
  119. expect(instance.slots).not.toHaveProperty('1')
  120. })
  121. // passes but no warning for slot invocation in vapor currently
  122. test.todo('should not warn when mounting another app in setup', () => {
  123. const Comp = defineVaporComponent({
  124. setup(_, { slots }) {
  125. return slots.default!()
  126. },
  127. })
  128. const mountComp = () => {
  129. createVaporApp({
  130. render() {
  131. return createComponent(
  132. Comp,
  133. {},
  134. { default: () => template('msg')() },
  135. )!
  136. },
  137. })
  138. }
  139. const App = {
  140. setup() {
  141. mountComp()
  142. return []
  143. },
  144. }
  145. createVaporApp(App).mount(document.createElement('div'))
  146. expect(
  147. 'Slot "default" invoked outside of the render function',
  148. ).not.toHaveBeenWarned()
  149. })
  150. describe('createSlot', () => {
  151. test('slot should be rendered correctly', () => {
  152. const Comp = defineVaporComponent(() => {
  153. const n0 = template('<div>')()
  154. insert(createSlot('header'), n0 as any as ParentNode)
  155. return n0
  156. })
  157. const { host } = define(() => {
  158. return createComponent(Comp, null, {
  159. header: withVaporCtx(() => template('header')()),
  160. })
  161. }).render()
  162. expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
  163. })
  164. test('slot should be rendered correctly with slot props', async () => {
  165. const src = ref('header')
  166. const Comp = defineVaporComponent(() => {
  167. const n0 = template('<div></div>')()
  168. insert(
  169. createSlot('header', { title: () => src.value }),
  170. n0 as any as ParentNode,
  171. )
  172. return n0
  173. })
  174. const { host } = define(() => {
  175. return createComponent(Comp, null, {
  176. header: props => {
  177. const el = template('<h1></h1>')()
  178. renderEffect(() => {
  179. setElementText(el, props.title)
  180. })
  181. return el
  182. },
  183. })
  184. }).render()
  185. expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
  186. src.value = 'footer'
  187. await nextTick()
  188. expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
  189. })
  190. test('dynamic slot props', async () => {
  191. let props: any
  192. const bindObj = ref<Record<string, any>>({ foo: 1, baz: 'qux' })
  193. const Comp = defineVaporComponent(() =>
  194. createSlot('default', { $: [() => bindObj.value] }),
  195. )
  196. define(() =>
  197. createComponent(Comp, null, {
  198. default: withVaporCtx((_props: any) => ((props = _props), [])),
  199. }),
  200. ).render()
  201. expect(props).toEqual({ foo: 1, baz: 'qux' })
  202. bindObj.value.foo = 2
  203. await nextTick()
  204. expect(props).toEqual({ foo: 2, baz: 'qux' })
  205. delete bindObj.value.baz
  206. await nextTick()
  207. expect(props).toEqual({ foo: 2 })
  208. })
  209. test('dynamic slot props with static slot props', async () => {
  210. let props: any
  211. const foo = ref(0)
  212. const bindObj = ref<Record<string, any>>({ foo: 100, baz: 'qux' })
  213. const Comp = defineVaporComponent(() =>
  214. createSlot('default', {
  215. foo: () => foo.value,
  216. $: [() => bindObj.value],
  217. }),
  218. )
  219. define(() =>
  220. createComponent(Comp, null, {
  221. default: withVaporCtx((_props: any) => ((props = _props), [])),
  222. }),
  223. ).render()
  224. expect(props).toEqual({ foo: 100, baz: 'qux' })
  225. foo.value = 2
  226. await nextTick()
  227. expect(props).toEqual({ foo: 100, baz: 'qux' })
  228. delete bindObj.value.foo
  229. await nextTick()
  230. expect(props).toEqual({ foo: 2, baz: 'qux' })
  231. })
  232. test('dynamic slot should be rendered correctly with slot props', async () => {
  233. const val = ref('header')
  234. const Comp = defineVaporComponent(() => {
  235. const n0 = template('<div></div>')()
  236. prepend(
  237. n0 as any as ParentNode,
  238. createSlot('header', { title: () => val.value }),
  239. )
  240. return n0
  241. })
  242. const { host } = define(() => {
  243. // dynamic slot
  244. return createComponent(Comp, null, {
  245. $: [
  246. () => ({
  247. name: 'header',
  248. fn: withVaporCtx((props: any) => {
  249. const el = template('<h1></h1>')()
  250. renderEffect(() => {
  251. setElementText(el, props.title)
  252. })
  253. return el
  254. }),
  255. }),
  256. ],
  257. })
  258. }).render()
  259. expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
  260. val.value = 'footer'
  261. await nextTick()
  262. expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
  263. })
  264. test('dynamic slot outlet should be render correctly with slot props', async () => {
  265. const val = ref('header')
  266. const Comp = defineVaporComponent(() => {
  267. const n0 = template('<div></div>')()
  268. prepend(
  269. n0 as any as ParentNode,
  270. createSlot(
  271. () => val.value, // dynamic slot outlet name
  272. ),
  273. )
  274. return n0
  275. })
  276. const { host } = define(() => {
  277. return createComponent(Comp, null, {
  278. header: withVaporCtx(() => template('header')()),
  279. footer: withVaporCtx(() => template('footer')()),
  280. })
  281. }).render()
  282. expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
  283. val.value = 'footer'
  284. await nextTick()
  285. expect(host.innerHTML).toBe('<div>footer<!--slot--></div>')
  286. })
  287. test('fallback should be render correctly', () => {
  288. const Comp = defineVaporComponent(() => {
  289. const n0 = template('<div></div>')()
  290. insert(
  291. createSlot('header', undefined, () => template('fallback')()),
  292. n0 as any as ParentNode,
  293. )
  294. return n0
  295. })
  296. const { host } = define(() => {
  297. return createComponent(Comp, {}, {})
  298. }).render()
  299. expect(host.innerHTML).toBe('<div>fallback<!--slot--></div>')
  300. })
  301. test('dynamic slot should be updated correctly', async () => {
  302. const flag1 = ref(true)
  303. const Child = defineVaporComponent(() => {
  304. const temp0 = template('<p></p>')
  305. const el0 = temp0()
  306. const el1 = temp0()
  307. const slot1 = createSlot('one', null, () => template('one fallback')())
  308. const slot2 = createSlot('two', null, () => template('two fallback')())
  309. insert(slot1, el0 as any as ParentNode)
  310. insert(slot2, el1 as any as ParentNode)
  311. return [el0, el1]
  312. })
  313. const { host } = define(() => {
  314. return createComponent(Child, null, {
  315. $: [
  316. () =>
  317. flag1.value
  318. ? {
  319. name: 'one',
  320. fn: withVaporCtx(() => template('one content')()),
  321. }
  322. : {
  323. name: 'two',
  324. fn: withVaporCtx(() => template('two content')()),
  325. },
  326. ],
  327. })
  328. }).render()
  329. expect(host.innerHTML).toBe(
  330. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  331. )
  332. flag1.value = false
  333. await nextTick()
  334. expect(host.innerHTML).toBe(
  335. '<p>one fallback<!--slot--></p><p>two content<!--slot--></p>',
  336. )
  337. flag1.value = true
  338. await nextTick()
  339. expect(host.innerHTML).toBe(
  340. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  341. )
  342. })
  343. test('dynamic slot outlet should be updated correctly', async () => {
  344. const slotOutletName = ref('one')
  345. const Child = defineVaporComponent(() => {
  346. const temp0 = template('<p>')
  347. const el0 = temp0()
  348. const slot1 = createSlot(
  349. () => slotOutletName.value,
  350. undefined,
  351. () => template('fallback')(),
  352. )
  353. insert(slot1, el0 as any as ParentNode)
  354. return el0
  355. })
  356. const { host } = define(() => {
  357. return createComponent(
  358. Child,
  359. {},
  360. {
  361. one: withVaporCtx(() => template('one content')()),
  362. two: withVaporCtx(() => template('two content')()),
  363. },
  364. )
  365. }).render()
  366. expect(host.innerHTML).toBe('<p>one content<!--slot--></p>')
  367. slotOutletName.value = 'two'
  368. await nextTick()
  369. expect(host.innerHTML).toBe('<p>two content<!--slot--></p>')
  370. slotOutletName.value = 'none'
  371. await nextTick()
  372. expect(host.innerHTML).toBe('<p>fallback<!--slot--></p>')
  373. })
  374. test('non-exist slot', async () => {
  375. const Child = defineVaporComponent(() => {
  376. const el0 = template('<p>')()
  377. const slot = createSlot('not-exist', undefined)
  378. insert(slot, el0 as any as ParentNode)
  379. return el0
  380. })
  381. const { host } = define(() => {
  382. return createComponent(Child)
  383. }).render()
  384. expect(host.innerHTML).toBe('<p><!--slot--></p>')
  385. })
  386. test('use fallback when inner content changes', async () => {
  387. const Child = {
  388. setup() {
  389. return createSlot('default', null, () =>
  390. document.createTextNode('fallback'),
  391. )
  392. },
  393. }
  394. const toggle = ref(true)
  395. const { html } = define({
  396. setup() {
  397. return createComponent(Child, null, {
  398. default: withVaporCtx(() => {
  399. return createIf(
  400. () => toggle.value,
  401. () => {
  402. return document.createTextNode('content')
  403. },
  404. )
  405. }),
  406. })
  407. },
  408. }).render()
  409. expect(html()).toBe('content<!--if--><!--slot-->')
  410. toggle.value = false
  411. await nextTick()
  412. expect(html()).toBe('fallback<!--if--><!--slot-->')
  413. toggle.value = true
  414. await nextTick()
  415. expect(html()).toBe('content<!--if--><!--slot-->')
  416. })
  417. test('use fallback on initial render', async () => {
  418. const Child = {
  419. setup() {
  420. return createSlot('default', null, () =>
  421. document.createTextNode('fallback'),
  422. )
  423. },
  424. }
  425. const toggle = ref(false)
  426. const { html } = define({
  427. setup() {
  428. return createComponent(Child, null, {
  429. default: withVaporCtx(() => {
  430. return createIf(
  431. () => toggle.value,
  432. () => {
  433. return document.createTextNode('content')
  434. },
  435. )
  436. }),
  437. })
  438. },
  439. }).render()
  440. expect(html()).toBe('fallback<!--if--><!--slot-->')
  441. toggle.value = true
  442. await nextTick()
  443. expect(html()).toBe('content<!--if--><!--slot-->')
  444. toggle.value = false
  445. await nextTick()
  446. expect(html()).toBe('fallback<!--if--><!--slot-->')
  447. })
  448. test('dynamic slot work with v-if', async () => {
  449. const val = ref('header')
  450. const toggle = ref(false)
  451. const Comp = defineVaporComponent(() => {
  452. const n0 = template('<div></div>')()
  453. prepend(n0 as any as ParentNode, createSlot('header', null))
  454. return n0
  455. })
  456. const { host } = define(() => {
  457. // dynamic slot
  458. return createComponent(Comp, null, {
  459. $: [
  460. () =>
  461. (toggle.value
  462. ? {
  463. name: val.value,
  464. fn: withVaporCtx(() => {
  465. return template('<h1></h1>')()
  466. }),
  467. }
  468. : void 0) as DynamicSlot,
  469. ],
  470. })
  471. }).render()
  472. expect(host.innerHTML).toBe('<div><!--slot--></div>')
  473. toggle.value = true
  474. await nextTick()
  475. expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
  476. })
  477. test('render fallback when slot content is not valid', async () => {
  478. const Child = {
  479. setup() {
  480. return createSlot('default', null, () =>
  481. document.createTextNode('fallback'),
  482. )
  483. },
  484. }
  485. const { html } = define({
  486. setup() {
  487. return createComponent(Child, null, {
  488. default: withVaporCtx(() => {
  489. return template('<!--comment-->')()
  490. }),
  491. })
  492. },
  493. }).render()
  494. expect(html()).toBe('fallback<!--slot-->')
  495. })
  496. test('render fallback when v-if condition is false', async () => {
  497. const Child = {
  498. setup() {
  499. return createSlot('default', null, () =>
  500. document.createTextNode('fallback'),
  501. )
  502. },
  503. }
  504. const toggle = ref(false)
  505. const { html } = define({
  506. setup() {
  507. return createComponent(Child, null, {
  508. default: withVaporCtx(() => {
  509. return createIf(
  510. () => toggle.value,
  511. () => {
  512. return document.createTextNode('content')
  513. },
  514. )
  515. }),
  516. })
  517. },
  518. }).render()
  519. expect(html()).toBe('fallback<!--if--><!--slot-->')
  520. toggle.value = true
  521. await nextTick()
  522. expect(html()).toBe('content<!--if--><!--slot-->')
  523. toggle.value = false
  524. await nextTick()
  525. expect(html()).toBe('fallback<!--if--><!--slot-->')
  526. })
  527. test('render fallback with nested v-if', async () => {
  528. const Child = {
  529. setup() {
  530. return createSlot('default', null, () =>
  531. document.createTextNode('fallback'),
  532. )
  533. },
  534. }
  535. const outerShow = ref(false)
  536. const innerShow = ref(false)
  537. const { html } = define({
  538. setup() {
  539. return createComponent(Child, null, {
  540. default: withVaporCtx(() => {
  541. return createIf(
  542. () => outerShow.value,
  543. () => {
  544. return createIf(
  545. () => innerShow.value,
  546. () => {
  547. return document.createTextNode('content')
  548. },
  549. )
  550. },
  551. )
  552. }),
  553. })
  554. },
  555. }).render()
  556. expect(html()).toBe('fallback<!--if--><!--slot-->')
  557. outerShow.value = true
  558. await nextTick()
  559. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  560. innerShow.value = true
  561. await nextTick()
  562. expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
  563. innerShow.value = false
  564. await nextTick()
  565. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  566. outerShow.value = false
  567. await nextTick()
  568. expect(html()).toBe('fallback<!--if--><!--slot-->')
  569. outerShow.value = true
  570. await nextTick()
  571. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  572. innerShow.value = true
  573. await nextTick()
  574. expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
  575. })
  576. test('render fallback with v-for', async () => {
  577. const Child = {
  578. setup() {
  579. return createSlot('default', null, () =>
  580. document.createTextNode('fallback'),
  581. )
  582. },
  583. }
  584. const items = ref<number[]>([1])
  585. const { html } = define({
  586. setup() {
  587. return createComponent(Child, null, {
  588. default: withVaporCtx(() => {
  589. const n2 = createFor(
  590. () => items.value,
  591. for_item0 => {
  592. const n4 = template('<span> </span>')() as any
  593. const x4 = child(n4) as any
  594. renderEffect(() =>
  595. setText(x4, toDisplayString(for_item0.value)),
  596. )
  597. return n4
  598. },
  599. )
  600. return n2
  601. }),
  602. })
  603. },
  604. }).render()
  605. expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
  606. items.value.pop()
  607. await nextTick()
  608. expect(html()).toBe('fallback<!--for--><!--slot-->')
  609. items.value.pop()
  610. await nextTick()
  611. expect(html()).toBe('fallback<!--for--><!--slot-->')
  612. items.value.push(2)
  613. await nextTick()
  614. expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
  615. })
  616. test('render fallback with v-for (empty source)', async () => {
  617. const Child = {
  618. setup() {
  619. return createSlot('default', null, () =>
  620. document.createTextNode('fallback'),
  621. )
  622. },
  623. }
  624. const items = ref<number[]>([])
  625. const { html } = define({
  626. setup() {
  627. return createComponent(Child, null, {
  628. default: withVaporCtx(() => {
  629. const n2 = createFor(
  630. () => items.value,
  631. for_item0 => {
  632. const n4 = template('<span> </span>')() as any
  633. const x4 = child(n4) as any
  634. renderEffect(() =>
  635. setText(x4, toDisplayString(for_item0.value)),
  636. )
  637. return n4
  638. },
  639. )
  640. return n2
  641. }),
  642. })
  643. },
  644. }).render()
  645. expect(html()).toBe('fallback<!--for--><!--slot-->')
  646. items.value.push(1)
  647. await nextTick()
  648. expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
  649. items.value.pop()
  650. await nextTick()
  651. expect(html()).toBe('fallback<!--for--><!--slot-->')
  652. items.value.pop()
  653. await nextTick()
  654. expect(html()).toBe('fallback<!--for--><!--slot-->')
  655. items.value.push(2)
  656. await nextTick()
  657. expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
  658. })
  659. })
  660. describe('forwarded slot', () => {
  661. test('should work', async () => {
  662. const Child = defineVaporComponent({
  663. setup() {
  664. return createSlot('foo', null)
  665. },
  666. })
  667. const Parent = defineVaporComponent({
  668. setup() {
  669. const n2 = createComponent(
  670. Child,
  671. null,
  672. {
  673. foo: withVaporCtx(() => {
  674. return createSlot('foo', null)
  675. }),
  676. },
  677. true,
  678. )
  679. return n2
  680. },
  681. })
  682. const foo = ref('foo')
  683. const { host } = define({
  684. setup() {
  685. const n2 = createComponent(
  686. Parent,
  687. null,
  688. {
  689. foo: withVaporCtx(() => {
  690. const n0 = template(' ')() as any
  691. renderEffect(() => setText(n0, foo.value))
  692. return n0
  693. }),
  694. },
  695. true,
  696. )
  697. return n2
  698. },
  699. }).render()
  700. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->')
  701. foo.value = 'bar'
  702. await nextTick()
  703. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->')
  704. })
  705. test('mixed with non-forwarded slot', async () => {
  706. const Child = defineVaporComponent({
  707. setup() {
  708. return [createSlot('foo', null)]
  709. },
  710. })
  711. const Parent = defineVaporComponent({
  712. setup() {
  713. const n2 = createComponent(Child, null, {
  714. foo: withVaporCtx(() => {
  715. const n0 = createSlot('foo', null)
  716. return n0
  717. }),
  718. })
  719. const n3 = createSlot('default', null)
  720. return [n2, n3]
  721. },
  722. })
  723. const foo = ref('foo')
  724. const { host } = define({
  725. setup() {
  726. const n2 = createComponent(
  727. Parent,
  728. null,
  729. {
  730. foo: withVaporCtx(() => {
  731. const n0 = template(' ')() as any
  732. renderEffect(() => setText(n0, foo.value))
  733. return n0
  734. }),
  735. default: withVaporCtx(() => {
  736. const n3 = template(' ')() as any
  737. renderEffect(() => setText(n3, foo.value))
  738. return n3
  739. }),
  740. },
  741. true,
  742. )
  743. return n2
  744. },
  745. }).render()
  746. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->foo<!--slot-->')
  747. foo.value = 'bar'
  748. await nextTick()
  749. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->bar<!--slot-->')
  750. })
  751. test('forwarded slot with fallback', async () => {
  752. const Child = defineVaporComponent({
  753. setup() {
  754. return createSlot('default', null, () => template('child fallback')())
  755. },
  756. })
  757. const Parent = defineVaporComponent({
  758. setup() {
  759. const n2 = createComponent(Child, null, {
  760. default: withVaporCtx(() => {
  761. const n0 = createSlot('default', null, () => {
  762. return template('<!-- <div></div> -->')()
  763. })
  764. return n0
  765. }),
  766. })
  767. return n2
  768. },
  769. })
  770. const { html } = define({
  771. setup() {
  772. return createComponent(Parent, null, {
  773. default: withVaporCtx(() => template('<!-- <div>App</div> -->')()),
  774. })
  775. },
  776. }).render()
  777. expect(html()).toBe('child fallback<!--slot--><!--slot-->')
  778. })
  779. test('forwarded slot with fallback (v-if)', async () => {
  780. const Child = defineVaporComponent({
  781. setup() {
  782. return createSlot('default', null, () => template('child fallback')())
  783. },
  784. })
  785. const show = ref(false)
  786. const Parent = defineVaporComponent({
  787. setup() {
  788. const n2 = createComponent(Child, null, {
  789. default: withVaporCtx(() => {
  790. const n0 = createSlot('default', null, () => {
  791. const n2 = createIf(
  792. () => show.value,
  793. () => {
  794. const n4 = template('<div>if content</div>')()
  795. return n4
  796. },
  797. )
  798. return n2
  799. })
  800. return n0
  801. }),
  802. })
  803. return n2
  804. },
  805. })
  806. const { html } = define({
  807. setup() {
  808. return createComponent(Parent, null, {
  809. default: withVaporCtx(() => template('<!-- <div>App</div> -->')()),
  810. })
  811. },
  812. }).render()
  813. expect(html()).toBe('child fallback<!--if--><!--slot--><!--slot-->')
  814. show.value = true
  815. await nextTick()
  816. expect(html()).toBe(
  817. '<div>if content</div><!--if--><!--slot--><!--slot-->',
  818. )
  819. })
  820. test('forwarded slot with fallback (v-for)', async () => {
  821. const Child = defineVaporComponent({
  822. setup() {
  823. return createSlot('default', null, () => template('child fallback')())
  824. },
  825. })
  826. const items = ref<number[]>([])
  827. const Parent = defineVaporComponent({
  828. setup() {
  829. const n2 = createComponent(Child, null, {
  830. default: withVaporCtx(() => {
  831. const n0 = createSlot('default', null, () => {
  832. const n2 = createFor(
  833. () => items.value,
  834. for_item0 => {
  835. const n4 = template('<span> </span>')() as any
  836. const x4 = child(n4) as any
  837. renderEffect(() =>
  838. setText(x4, toDisplayString(for_item0.value)),
  839. )
  840. return n4
  841. },
  842. )
  843. return n2
  844. })
  845. return n0
  846. }),
  847. })
  848. return n2
  849. },
  850. })
  851. const { html } = define({
  852. setup() {
  853. return createComponent(Parent, null, {
  854. default: () => template('<!-- <div>App</div> -->')(),
  855. })
  856. },
  857. }).render()
  858. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  859. items.value.push(1)
  860. await nextTick()
  861. expect(html()).toBe('<span>1</span><!--for--><!--slot--><!--slot-->')
  862. items.value.pop()
  863. await nextTick()
  864. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  865. })
  866. test('consecutive slots with insertion state', async () => {
  867. const { component: Child } = define({
  868. setup() {
  869. const n2 = template('<div><div>baz</div></div>', true)() as any
  870. setInsertionState(n2, 0)
  871. createSlot('default', null)
  872. setInsertionState(n2, 0)
  873. createSlot('foo', null)
  874. return n2
  875. },
  876. })
  877. const { html } = define({
  878. setup() {
  879. return createComponent(Child, null, {
  880. default: withVaporCtx(() => template('default')()),
  881. foo: withVaporCtx(() => template('foo')()),
  882. })
  883. },
  884. }).render()
  885. expect(html()).toBe(
  886. `<div>` +
  887. `default<!--slot-->` +
  888. `foo<!--slot-->` +
  889. `<div>baz</div>` +
  890. `</div>`,
  891. )
  892. })
  893. describe('vdom interop', () => {
  894. const createVaporSlot = (fallbackText = 'fallback') => {
  895. return defineVaporComponent({
  896. setup() {
  897. const n0 = createSlot('foo', null, () => {
  898. const n2 = template(`<div>${fallbackText}</div>`)()
  899. return n2
  900. })
  901. return n0
  902. },
  903. })
  904. }
  905. const createVdomSlot = (fallbackText = 'fallback') => {
  906. return {
  907. render(this: any) {
  908. return renderSlot(this.$slots, 'foo', {}, () => [
  909. h('div', fallbackText),
  910. ])
  911. },
  912. }
  913. }
  914. const createVaporForwardedSlot = (
  915. targetComponent: any,
  916. fallbackText?: string,
  917. ) => {
  918. return defineVaporComponent({
  919. setup() {
  920. const n2 = createComponent(
  921. targetComponent,
  922. null,
  923. {
  924. foo: withVaporCtx(() => {
  925. return fallbackText
  926. ? createSlot('foo', null, () => {
  927. const n2 = template(`<div>${fallbackText}</div>`)()
  928. return n2
  929. })
  930. : createSlot('foo', null)
  931. }),
  932. },
  933. true,
  934. )
  935. return n2
  936. },
  937. })
  938. }
  939. const createVdomForwardedSlot = (
  940. targetComponent: any,
  941. fallbackText?: string,
  942. ) => {
  943. return {
  944. render(this: any) {
  945. return h(targetComponent, null, {
  946. foo: () => [
  947. fallbackText
  948. ? renderSlot(this.$slots, 'foo', {}, () => [
  949. h('div', fallbackText),
  950. ])
  951. : renderSlot(this.$slots, 'foo'),
  952. ],
  953. _: 3 /* FORWARDED */,
  954. })
  955. },
  956. }
  957. }
  958. const createMultipleVaporForwardedSlots = (
  959. targetComponent: any,
  960. count: number,
  961. ) => {
  962. let current = targetComponent
  963. for (let i = 0; i < count; i++) {
  964. current = createVaporForwardedSlot(current)
  965. }
  966. return current
  967. }
  968. const createMultipleVdomForwardedSlots = (
  969. targetComponent: any,
  970. count: number,
  971. ) => {
  972. let current = targetComponent
  973. for (let i = 0; i < count; i++) {
  974. current = createVdomForwardedSlot(current)
  975. }
  976. return current
  977. }
  978. const createTestApp = (
  979. rootComponent: any,
  980. foo: Ref<string>,
  981. show: Ref<Boolean>,
  982. ) => {
  983. return {
  984. setup() {
  985. return () =>
  986. h(
  987. rootComponent,
  988. null,
  989. createSlots({ _: 2 /* DYNAMIC */ } as any, [
  990. show.value
  991. ? {
  992. name: 'foo',
  993. fn: () => [h('span', foo.value)],
  994. key: '0',
  995. }
  996. : undefined,
  997. ]),
  998. )
  999. },
  1000. }
  1001. }
  1002. const createEmptyTestApp = (rootComponent: any) => {
  1003. return {
  1004. setup() {
  1005. return () => h(rootComponent)
  1006. },
  1007. }
  1008. }
  1009. test('vdom slot > vapor forwarded slot > vapor slot', async () => {
  1010. const foo = ref('foo')
  1011. const show = ref(true)
  1012. const VaporSlot = createVaporSlot()
  1013. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1014. const App = createTestApp(VaporForwardedSlot, foo, show)
  1015. const root = document.createElement('div')
  1016. createApp(App).use(vaporInteropPlugin).mount(root)
  1017. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1018. foo.value = 'bar'
  1019. await nextTick()
  1020. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1021. show.value = false
  1022. await nextTick()
  1023. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1024. })
  1025. test('vdom slot > vapor forwarded slot(with fallback) > vapor slot', async () => {
  1026. const foo = ref('foo')
  1027. const show = ref(true)
  1028. const VaporSlot = createVaporSlot()
  1029. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1030. VaporSlot,
  1031. 'forwarded fallback',
  1032. )
  1033. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1034. const root = document.createElement('div')
  1035. createApp(App).use(vaporInteropPlugin).mount(root)
  1036. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1037. foo.value = 'bar'
  1038. await nextTick()
  1039. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1040. show.value = false
  1041. await nextTick()
  1042. expect(root.innerHTML).toBe('<div>forwarded fallback</div><!--slot-->')
  1043. })
  1044. test('vdom slot > vapor forwarded slot > vdom slot', async () => {
  1045. const foo = ref('foo')
  1046. const show = ref(true)
  1047. const VdomSlot = createVdomSlot()
  1048. const VaporForwardedSlot = createVaporForwardedSlot(VdomSlot)
  1049. const App = createTestApp(VaporForwardedSlot, foo, show)
  1050. const root = document.createElement('div')
  1051. createApp(App).use(vaporInteropPlugin).mount(root)
  1052. expect(root.innerHTML).toBe('<span>foo</span>')
  1053. foo.value = 'bar'
  1054. await nextTick()
  1055. expect(root.innerHTML).toBe('<span>bar</span>')
  1056. show.value = false
  1057. await nextTick()
  1058. expect(root.innerHTML).toBe('<div>fallback</div>')
  1059. show.value = true
  1060. await nextTick()
  1061. expect(root.innerHTML).toBe('<span>bar</span>')
  1062. })
  1063. test('vdom slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1064. const foo = ref('foo')
  1065. const show = ref(true)
  1066. const VdomSlot = createVdomSlot()
  1067. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1068. VdomSlot,
  1069. 'forwarded fallback',
  1070. )
  1071. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1072. const root = document.createElement('div')
  1073. createApp(App).use(vaporInteropPlugin).mount(root)
  1074. expect(root.innerHTML).toBe('<span>foo</span>')
  1075. foo.value = 'bar'
  1076. await nextTick()
  1077. expect(root.innerHTML).toBe('<span>bar</span>')
  1078. show.value = false
  1079. await nextTick()
  1080. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1081. })
  1082. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vapor slot', async () => {
  1083. const foo = ref('foo')
  1084. const show = ref(true)
  1085. const VaporSlot = createVaporSlot()
  1086. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1087. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1088. const App = createTestApp(VaporForwardedSlot, foo, show)
  1089. const root = document.createElement('div')
  1090. createApp(App).use(vaporInteropPlugin).mount(root)
  1091. expect(root.innerHTML).toBe('<span>foo</span>')
  1092. foo.value = 'bar'
  1093. await nextTick()
  1094. expect(root.innerHTML).toBe('<span>bar</span>')
  1095. show.value = false
  1096. await nextTick()
  1097. expect(root.innerHTML).toBe('<div>fallback</div>')
  1098. show.value = true
  1099. await nextTick()
  1100. expect(root.innerHTML).toBe('<span>bar</span>')
  1101. })
  1102. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vapor slot', async () => {
  1103. const foo = ref('foo')
  1104. const show = ref(true)
  1105. const VaporSlot = createVaporSlot()
  1106. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1107. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1108. VdomForwardedSlot,
  1109. 'forwarded fallback',
  1110. )
  1111. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1112. const root = document.createElement('div')
  1113. createApp(App).use(vaporInteropPlugin).mount(root)
  1114. expect(root.innerHTML).toBe('<span>foo</span>')
  1115. foo.value = 'bar'
  1116. await nextTick()
  1117. expect(root.innerHTML).toBe('<span>bar</span>')
  1118. show.value = false
  1119. await nextTick()
  1120. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1121. show.value = true
  1122. await nextTick()
  1123. expect(root.innerHTML).toBe('<span>bar</span>')
  1124. })
  1125. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1126. const foo = ref('foo')
  1127. const show = ref(true)
  1128. const VaporSlot = createVaporSlot()
  1129. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1130. VaporSlot,
  1131. 'vdom fallback',
  1132. )
  1133. const VaporForwardedSlot = createVaporForwardedSlot(
  1134. VdomForwardedSlotWithFallback,
  1135. )
  1136. const App = createTestApp(VaporForwardedSlot, foo, show)
  1137. const root = document.createElement('div')
  1138. createApp(App).use(vaporInteropPlugin).mount(root)
  1139. expect(root.innerHTML).toBe('<span>foo</span>')
  1140. foo.value = 'bar'
  1141. await nextTick()
  1142. expect(root.innerHTML).toBe('<span>bar</span>')
  1143. show.value = false
  1144. await nextTick()
  1145. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1146. show.value = true
  1147. await nextTick()
  1148. expect(root.innerHTML).toBe('<span>bar</span>')
  1149. })
  1150. test('vdom slot(empty) > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1151. const VaporSlot = createVaporSlot()
  1152. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1153. VaporSlot,
  1154. 'vdom fallback',
  1155. )
  1156. const VaporForwardedSlot = createVaporForwardedSlot(
  1157. VdomForwardedSlotWithFallback,
  1158. )
  1159. const App = createEmptyTestApp(VaporForwardedSlot)
  1160. const root = document.createElement('div')
  1161. createApp(App).use(vaporInteropPlugin).mount(root)
  1162. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1163. })
  1164. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vdom slot', async () => {
  1165. const foo = ref('foo')
  1166. const show = ref(true)
  1167. const VdomSlot = createVdomSlot()
  1168. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1169. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1170. const App = createTestApp(VaporForwardedSlot, foo, show)
  1171. const root = document.createElement('div')
  1172. createApp(App).use(vaporInteropPlugin).mount(root)
  1173. expect(root.innerHTML).toBe('<span>foo</span>')
  1174. foo.value = 'bar'
  1175. await nextTick()
  1176. expect(root.innerHTML).toBe('<span>bar</span>')
  1177. show.value = false
  1178. await nextTick()
  1179. expect(root.innerHTML).toBe('<div>fallback</div>')
  1180. show.value = true
  1181. await nextTick()
  1182. expect(root.innerHTML).toBe('<span>bar</span>')
  1183. })
  1184. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vdom slot', async () => {
  1185. const foo = ref('foo')
  1186. const show = ref(true)
  1187. const VdomSlot = createVdomSlot()
  1188. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1189. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1190. VdomForwardedSlot,
  1191. 'vapor fallback',
  1192. )
  1193. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1194. const root = document.createElement('div')
  1195. createApp(App).use(vaporInteropPlugin).mount(root)
  1196. expect(root.innerHTML).toBe('<span>foo</span>')
  1197. foo.value = 'bar'
  1198. await nextTick()
  1199. expect(root.innerHTML).toBe('<span>bar</span>')
  1200. show.value = false
  1201. await nextTick()
  1202. expect(root.innerHTML).toBe('<div>vapor fallback</div>')
  1203. show.value = true
  1204. await nextTick()
  1205. expect(root.innerHTML).toBe('<span>bar</span>')
  1206. })
  1207. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1208. const foo = ref('foo')
  1209. const show = ref(true)
  1210. const VdomSlot = createVdomSlot()
  1211. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1212. VdomSlot,
  1213. 'vdom fallback',
  1214. )
  1215. const VaporForwardedSlot = createVaporForwardedSlot(
  1216. VdomForwardedSlotWithFallback,
  1217. )
  1218. const App = createTestApp(VaporForwardedSlot, foo, show)
  1219. const root = document.createElement('div')
  1220. createApp(App).use(vaporInteropPlugin).mount(root)
  1221. expect(root.innerHTML).toBe('<span>foo</span>')
  1222. foo.value = 'bar'
  1223. await nextTick()
  1224. expect(root.innerHTML).toBe('<span>bar</span>')
  1225. show.value = false
  1226. await nextTick()
  1227. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1228. show.value = true
  1229. await nextTick()
  1230. expect(root.innerHTML).toBe('<span>bar</span>')
  1231. })
  1232. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot > vdom slot', async () => {
  1233. const foo = ref('foo')
  1234. const show = ref(true)
  1235. const VdomSlot = createVdomSlot()
  1236. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1237. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1238. VdomForwardedSlot,
  1239. 3,
  1240. )
  1241. const App = createTestApp(VaporForwardedSlot, foo, show)
  1242. const root = document.createElement('div')
  1243. createApp(App).use(vaporInteropPlugin).mount(root)
  1244. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1245. foo.value = 'bar'
  1246. await nextTick()
  1247. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1248. show.value = false
  1249. await nextTick()
  1250. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1251. show.value = true
  1252. await nextTick()
  1253. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1254. })
  1255. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1256. const foo = ref('foo')
  1257. const show = ref(true)
  1258. const VdomSlot = createVdomSlot()
  1259. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1260. VdomSlot,
  1261. 'vdom fallback',
  1262. )
  1263. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1264. VdomForwardedSlotWithFallback,
  1265. 3,
  1266. )
  1267. const App = createTestApp(VaporForwardedSlot, foo, show)
  1268. const root = document.createElement('div')
  1269. createApp(App).use(vaporInteropPlugin).mount(root)
  1270. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1271. foo.value = 'bar'
  1272. await nextTick()
  1273. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1274. show.value = false
  1275. await nextTick()
  1276. expect(root.innerHTML).toBe(
  1277. '<div>vdom fallback</div><!--slot--><!--slot-->',
  1278. )
  1279. show.value = true
  1280. await nextTick()
  1281. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1282. })
  1283. test('vdom slot > vdom forwarded slot > vapor slot', async () => {
  1284. const foo = ref('foo')
  1285. const show = ref(true)
  1286. const VaporSlot = createVaporSlot()
  1287. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1288. const App = createTestApp(VdomForwardedSlot, foo, show)
  1289. const root = document.createElement('div')
  1290. createApp(App).use(vaporInteropPlugin).mount(root)
  1291. expect(root.innerHTML).toBe('<span>foo</span>')
  1292. foo.value = 'bar'
  1293. await nextTick()
  1294. expect(root.innerHTML).toBe('<span>bar</span>')
  1295. show.value = false
  1296. await nextTick()
  1297. expect(root.innerHTML).toBe('<div>fallback</div>')
  1298. show.value = true
  1299. await nextTick()
  1300. expect(root.innerHTML).toBe('<span>bar</span>')
  1301. })
  1302. test('vdom slot > vdom forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1303. const foo = ref('foo')
  1304. const show = ref(true)
  1305. const VaporSlot = createVaporSlot()
  1306. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1307. const VdomForwardedSlot = createVdomForwardedSlot(VaporForwardedSlot)
  1308. const App = createTestApp(VdomForwardedSlot, foo, show)
  1309. const root = document.createElement('div')
  1310. createApp(App).use(vaporInteropPlugin).mount(root)
  1311. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1312. foo.value = 'bar'
  1313. await nextTick()
  1314. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1315. show.value = false
  1316. await nextTick()
  1317. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1318. show.value = true
  1319. await nextTick()
  1320. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1321. })
  1322. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot > vdom slot', async () => {
  1323. const foo = ref('foo')
  1324. const show = ref(true)
  1325. const VaporSlot = createVaporSlot()
  1326. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1327. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1328. VaporForwardedSlot,
  1329. 3,
  1330. )
  1331. const App = createTestApp(VdomForwardedSlot, foo, show)
  1332. const root = document.createElement('div')
  1333. createApp(App).use(vaporInteropPlugin).mount(root)
  1334. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1335. foo.value = 'bar'
  1336. await nextTick()
  1337. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1338. show.value = false
  1339. await nextTick()
  1340. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1341. show.value = true
  1342. await nextTick()
  1343. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1344. })
  1345. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1346. const foo = ref('foo')
  1347. const show = ref(true)
  1348. const VaporSlot = createVaporSlot()
  1349. const VaporForwardedSlot = createVaporForwardedSlot(
  1350. VaporSlot,
  1351. 'vapor fallback',
  1352. )
  1353. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1354. VaporForwardedSlot,
  1355. 3,
  1356. )
  1357. const App = createTestApp(VdomForwardedSlot, foo, show)
  1358. const root = document.createElement('div')
  1359. createApp(App).use(vaporInteropPlugin).mount(root)
  1360. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1361. foo.value = 'bar'
  1362. await nextTick()
  1363. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1364. show.value = false
  1365. await nextTick()
  1366. expect(root.innerHTML).toBe('<div>vapor fallback</div><!--slot-->')
  1367. show.value = true
  1368. await nextTick()
  1369. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1370. })
  1371. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vdom slot', async () => {
  1372. const foo = ref('foo')
  1373. const show = ref(true)
  1374. const VdomSlot = createVdomSlot()
  1375. const VaporForwardedSlot1 = createMultipleVaporForwardedSlots(
  1376. VdomSlot,
  1377. 2,
  1378. )
  1379. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1380. const root = document.createElement('div')
  1381. createApp(App).use(vaporInteropPlugin).mount(root)
  1382. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1383. foo.value = 'bar'
  1384. await nextTick()
  1385. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1386. show.value = false
  1387. await nextTick()
  1388. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1389. show.value = true
  1390. await nextTick()
  1391. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1392. })
  1393. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot > vdom slot', async () => {
  1394. const foo = ref('foo')
  1395. const show = ref(true)
  1396. const VdomSlot = createVdomSlot()
  1397. const VaporForwardedSlot2 = createVaporForwardedSlot(VdomSlot)
  1398. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1399. VaporForwardedSlot2,
  1400. 'vapor1 fallback',
  1401. )
  1402. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1403. const root = document.createElement('div')
  1404. createApp(App).use(vaporInteropPlugin).mount(root)
  1405. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1406. foo.value = 'bar'
  1407. await nextTick()
  1408. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1409. show.value = false
  1410. await nextTick()
  1411. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1412. show.value = true
  1413. await nextTick()
  1414. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1415. })
  1416. test('vdom slot > vapor forwarded slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1417. const foo = ref('foo')
  1418. const show = ref(true)
  1419. const VdomSlot = createVdomSlot()
  1420. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1421. VdomSlot,
  1422. 'vapor2 fallback',
  1423. )
  1424. const VaporForwardedSlot1 = createVaporForwardedSlot(
  1425. VaporForwardedSlot2WithFallback,
  1426. )
  1427. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1428. const root = document.createElement('div')
  1429. createApp(App).use(vaporInteropPlugin).mount(root)
  1430. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1431. foo.value = 'bar'
  1432. await nextTick()
  1433. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1434. show.value = false
  1435. await nextTick()
  1436. expect(root.innerHTML).toBe('<div>vapor2 fallback</div><!--slot-->')
  1437. show.value = true
  1438. await nextTick()
  1439. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1440. })
  1441. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1442. const foo = ref('foo')
  1443. const show = ref(true)
  1444. const VaporSlot = createVaporSlot()
  1445. const VaporForwardedSlot2 = createVaporForwardedSlot(VaporSlot)
  1446. const VaporForwardedSlot1 =
  1447. createVaporForwardedSlot(VaporForwardedSlot2)
  1448. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1449. const root = document.createElement('div')
  1450. createApp(App).use(vaporInteropPlugin).mount(root)
  1451. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1452. foo.value = 'bar'
  1453. await nextTick()
  1454. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1455. show.value = false
  1456. await nextTick()
  1457. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1458. show.value = true
  1459. await nextTick()
  1460. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1461. })
  1462. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1463. const foo = ref('foo')
  1464. const show = ref(true)
  1465. const VdomSlot = createVdomSlot()
  1466. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1467. VdomSlot,
  1468. 'vapor2 fallback',
  1469. )
  1470. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1471. VaporForwardedSlot2WithFallback,
  1472. 'vapor1 fallback',
  1473. )
  1474. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1475. const root = document.createElement('div')
  1476. createApp(App).use(vaporInteropPlugin).mount(root)
  1477. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1478. foo.value = 'bar'
  1479. await nextTick()
  1480. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1481. show.value = false
  1482. await nextTick()
  1483. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1484. show.value = true
  1485. await nextTick()
  1486. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1487. })
  1488. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vapor slot', async () => {
  1489. const foo = ref('foo')
  1490. const show = ref(true)
  1491. const VaporSlot = createVaporSlot()
  1492. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1493. VaporSlot,
  1494. 'vapor2 fallback',
  1495. )
  1496. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1497. VaporForwardedSlot2WithFallback,
  1498. 'vapor1 fallback',
  1499. )
  1500. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1501. const root = document.createElement('div')
  1502. createApp(App).use(vaporInteropPlugin).mount(root)
  1503. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1504. foo.value = 'bar'
  1505. await nextTick()
  1506. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1507. show.value = false
  1508. await nextTick()
  1509. expect(root.innerHTML).toBe(
  1510. '<div>vapor1 fallback</div><!--slot--><!--slot-->',
  1511. )
  1512. show.value = true
  1513. await nextTick()
  1514. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1515. })
  1516. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1517. const foo = ref('foo')
  1518. const show = ref(true)
  1519. const VaporSlot = createVaporSlot()
  1520. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1521. VaporSlot,
  1522. 'vdom2 fallback',
  1523. )
  1524. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1525. VdomForwardedSlot2WithFallback,
  1526. 'vdom1 fallback',
  1527. )
  1528. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1529. const root = document.createElement('div')
  1530. createApp(App).use(vaporInteropPlugin).mount(root)
  1531. expect(root.innerHTML).toBe('<span>foo</span>')
  1532. foo.value = 'bar'
  1533. await nextTick()
  1534. expect(root.innerHTML).toBe('<span>bar</span>')
  1535. show.value = false
  1536. await nextTick()
  1537. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1538. show.value = true
  1539. await nextTick()
  1540. expect(root.innerHTML).toBe('<span>bar</span>')
  1541. })
  1542. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1543. const foo = ref('foo')
  1544. const show = ref(true)
  1545. const VdomSlot = createVdomSlot()
  1546. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1547. VdomSlot,
  1548. 'vdom2 fallback',
  1549. )
  1550. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1551. VdomForwardedSlot2WithFallback,
  1552. 'vdom1 fallback',
  1553. )
  1554. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1555. const root = document.createElement('div')
  1556. createApp(App).use(vaporInteropPlugin).mount(root)
  1557. expect(root.innerHTML).toBe('<span>foo</span>')
  1558. foo.value = 'bar'
  1559. await nextTick()
  1560. expect(root.innerHTML).toBe('<span>bar</span>')
  1561. show.value = false
  1562. await nextTick()
  1563. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1564. show.value = true
  1565. await nextTick()
  1566. expect(root.innerHTML).toBe('<span>bar</span>')
  1567. })
  1568. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) (multiple) > vapor slot', async () => {
  1569. const foo = ref('foo')
  1570. const show = ref(true)
  1571. const VaporSlot = createVaporSlot()
  1572. const VdomForwardedSlot3WithFallback = createVdomForwardedSlot(
  1573. VaporSlot,
  1574. 'vdom3 fallback',
  1575. )
  1576. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1577. VdomForwardedSlot3WithFallback,
  1578. 'vdom2 fallback',
  1579. )
  1580. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1581. VdomForwardedSlot2WithFallback,
  1582. 'vdom1 fallback',
  1583. )
  1584. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1585. const root = document.createElement('div')
  1586. createApp(App).use(vaporInteropPlugin).mount(root)
  1587. expect(root.innerHTML).toBe('<span>foo</span>')
  1588. foo.value = 'bar'
  1589. await nextTick()
  1590. expect(root.innerHTML).toBe('<span>bar</span>')
  1591. show.value = false
  1592. await nextTick()
  1593. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1594. show.value = true
  1595. await nextTick()
  1596. expect(root.innerHTML).toBe('<span>bar</span>')
  1597. })
  1598. })
  1599. })
  1600. })