componentSlots.spec.ts 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869
  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. forwardedSlotCreator,
  12. insert,
  13. prepend,
  14. renderEffect,
  15. setInsertionState,
  16. template,
  17. vaporInteropPlugin,
  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: () => 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: _props => ((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: _props => ((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: (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: () => template('header')(),
  279. footer: () => 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. ? { name: 'one', fn: () => template('one content')() }
  319. : { name: 'two', fn: () => template('two content')() },
  320. ],
  321. })
  322. }).render()
  323. expect(host.innerHTML).toBe(
  324. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  325. )
  326. flag1.value = false
  327. await nextTick()
  328. expect(host.innerHTML).toBe(
  329. '<p>one fallback<!--slot--></p><p>two content<!--slot--></p>',
  330. )
  331. flag1.value = true
  332. await nextTick()
  333. expect(host.innerHTML).toBe(
  334. '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
  335. )
  336. })
  337. test('dynamic slot outlet should be updated correctly', async () => {
  338. const slotOutletName = ref('one')
  339. const Child = defineVaporComponent(() => {
  340. const temp0 = template('<p>')
  341. const el0 = temp0()
  342. const slot1 = createSlot(
  343. () => slotOutletName.value,
  344. undefined,
  345. () => template('fallback')(),
  346. )
  347. insert(slot1, el0 as any as ParentNode)
  348. return el0
  349. })
  350. const { host } = define(() => {
  351. return createComponent(
  352. Child,
  353. {},
  354. {
  355. one: () => template('one content')(),
  356. two: () => template('two content')(),
  357. },
  358. )
  359. }).render()
  360. expect(host.innerHTML).toBe('<p>one content<!--slot--></p>')
  361. slotOutletName.value = 'two'
  362. await nextTick()
  363. expect(host.innerHTML).toBe('<p>two content<!--slot--></p>')
  364. slotOutletName.value = 'none'
  365. await nextTick()
  366. expect(host.innerHTML).toBe('<p>fallback<!--slot--></p>')
  367. })
  368. test('non-exist slot', async () => {
  369. const Child = defineVaporComponent(() => {
  370. const el0 = template('<p>')()
  371. const slot = createSlot('not-exist', undefined)
  372. insert(slot, el0 as any as ParentNode)
  373. return el0
  374. })
  375. const { host } = define(() => {
  376. return createComponent(Child)
  377. }).render()
  378. expect(host.innerHTML).toBe('<p><!--slot--></p>')
  379. })
  380. test('use fallback when inner content changes', async () => {
  381. const Child = {
  382. setup() {
  383. return createSlot('default', null, () =>
  384. document.createTextNode('fallback'),
  385. )
  386. },
  387. }
  388. const toggle = ref(true)
  389. const { html } = define({
  390. setup() {
  391. return createComponent(Child, null, {
  392. default: () => {
  393. return createIf(
  394. () => toggle.value,
  395. () => {
  396. return document.createTextNode('content')
  397. },
  398. )
  399. },
  400. })
  401. },
  402. }).render()
  403. expect(html()).toBe('content<!--if--><!--slot-->')
  404. toggle.value = false
  405. await nextTick()
  406. expect(html()).toBe('fallback<!--if--><!--slot-->')
  407. toggle.value = true
  408. await nextTick()
  409. expect(html()).toBe('content<!--if--><!--slot-->')
  410. })
  411. test('use fallback on initial render', async () => {
  412. const Child = {
  413. setup() {
  414. return createSlot('default', null, () =>
  415. document.createTextNode('fallback'),
  416. )
  417. },
  418. }
  419. const toggle = ref(false)
  420. const { html } = define({
  421. setup() {
  422. return createComponent(Child, null, {
  423. default: () => {
  424. return createIf(
  425. () => toggle.value,
  426. () => {
  427. return document.createTextNode('content')
  428. },
  429. )
  430. },
  431. })
  432. },
  433. }).render()
  434. expect(html()).toBe('fallback<!--if--><!--slot-->')
  435. toggle.value = true
  436. await nextTick()
  437. expect(html()).toBe('content<!--if--><!--slot-->')
  438. toggle.value = false
  439. await nextTick()
  440. expect(html()).toBe('fallback<!--if--><!--slot-->')
  441. })
  442. test('dynamic slot work with v-if', async () => {
  443. const val = ref('header')
  444. const toggle = ref(false)
  445. const Comp = defineVaporComponent(() => {
  446. const n0 = template('<div></div>')()
  447. prepend(n0 as any as ParentNode, createSlot('header', null))
  448. return n0
  449. })
  450. const { host } = define(() => {
  451. // dynamic slot
  452. return createComponent(Comp, null, {
  453. $: [
  454. () =>
  455. (toggle.value
  456. ? {
  457. name: val.value,
  458. fn: () => {
  459. return template('<h1></h1>')()
  460. },
  461. }
  462. : void 0) as DynamicSlot,
  463. ],
  464. })
  465. }).render()
  466. expect(host.innerHTML).toBe('<div><!--slot--></div>')
  467. toggle.value = true
  468. await nextTick()
  469. expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
  470. })
  471. test('render fallback when slot content is not valid', async () => {
  472. const Child = {
  473. setup() {
  474. return createSlot('default', null, () =>
  475. document.createTextNode('fallback'),
  476. )
  477. },
  478. }
  479. const { html } = define({
  480. setup() {
  481. return createComponent(Child, null, {
  482. default: () => {
  483. return template('<!--comment-->')()
  484. },
  485. })
  486. },
  487. }).render()
  488. expect(html()).toBe('fallback<!--slot-->')
  489. })
  490. test('render fallback when v-if condition is false', async () => {
  491. const Child = {
  492. setup() {
  493. return createSlot('default', null, () =>
  494. document.createTextNode('fallback'),
  495. )
  496. },
  497. }
  498. const toggle = ref(false)
  499. const { html } = define({
  500. setup() {
  501. return createComponent(Child, null, {
  502. default: () => {
  503. return createIf(
  504. () => toggle.value,
  505. () => {
  506. return document.createTextNode('content')
  507. },
  508. )
  509. },
  510. })
  511. },
  512. }).render()
  513. expect(html()).toBe('fallback<!--if--><!--slot-->')
  514. toggle.value = true
  515. await nextTick()
  516. expect(html()).toBe('content<!--if--><!--slot-->')
  517. toggle.value = false
  518. await nextTick()
  519. expect(html()).toBe('fallback<!--if--><!--slot-->')
  520. })
  521. test('render fallback with nested v-if', async () => {
  522. const Child = {
  523. setup() {
  524. return createSlot('default', null, () =>
  525. document.createTextNode('fallback'),
  526. )
  527. },
  528. }
  529. const outerShow = ref(false)
  530. const innerShow = ref(false)
  531. const { html } = define({
  532. setup() {
  533. return createComponent(Child, null, {
  534. default: () => {
  535. return createIf(
  536. () => outerShow.value,
  537. () => {
  538. return createIf(
  539. () => innerShow.value,
  540. () => {
  541. return document.createTextNode('content')
  542. },
  543. )
  544. },
  545. )
  546. },
  547. })
  548. },
  549. }).render()
  550. expect(html()).toBe('fallback<!--if--><!--slot-->')
  551. outerShow.value = true
  552. await nextTick()
  553. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  554. innerShow.value = true
  555. await nextTick()
  556. expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
  557. innerShow.value = false
  558. await nextTick()
  559. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  560. outerShow.value = false
  561. await nextTick()
  562. expect(html()).toBe('fallback<!--if--><!--slot-->')
  563. outerShow.value = true
  564. await nextTick()
  565. expect(html()).toBe('fallback<!--if--><!--if--><!--slot-->')
  566. innerShow.value = true
  567. await nextTick()
  568. expect(html()).toBe('content<!--if--><!--if--><!--slot-->')
  569. })
  570. test('render fallback with v-for', async () => {
  571. const Child = {
  572. setup() {
  573. return createSlot('default', null, () =>
  574. document.createTextNode('fallback'),
  575. )
  576. },
  577. }
  578. const items = ref<number[]>([1])
  579. const { html } = define({
  580. setup() {
  581. return createComponent(Child, null, {
  582. default: () => {
  583. const n2 = createFor(
  584. () => items.value,
  585. for_item0 => {
  586. const n4 = template('<span> </span>')() as any
  587. const x4 = child(n4) as any
  588. renderEffect(() =>
  589. setText(x4, toDisplayString(for_item0.value)),
  590. )
  591. return n4
  592. },
  593. )
  594. return n2
  595. },
  596. })
  597. },
  598. }).render()
  599. expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
  600. items.value.pop()
  601. await nextTick()
  602. expect(html()).toBe('fallback<!--for--><!--slot-->')
  603. items.value.pop()
  604. await nextTick()
  605. expect(html()).toBe('fallback<!--for--><!--slot-->')
  606. items.value.push(2)
  607. await nextTick()
  608. expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
  609. })
  610. test('render fallback with v-for (empty source)', async () => {
  611. const Child = {
  612. setup() {
  613. return createSlot('default', null, () =>
  614. document.createTextNode('fallback'),
  615. )
  616. },
  617. }
  618. const items = ref<number[]>([])
  619. const { html } = define({
  620. setup() {
  621. return createComponent(Child, null, {
  622. default: () => {
  623. const n2 = createFor(
  624. () => items.value,
  625. for_item0 => {
  626. const n4 = template('<span> </span>')() as any
  627. const x4 = child(n4) as any
  628. renderEffect(() =>
  629. setText(x4, toDisplayString(for_item0.value)),
  630. )
  631. return n4
  632. },
  633. )
  634. return n2
  635. },
  636. })
  637. },
  638. }).render()
  639. expect(html()).toBe('fallback<!--for--><!--slot-->')
  640. items.value.push(1)
  641. await nextTick()
  642. expect(html()).toBe('<span>1</span><!--for--><!--slot-->')
  643. items.value.pop()
  644. await nextTick()
  645. expect(html()).toBe('fallback<!--for--><!--slot-->')
  646. items.value.pop()
  647. await nextTick()
  648. expect(html()).toBe('fallback<!--for--><!--slot-->')
  649. items.value.push(2)
  650. await nextTick()
  651. expect(html()).toBe('<span>2</span><!--for--><!--slot-->')
  652. })
  653. })
  654. describe('forwarded slot', () => {
  655. test('should work', async () => {
  656. const Child = defineVaporComponent({
  657. setup() {
  658. return createSlot('foo', null)
  659. },
  660. })
  661. const Parent = defineVaporComponent({
  662. setup() {
  663. const createForwardedSlot = forwardedSlotCreator()
  664. const n2 = createComponent(
  665. Child,
  666. null,
  667. {
  668. foo: () => {
  669. return createForwardedSlot('foo', null)
  670. },
  671. },
  672. true,
  673. )
  674. return n2
  675. },
  676. })
  677. const foo = ref('foo')
  678. const { host } = define({
  679. setup() {
  680. const n2 = createComponent(
  681. Parent,
  682. null,
  683. {
  684. foo: () => {
  685. const n0 = template(' ')() as any
  686. renderEffect(() => setText(n0, foo.value))
  687. return n0
  688. },
  689. },
  690. true,
  691. )
  692. return n2
  693. },
  694. }).render()
  695. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->')
  696. foo.value = 'bar'
  697. await nextTick()
  698. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->')
  699. })
  700. test('mixed with non-forwarded slot', async () => {
  701. const Child = defineVaporComponent({
  702. setup() {
  703. return [createSlot('foo', null)]
  704. },
  705. })
  706. const Parent = defineVaporComponent({
  707. setup() {
  708. const createForwardedSlot = forwardedSlotCreator()
  709. const n2 = createComponent(Child, null, {
  710. foo: () => {
  711. const n0 = createForwardedSlot('foo', null)
  712. return n0
  713. },
  714. })
  715. const n3 = createSlot('default', null)
  716. return [n2, n3]
  717. },
  718. })
  719. const foo = ref('foo')
  720. const { host } = define({
  721. setup() {
  722. const n2 = createComponent(
  723. Parent,
  724. null,
  725. {
  726. foo: () => {
  727. const n0 = template(' ')() as any
  728. renderEffect(() => setText(n0, foo.value))
  729. return n0
  730. },
  731. default: () => {
  732. const n3 = template(' ')() as any
  733. renderEffect(() => setText(n3, foo.value))
  734. return n3
  735. },
  736. },
  737. true,
  738. )
  739. return n2
  740. },
  741. }).render()
  742. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->foo<!--slot-->')
  743. foo.value = 'bar'
  744. await nextTick()
  745. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->bar<!--slot-->')
  746. })
  747. test('forwarded slot with fallback', async () => {
  748. const Child = defineVaporComponent({
  749. setup() {
  750. return createSlot('default', null, () => template('child fallback')())
  751. },
  752. })
  753. const Parent = defineVaporComponent({
  754. setup() {
  755. const createForwardedSlot = forwardedSlotCreator()
  756. const n2 = createComponent(Child, null, {
  757. default: () => {
  758. const n0 = createForwardedSlot('default', null, () => {
  759. return template('<!-- <div></div> -->')()
  760. })
  761. return n0
  762. },
  763. })
  764. return n2
  765. },
  766. })
  767. const { html } = define({
  768. setup() {
  769. return createComponent(Parent, null, {
  770. default: () => template('<!-- <div>App</div> -->')(),
  771. })
  772. },
  773. }).render()
  774. expect(html()).toBe('child fallback<!--slot--><!--slot-->')
  775. })
  776. test('forwarded slot with fallback (v-if)', async () => {
  777. const Child = defineVaporComponent({
  778. setup() {
  779. return createSlot('default', null, () => template('child fallback')())
  780. },
  781. })
  782. const show = ref(false)
  783. const Parent = defineVaporComponent({
  784. setup() {
  785. const createForwardedSlot = forwardedSlotCreator()
  786. const n2 = createComponent(Child, null, {
  787. default: () => {
  788. const n0 = createForwardedSlot('default', null, () => {
  789. const n2 = createIf(
  790. () => show.value,
  791. () => {
  792. const n4 = template('<div>if content</div>')()
  793. return n4
  794. },
  795. )
  796. return n2
  797. })
  798. return n0
  799. },
  800. })
  801. return n2
  802. },
  803. })
  804. const { html } = define({
  805. setup() {
  806. return createComponent(Parent, null, {
  807. default: () => template('<!-- <div>App</div> -->')(),
  808. })
  809. },
  810. }).render()
  811. expect(html()).toBe('child fallback<!--if--><!--slot--><!--slot-->')
  812. show.value = true
  813. await nextTick()
  814. expect(html()).toBe(
  815. '<div>if content</div><!--if--><!--slot--><!--slot-->',
  816. )
  817. })
  818. test('forwarded slot with fallback (v-for)', async () => {
  819. const Child = defineVaporComponent({
  820. setup() {
  821. return createSlot('default', null, () => template('child fallback')())
  822. },
  823. })
  824. const items = ref<number[]>([])
  825. const Parent = defineVaporComponent({
  826. setup() {
  827. const createForwardedSlot = forwardedSlotCreator()
  828. const n2 = createComponent(Child, null, {
  829. default: () => {
  830. const n0 = createForwardedSlot('default', null, () => {
  831. const n2 = createFor(
  832. () => items.value,
  833. for_item0 => {
  834. const n4 = template('<span> </span>')() as any
  835. const x4 = child(n4) as any
  836. renderEffect(() =>
  837. setText(x4, toDisplayString(for_item0.value)),
  838. )
  839. return n4
  840. },
  841. )
  842. return n2
  843. })
  844. return n0
  845. },
  846. })
  847. return n2
  848. },
  849. })
  850. const { html } = define({
  851. setup() {
  852. return createComponent(Parent, null, {
  853. default: () => template('<!-- <div>App</div> -->')(),
  854. })
  855. },
  856. }).render()
  857. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  858. items.value.push(1)
  859. await nextTick()
  860. expect(html()).toBe('<span>1</span><!--for--><!--slot--><!--slot-->')
  861. items.value.pop()
  862. await nextTick()
  863. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  864. })
  865. describe('vdom interop', () => {
  866. const createVaporSlot = (fallbackText = 'fallback') => {
  867. return defineVaporComponent({
  868. setup() {
  869. const n0 = createSlot('foo', null, () => {
  870. const n2 = template(`<div>${fallbackText}</div>`)()
  871. return n2
  872. })
  873. return n0
  874. },
  875. })
  876. }
  877. const createVdomSlot = (fallbackText = 'fallback') => {
  878. return {
  879. render(this: any) {
  880. return renderSlot(this.$slots, 'foo', {}, () => [
  881. h('div', fallbackText),
  882. ])
  883. },
  884. }
  885. }
  886. const createVaporForwardedSlot = (
  887. targetComponent: any,
  888. fallbackText?: string,
  889. ) => {
  890. return defineVaporComponent({
  891. setup() {
  892. const createForwardedSlot = forwardedSlotCreator()
  893. const n2 = createComponent(
  894. targetComponent,
  895. null,
  896. {
  897. foo: () => {
  898. return fallbackText
  899. ? createForwardedSlot('foo', null, () => {
  900. const n2 = template(`<div>${fallbackText}</div>`)()
  901. return n2
  902. })
  903. : createForwardedSlot('foo', null)
  904. },
  905. },
  906. true,
  907. )
  908. return n2
  909. },
  910. })
  911. }
  912. const createVdomForwardedSlot = (
  913. targetComponent: any,
  914. fallbackText?: string,
  915. ) => {
  916. return {
  917. render(this: any) {
  918. return h(targetComponent, null, {
  919. foo: () => [
  920. fallbackText
  921. ? renderSlot(this.$slots, 'foo', {}, () => [
  922. h('div', fallbackText),
  923. ])
  924. : renderSlot(this.$slots, 'foo'),
  925. ],
  926. _: 3 /* FORWARDED */,
  927. })
  928. },
  929. }
  930. }
  931. const createMultipleVaporForwardedSlots = (
  932. targetComponent: any,
  933. count: number,
  934. ) => {
  935. let current = targetComponent
  936. for (let i = 0; i < count; i++) {
  937. current = createVaporForwardedSlot(current)
  938. }
  939. return current
  940. }
  941. const createMultipleVdomForwardedSlots = (
  942. targetComponent: any,
  943. count: number,
  944. ) => {
  945. let current = targetComponent
  946. for (let i = 0; i < count; i++) {
  947. current = createVdomForwardedSlot(current)
  948. }
  949. return current
  950. }
  951. const createTestApp = (
  952. rootComponent: any,
  953. foo: Ref<string>,
  954. show: Ref<Boolean>,
  955. ) => {
  956. return {
  957. setup() {
  958. return () =>
  959. h(
  960. rootComponent,
  961. null,
  962. createSlots({ _: 2 /* DYNAMIC */ } as any, [
  963. show.value
  964. ? {
  965. name: 'foo',
  966. fn: () => [h('span', foo.value)],
  967. key: '0',
  968. }
  969. : undefined,
  970. ]),
  971. )
  972. },
  973. }
  974. }
  975. const createEmptyTestApp = (rootComponent: any) => {
  976. return {
  977. setup() {
  978. return () => h(rootComponent)
  979. },
  980. }
  981. }
  982. test('vdom slot > vapor forwarded slot > vapor slot', async () => {
  983. const foo = ref('foo')
  984. const show = ref(true)
  985. const VaporSlot = createVaporSlot()
  986. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  987. const App = createTestApp(VaporForwardedSlot, foo, show)
  988. const root = document.createElement('div')
  989. createApp(App).use(vaporInteropPlugin).mount(root)
  990. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  991. foo.value = 'bar'
  992. await nextTick()
  993. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  994. show.value = false
  995. await nextTick()
  996. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  997. })
  998. test('vdom slot > vapor forwarded slot(with fallback) > vapor slot', async () => {
  999. const foo = ref('foo')
  1000. const show = ref(true)
  1001. const VaporSlot = createVaporSlot()
  1002. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1003. VaporSlot,
  1004. 'forwarded fallback',
  1005. )
  1006. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1007. const root = document.createElement('div')
  1008. createApp(App).use(vaporInteropPlugin).mount(root)
  1009. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1010. foo.value = 'bar'
  1011. await nextTick()
  1012. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1013. show.value = false
  1014. await nextTick()
  1015. expect(root.innerHTML).toBe('<div>forwarded fallback</div><!--slot-->')
  1016. })
  1017. test('vdom slot > vapor forwarded slot > vdom slot', async () => {
  1018. const foo = ref('foo')
  1019. const show = ref(true)
  1020. const VdomSlot = createVdomSlot()
  1021. const VaporForwardedSlot = createVaporForwardedSlot(VdomSlot)
  1022. const App = createTestApp(VaporForwardedSlot, foo, show)
  1023. const root = document.createElement('div')
  1024. createApp(App).use(vaporInteropPlugin).mount(root)
  1025. expect(root.innerHTML).toBe('<span>foo</span>')
  1026. foo.value = 'bar'
  1027. await nextTick()
  1028. expect(root.innerHTML).toBe('<span>bar</span>')
  1029. show.value = false
  1030. await nextTick()
  1031. expect(root.innerHTML).toBe('<div>fallback</div>')
  1032. show.value = true
  1033. await nextTick()
  1034. expect(root.innerHTML).toBe('<span>bar</span>')
  1035. })
  1036. test('vdom slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1037. const foo = ref('foo')
  1038. const show = ref(true)
  1039. const VdomSlot = createVdomSlot()
  1040. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1041. VdomSlot,
  1042. 'forwarded fallback',
  1043. )
  1044. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1045. const root = document.createElement('div')
  1046. createApp(App).use(vaporInteropPlugin).mount(root)
  1047. expect(root.innerHTML).toBe('<span>foo</span>')
  1048. foo.value = 'bar'
  1049. await nextTick()
  1050. expect(root.innerHTML).toBe('<span>bar</span>')
  1051. show.value = false
  1052. await nextTick()
  1053. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1054. })
  1055. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vapor slot', async () => {
  1056. const foo = ref('foo')
  1057. const show = ref(true)
  1058. const VaporSlot = createVaporSlot()
  1059. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1060. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1061. const App = createTestApp(VaporForwardedSlot, foo, show)
  1062. const root = document.createElement('div')
  1063. createApp(App).use(vaporInteropPlugin).mount(root)
  1064. expect(root.innerHTML).toBe('<span>foo</span>')
  1065. foo.value = 'bar'
  1066. await nextTick()
  1067. expect(root.innerHTML).toBe('<span>bar</span>')
  1068. show.value = false
  1069. await nextTick()
  1070. expect(root.innerHTML).toBe('<div>fallback</div>')
  1071. show.value = true
  1072. await nextTick()
  1073. expect(root.innerHTML).toBe('<span>bar</span>')
  1074. })
  1075. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vapor slot', async () => {
  1076. const foo = ref('foo')
  1077. const show = ref(true)
  1078. const VaporSlot = createVaporSlot()
  1079. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1080. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1081. VdomForwardedSlot,
  1082. 'forwarded fallback',
  1083. )
  1084. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1085. const root = document.createElement('div')
  1086. createApp(App).use(vaporInteropPlugin).mount(root)
  1087. expect(root.innerHTML).toBe('<span>foo</span>')
  1088. foo.value = 'bar'
  1089. await nextTick()
  1090. expect(root.innerHTML).toBe('<span>bar</span>')
  1091. show.value = false
  1092. await nextTick()
  1093. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1094. show.value = true
  1095. await nextTick()
  1096. expect(root.innerHTML).toBe('<span>bar</span>')
  1097. })
  1098. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1099. const foo = ref('foo')
  1100. const show = ref(true)
  1101. const VaporSlot = createVaporSlot()
  1102. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1103. VaporSlot,
  1104. 'vdom fallback',
  1105. )
  1106. const VaporForwardedSlot = createVaporForwardedSlot(
  1107. VdomForwardedSlotWithFallback,
  1108. )
  1109. const App = createTestApp(VaporForwardedSlot, foo, show)
  1110. const root = document.createElement('div')
  1111. createApp(App).use(vaporInteropPlugin).mount(root)
  1112. expect(root.innerHTML).toBe('<span>foo</span>')
  1113. foo.value = 'bar'
  1114. await nextTick()
  1115. expect(root.innerHTML).toBe('<span>bar</span>')
  1116. show.value = false
  1117. await nextTick()
  1118. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1119. show.value = true
  1120. await nextTick()
  1121. expect(root.innerHTML).toBe('<span>bar</span>')
  1122. })
  1123. test('vdom slot(empty) > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1124. const VaporSlot = createVaporSlot()
  1125. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1126. VaporSlot,
  1127. 'vdom fallback',
  1128. )
  1129. const VaporForwardedSlot = createVaporForwardedSlot(
  1130. VdomForwardedSlotWithFallback,
  1131. )
  1132. const App = createEmptyTestApp(VaporForwardedSlot)
  1133. const root = document.createElement('div')
  1134. createApp(App).use(vaporInteropPlugin).mount(root)
  1135. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1136. })
  1137. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vdom slot', async () => {
  1138. const foo = ref('foo')
  1139. const show = ref(true)
  1140. const VdomSlot = createVdomSlot()
  1141. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1142. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1143. const App = createTestApp(VaporForwardedSlot, foo, show)
  1144. const root = document.createElement('div')
  1145. createApp(App).use(vaporInteropPlugin).mount(root)
  1146. expect(root.innerHTML).toBe('<span>foo</span>')
  1147. foo.value = 'bar'
  1148. await nextTick()
  1149. expect(root.innerHTML).toBe('<span>bar</span>')
  1150. show.value = false
  1151. await nextTick()
  1152. expect(root.innerHTML).toBe('<div>fallback</div>')
  1153. show.value = true
  1154. await nextTick()
  1155. expect(root.innerHTML).toBe('<span>bar</span>')
  1156. })
  1157. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vdom slot', async () => {
  1158. const foo = ref('foo')
  1159. const show = ref(true)
  1160. const VdomSlot = createVdomSlot()
  1161. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1162. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1163. VdomForwardedSlot,
  1164. 'vapor fallback',
  1165. )
  1166. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1167. const root = document.createElement('div')
  1168. createApp(App).use(vaporInteropPlugin).mount(root)
  1169. expect(root.innerHTML).toBe('<span>foo</span>')
  1170. foo.value = 'bar'
  1171. await nextTick()
  1172. expect(root.innerHTML).toBe('<span>bar</span>')
  1173. show.value = false
  1174. await nextTick()
  1175. expect(root.innerHTML).toBe('<div>vapor fallback</div>')
  1176. show.value = true
  1177. await nextTick()
  1178. expect(root.innerHTML).toBe('<span>bar</span>')
  1179. })
  1180. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1181. const foo = ref('foo')
  1182. const show = ref(true)
  1183. const VdomSlot = createVdomSlot()
  1184. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1185. VdomSlot,
  1186. 'vdom fallback',
  1187. )
  1188. const VaporForwardedSlot = createVaporForwardedSlot(
  1189. VdomForwardedSlotWithFallback,
  1190. )
  1191. const App = createTestApp(VaporForwardedSlot, foo, show)
  1192. const root = document.createElement('div')
  1193. createApp(App).use(vaporInteropPlugin).mount(root)
  1194. expect(root.innerHTML).toBe('<span>foo</span>')
  1195. foo.value = 'bar'
  1196. await nextTick()
  1197. expect(root.innerHTML).toBe('<span>bar</span>')
  1198. show.value = false
  1199. await nextTick()
  1200. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1201. show.value = true
  1202. await nextTick()
  1203. expect(root.innerHTML).toBe('<span>bar</span>')
  1204. })
  1205. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot > vdom slot', async () => {
  1206. const foo = ref('foo')
  1207. const show = ref(true)
  1208. const VdomSlot = createVdomSlot()
  1209. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1210. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1211. VdomForwardedSlot,
  1212. 3,
  1213. )
  1214. const App = createTestApp(VaporForwardedSlot, foo, show)
  1215. const root = document.createElement('div')
  1216. createApp(App).use(vaporInteropPlugin).mount(root)
  1217. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1218. foo.value = 'bar'
  1219. await nextTick()
  1220. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1221. show.value = false
  1222. await nextTick()
  1223. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1224. show.value = true
  1225. await nextTick()
  1226. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1227. })
  1228. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1229. const foo = ref('foo')
  1230. const show = ref(true)
  1231. const VdomSlot = createVdomSlot()
  1232. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1233. VdomSlot,
  1234. 'vdom fallback',
  1235. )
  1236. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1237. VdomForwardedSlotWithFallback,
  1238. 3,
  1239. )
  1240. const App = createTestApp(VaporForwardedSlot, foo, show)
  1241. const root = document.createElement('div')
  1242. createApp(App).use(vaporInteropPlugin).mount(root)
  1243. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1244. foo.value = 'bar'
  1245. await nextTick()
  1246. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1247. show.value = false
  1248. await nextTick()
  1249. expect(root.innerHTML).toBe(
  1250. '<div>vdom fallback</div><!--slot--><!--slot-->',
  1251. )
  1252. show.value = true
  1253. await nextTick()
  1254. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1255. })
  1256. test('vdom slot > vdom forwarded slot > vapor slot', async () => {
  1257. const foo = ref('foo')
  1258. const show = ref(true)
  1259. const VaporSlot = createVaporSlot()
  1260. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1261. const App = createTestApp(VdomForwardedSlot, foo, show)
  1262. const root = document.createElement('div')
  1263. createApp(App).use(vaporInteropPlugin).mount(root)
  1264. expect(root.innerHTML).toBe('<span>foo</span>')
  1265. foo.value = 'bar'
  1266. await nextTick()
  1267. expect(root.innerHTML).toBe('<span>bar</span>')
  1268. show.value = false
  1269. await nextTick()
  1270. expect(root.innerHTML).toBe('<div>fallback</div>')
  1271. show.value = true
  1272. await nextTick()
  1273. expect(root.innerHTML).toBe('<span>bar</span>')
  1274. })
  1275. test('vdom slot > vdom forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1276. const foo = ref('foo')
  1277. const show = ref(true)
  1278. const VaporSlot = createVaporSlot()
  1279. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1280. const VdomForwardedSlot = createVdomForwardedSlot(VaporForwardedSlot)
  1281. const App = createTestApp(VdomForwardedSlot, foo, show)
  1282. const root = document.createElement('div')
  1283. createApp(App).use(vaporInteropPlugin).mount(root)
  1284. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1285. foo.value = 'bar'
  1286. await nextTick()
  1287. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1288. show.value = false
  1289. await nextTick()
  1290. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1291. show.value = true
  1292. await nextTick()
  1293. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1294. })
  1295. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot > vdom slot', async () => {
  1296. const foo = ref('foo')
  1297. const show = ref(true)
  1298. const VaporSlot = createVaporSlot()
  1299. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1300. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1301. VaporForwardedSlot,
  1302. 3,
  1303. )
  1304. const App = createTestApp(VdomForwardedSlot, foo, show)
  1305. const root = document.createElement('div')
  1306. createApp(App).use(vaporInteropPlugin).mount(root)
  1307. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1308. foo.value = 'bar'
  1309. await nextTick()
  1310. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1311. show.value = false
  1312. await nextTick()
  1313. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1314. show.value = true
  1315. await nextTick()
  1316. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1317. })
  1318. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1319. const foo = ref('foo')
  1320. const show = ref(true)
  1321. const VaporSlot = createVaporSlot()
  1322. const VaporForwardedSlot = createVaporForwardedSlot(
  1323. VaporSlot,
  1324. 'vapor fallback',
  1325. )
  1326. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1327. VaporForwardedSlot,
  1328. 3,
  1329. )
  1330. const App = createTestApp(VdomForwardedSlot, foo, show)
  1331. const root = document.createElement('div')
  1332. createApp(App).use(vaporInteropPlugin).mount(root)
  1333. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1334. foo.value = 'bar'
  1335. await nextTick()
  1336. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1337. show.value = false
  1338. await nextTick()
  1339. expect(root.innerHTML).toBe('<div>vapor fallback</div><!--slot-->')
  1340. show.value = true
  1341. await nextTick()
  1342. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1343. })
  1344. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vdom slot', async () => {
  1345. const foo = ref('foo')
  1346. const show = ref(true)
  1347. const VdomSlot = createVdomSlot()
  1348. const VaporForwardedSlot1 = createMultipleVaporForwardedSlots(
  1349. VdomSlot,
  1350. 2,
  1351. )
  1352. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1353. const root = document.createElement('div')
  1354. createApp(App).use(vaporInteropPlugin).mount(root)
  1355. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1356. foo.value = 'bar'
  1357. await nextTick()
  1358. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1359. show.value = false
  1360. await nextTick()
  1361. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1362. show.value = true
  1363. await nextTick()
  1364. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1365. })
  1366. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot > vdom slot', async () => {
  1367. const foo = ref('foo')
  1368. const show = ref(true)
  1369. const VdomSlot = createVdomSlot()
  1370. const VaporForwardedSlot2 = createVaporForwardedSlot(VdomSlot)
  1371. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1372. VaporForwardedSlot2,
  1373. 'vapor1 fallback',
  1374. )
  1375. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1376. const root = document.createElement('div')
  1377. createApp(App).use(vaporInteropPlugin).mount(root)
  1378. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1379. foo.value = 'bar'
  1380. await nextTick()
  1381. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1382. show.value = false
  1383. await nextTick()
  1384. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1385. show.value = true
  1386. await nextTick()
  1387. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1388. })
  1389. test('vdom slot > vapor forwarded slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1390. const foo = ref('foo')
  1391. const show = ref(true)
  1392. const VdomSlot = createVdomSlot()
  1393. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1394. VdomSlot,
  1395. 'vapor2 fallback',
  1396. )
  1397. const VaporForwardedSlot1 = createVaporForwardedSlot(
  1398. VaporForwardedSlot2WithFallback,
  1399. )
  1400. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1401. const root = document.createElement('div')
  1402. createApp(App).use(vaporInteropPlugin).mount(root)
  1403. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1404. foo.value = 'bar'
  1405. await nextTick()
  1406. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1407. show.value = false
  1408. await nextTick()
  1409. expect(root.innerHTML).toBe('<div>vapor2 fallback</div><!--slot-->')
  1410. show.value = true
  1411. await nextTick()
  1412. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1413. })
  1414. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1415. const foo = ref('foo')
  1416. const show = ref(true)
  1417. const VaporSlot = createVaporSlot()
  1418. const VaporForwardedSlot2 = createVaporForwardedSlot(VaporSlot)
  1419. const VaporForwardedSlot1 =
  1420. createVaporForwardedSlot(VaporForwardedSlot2)
  1421. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1422. const root = document.createElement('div')
  1423. createApp(App).use(vaporInteropPlugin).mount(root)
  1424. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1425. foo.value = 'bar'
  1426. await nextTick()
  1427. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1428. show.value = false
  1429. await nextTick()
  1430. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1431. show.value = true
  1432. await nextTick()
  1433. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1434. })
  1435. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1436. const foo = ref('foo')
  1437. const show = ref(true)
  1438. const VdomSlot = createVdomSlot()
  1439. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1440. VdomSlot,
  1441. 'vapor2 fallback',
  1442. )
  1443. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1444. VaporForwardedSlot2WithFallback,
  1445. 'vapor1 fallback',
  1446. )
  1447. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1448. const root = document.createElement('div')
  1449. createApp(App).use(vaporInteropPlugin).mount(root)
  1450. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1451. foo.value = 'bar'
  1452. await nextTick()
  1453. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1454. show.value = false
  1455. await nextTick()
  1456. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1457. show.value = true
  1458. await nextTick()
  1459. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1460. })
  1461. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vapor slot', async () => {
  1462. const foo = ref('foo')
  1463. const show = ref(true)
  1464. const VaporSlot = createVaporSlot()
  1465. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1466. VaporSlot,
  1467. 'vapor2 fallback',
  1468. )
  1469. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1470. VaporForwardedSlot2WithFallback,
  1471. 'vapor1 fallback',
  1472. )
  1473. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1474. const root = document.createElement('div')
  1475. createApp(App).use(vaporInteropPlugin).mount(root)
  1476. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1477. foo.value = 'bar'
  1478. await nextTick()
  1479. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1480. show.value = false
  1481. await nextTick()
  1482. expect(root.innerHTML).toBe(
  1483. '<div>vapor1 fallback</div><!--slot--><!--slot-->',
  1484. )
  1485. show.value = true
  1486. await nextTick()
  1487. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1488. })
  1489. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1490. const foo = ref('foo')
  1491. const show = ref(true)
  1492. const VaporSlot = createVaporSlot()
  1493. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1494. VaporSlot,
  1495. 'vdom2 fallback',
  1496. )
  1497. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1498. VdomForwardedSlot2WithFallback,
  1499. 'vdom1 fallback',
  1500. )
  1501. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1502. const root = document.createElement('div')
  1503. createApp(App).use(vaporInteropPlugin).mount(root)
  1504. expect(root.innerHTML).toBe('<span>foo</span>')
  1505. foo.value = 'bar'
  1506. await nextTick()
  1507. expect(root.innerHTML).toBe('<span>bar</span>')
  1508. show.value = false
  1509. await nextTick()
  1510. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1511. show.value = true
  1512. await nextTick()
  1513. expect(root.innerHTML).toBe('<span>bar</span>')
  1514. })
  1515. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1516. const foo = ref('foo')
  1517. const show = ref(true)
  1518. const VdomSlot = createVdomSlot()
  1519. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1520. VdomSlot,
  1521. 'vdom2 fallback',
  1522. )
  1523. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1524. VdomForwardedSlot2WithFallback,
  1525. 'vdom1 fallback',
  1526. )
  1527. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1528. const root = document.createElement('div')
  1529. createApp(App).use(vaporInteropPlugin).mount(root)
  1530. expect(root.innerHTML).toBe('<span>foo</span>')
  1531. foo.value = 'bar'
  1532. await nextTick()
  1533. expect(root.innerHTML).toBe('<span>bar</span>')
  1534. show.value = false
  1535. await nextTick()
  1536. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1537. show.value = true
  1538. await nextTick()
  1539. expect(root.innerHTML).toBe('<span>bar</span>')
  1540. })
  1541. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) (multiple) > vapor slot', async () => {
  1542. const foo = ref('foo')
  1543. const show = ref(true)
  1544. const VaporSlot = createVaporSlot()
  1545. const VdomForwardedSlot3WithFallback = createVdomForwardedSlot(
  1546. VaporSlot,
  1547. 'vdom3 fallback',
  1548. )
  1549. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1550. VdomForwardedSlot3WithFallback,
  1551. 'vdom2 fallback',
  1552. )
  1553. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1554. VdomForwardedSlot2WithFallback,
  1555. 'vdom1 fallback',
  1556. )
  1557. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1558. const root = document.createElement('div')
  1559. createApp(App).use(vaporInteropPlugin).mount(root)
  1560. expect(root.innerHTML).toBe('<span>foo</span>')
  1561. foo.value = 'bar'
  1562. await nextTick()
  1563. expect(root.innerHTML).toBe('<span>bar</span>')
  1564. show.value = false
  1565. await nextTick()
  1566. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1567. show.value = true
  1568. await nextTick()
  1569. expect(root.innerHTML).toBe('<span>bar</span>')
  1570. })
  1571. })
  1572. test('consecutive slots with insertion state', async () => {
  1573. const { component: Child } = define({
  1574. setup() {
  1575. const n2 = template('<div><div>baz</div></div>', true)() as any
  1576. setInsertionState(n2, 0)
  1577. createSlot('default', null)
  1578. setInsertionState(n2, 0)
  1579. createSlot('foo', null)
  1580. return n2
  1581. },
  1582. })
  1583. const { html } = define({
  1584. setup() {
  1585. return createComponent(Child, null, {
  1586. default: () => template('default')(),
  1587. foo: () => template('foo')(),
  1588. })
  1589. },
  1590. }).render()
  1591. expect(html()).toBe(
  1592. `<div>` +
  1593. `default<!--slot-->` +
  1594. `foo<!--slot-->` +
  1595. `<div>baz</div>` +
  1596. `</div>`,
  1597. )
  1598. })
  1599. })
  1600. describe('forwarded slot', () => {
  1601. test('should work', async () => {
  1602. const Child = defineVaporComponent({
  1603. setup() {
  1604. return createSlot('foo', null)
  1605. },
  1606. })
  1607. const Parent = defineVaporComponent({
  1608. setup() {
  1609. const createForwardedSlot = forwardedSlotCreator()
  1610. const n2 = createComponent(
  1611. Child,
  1612. null,
  1613. {
  1614. foo: () => {
  1615. return createForwardedSlot('foo', null)
  1616. },
  1617. },
  1618. true,
  1619. )
  1620. return n2
  1621. },
  1622. })
  1623. const foo = ref('foo')
  1624. const { host } = define({
  1625. setup() {
  1626. const n2 = createComponent(
  1627. Parent,
  1628. null,
  1629. {
  1630. foo: () => {
  1631. const n0 = template(' ')() as any
  1632. renderEffect(() => setText(n0, foo.value))
  1633. return n0
  1634. },
  1635. },
  1636. true,
  1637. )
  1638. return n2
  1639. },
  1640. }).render()
  1641. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->')
  1642. foo.value = 'bar'
  1643. await nextTick()
  1644. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->')
  1645. })
  1646. test('mixed with non-forwarded slot', async () => {
  1647. const Child = defineVaporComponent({
  1648. setup() {
  1649. return [createSlot('foo', null)]
  1650. },
  1651. })
  1652. const Parent = defineVaporComponent({
  1653. setup() {
  1654. const createForwardedSlot = forwardedSlotCreator()
  1655. const n2 = createComponent(Child, null, {
  1656. foo: () => {
  1657. const n0 = createForwardedSlot('foo', null)
  1658. return n0
  1659. },
  1660. })
  1661. const n3 = createSlot('default', null)
  1662. return [n2, n3]
  1663. },
  1664. })
  1665. const foo = ref('foo')
  1666. const { host } = define({
  1667. setup() {
  1668. const n2 = createComponent(
  1669. Parent,
  1670. null,
  1671. {
  1672. foo: () => {
  1673. const n0 = template(' ')() as any
  1674. renderEffect(() => setText(n0, foo.value))
  1675. return n0
  1676. },
  1677. default: () => {
  1678. const n3 = template(' ')() as any
  1679. renderEffect(() => setText(n3, foo.value))
  1680. return n3
  1681. },
  1682. },
  1683. true,
  1684. )
  1685. return n2
  1686. },
  1687. }).render()
  1688. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->foo<!--slot-->')
  1689. foo.value = 'bar'
  1690. await nextTick()
  1691. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->bar<!--slot-->')
  1692. })
  1693. describe('vdom interop', () => {
  1694. const createVaporSlot = (fallbackText = 'fallback') => {
  1695. return defineVaporComponent({
  1696. setup() {
  1697. const n0 = createSlot('foo', null, () => {
  1698. const n2 = template(`<div>${fallbackText}</div>`)()
  1699. return n2
  1700. })
  1701. return n0
  1702. },
  1703. })
  1704. }
  1705. const createVdomSlot = (fallbackText = 'fallback') => {
  1706. return {
  1707. render(this: any) {
  1708. return renderSlot(this.$slots, 'foo', {}, () => [
  1709. h('div', fallbackText),
  1710. ])
  1711. },
  1712. }
  1713. }
  1714. const createVaporForwardedSlot = (
  1715. targetComponent: any,
  1716. fallbackText?: string,
  1717. ) => {
  1718. return defineVaporComponent({
  1719. setup() {
  1720. const createForwardedSlot = forwardedSlotCreator()
  1721. const n2 = createComponent(
  1722. targetComponent,
  1723. null,
  1724. {
  1725. foo: () => {
  1726. return fallbackText
  1727. ? createForwardedSlot('foo', null, () => {
  1728. const n2 = template(`<div>${fallbackText}</div>`)()
  1729. return n2
  1730. })
  1731. : createForwardedSlot('foo', null)
  1732. },
  1733. },
  1734. true,
  1735. )
  1736. return n2
  1737. },
  1738. })
  1739. }
  1740. const createVdomForwardedSlot = (
  1741. targetComponent: any,
  1742. fallbackText?: string,
  1743. ) => {
  1744. return {
  1745. render(this: any) {
  1746. return h(targetComponent, null, {
  1747. foo: () => [
  1748. fallbackText
  1749. ? renderSlot(this.$slots, 'foo', {}, () => [
  1750. h('div', fallbackText),
  1751. ])
  1752. : renderSlot(this.$slots, 'foo'),
  1753. ],
  1754. _: 3 /* FORWARDED */,
  1755. })
  1756. },
  1757. }
  1758. }
  1759. const createMultipleVaporForwardedSlots = (
  1760. targetComponent: any,
  1761. count: number,
  1762. ) => {
  1763. let current = targetComponent
  1764. for (let i = 0; i < count; i++) {
  1765. current = createVaporForwardedSlot(current)
  1766. }
  1767. return current
  1768. }
  1769. const createMultipleVdomForwardedSlots = (
  1770. targetComponent: any,
  1771. count: number,
  1772. ) => {
  1773. let current = targetComponent
  1774. for (let i = 0; i < count; i++) {
  1775. current = createVdomForwardedSlot(current)
  1776. }
  1777. return current
  1778. }
  1779. const createTestApp = (
  1780. rootComponent: any,
  1781. foo: Ref<string>,
  1782. show: Ref<Boolean>,
  1783. ) => {
  1784. return {
  1785. setup() {
  1786. return () =>
  1787. h(
  1788. rootComponent,
  1789. null,
  1790. createSlots({ _: 2 /* DYNAMIC */ } as any, [
  1791. show.value
  1792. ? {
  1793. name: 'foo',
  1794. fn: () => [h('span', foo.value)],
  1795. key: '0',
  1796. }
  1797. : undefined,
  1798. ]),
  1799. )
  1800. },
  1801. }
  1802. }
  1803. const createEmptyTestApp = (rootComponent: any) => {
  1804. return {
  1805. setup() {
  1806. return () => h(rootComponent)
  1807. },
  1808. }
  1809. }
  1810. test('vdom slot > vapor forwarded slot > vapor slot', async () => {
  1811. const foo = ref('foo')
  1812. const show = ref(true)
  1813. const VaporSlot = createVaporSlot()
  1814. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1815. const App = createTestApp(VaporForwardedSlot, foo, show)
  1816. const root = document.createElement('div')
  1817. createApp(App).use(vaporInteropPlugin).mount(root)
  1818. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1819. foo.value = 'bar'
  1820. await nextTick()
  1821. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1822. show.value = false
  1823. await nextTick()
  1824. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1825. })
  1826. test('vdom slot > vapor forwarded slot(with fallback) > vapor slot', async () => {
  1827. const foo = ref('foo')
  1828. const show = ref(true)
  1829. const VaporSlot = createVaporSlot()
  1830. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1831. VaporSlot,
  1832. 'forwarded fallback',
  1833. )
  1834. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1835. const root = document.createElement('div')
  1836. createApp(App).use(vaporInteropPlugin).mount(root)
  1837. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1838. foo.value = 'bar'
  1839. await nextTick()
  1840. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1841. show.value = false
  1842. await nextTick()
  1843. expect(root.innerHTML).toBe('<div>forwarded fallback</div><!--slot-->')
  1844. })
  1845. test('vdom slot > vapor forwarded slot > vdom slot', async () => {
  1846. const foo = ref('foo')
  1847. const show = ref(true)
  1848. const VdomSlot = createVdomSlot()
  1849. const VaporForwardedSlot = createVaporForwardedSlot(VdomSlot)
  1850. const App = createTestApp(VaporForwardedSlot, foo, show)
  1851. const root = document.createElement('div')
  1852. createApp(App).use(vaporInteropPlugin).mount(root)
  1853. expect(root.innerHTML).toBe('<span>foo</span>')
  1854. foo.value = 'bar'
  1855. await nextTick()
  1856. expect(root.innerHTML).toBe('<span>bar</span>')
  1857. show.value = false
  1858. await nextTick()
  1859. expect(root.innerHTML).toBe('<div>fallback</div>')
  1860. show.value = true
  1861. await nextTick()
  1862. expect(root.innerHTML).toBe('<span>bar</span>')
  1863. })
  1864. test('vdom slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1865. const foo = ref('foo')
  1866. const show = ref(true)
  1867. const VdomSlot = createVdomSlot()
  1868. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1869. VdomSlot,
  1870. 'forwarded fallback',
  1871. )
  1872. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1873. const root = document.createElement('div')
  1874. createApp(App).use(vaporInteropPlugin).mount(root)
  1875. expect(root.innerHTML).toBe('<span>foo</span>')
  1876. foo.value = 'bar'
  1877. await nextTick()
  1878. expect(root.innerHTML).toBe('<span>bar</span>')
  1879. show.value = false
  1880. await nextTick()
  1881. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1882. })
  1883. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vapor slot', async () => {
  1884. const foo = ref('foo')
  1885. const show = ref(true)
  1886. const VaporSlot = createVaporSlot()
  1887. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1888. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1889. const App = createTestApp(VaporForwardedSlot, foo, show)
  1890. const root = document.createElement('div')
  1891. createApp(App).use(vaporInteropPlugin).mount(root)
  1892. expect(root.innerHTML).toBe('<span>foo</span>')
  1893. foo.value = 'bar'
  1894. await nextTick()
  1895. expect(root.innerHTML).toBe('<span>bar</span>')
  1896. show.value = false
  1897. await nextTick()
  1898. expect(root.innerHTML).toBe('<div>fallback</div>')
  1899. show.value = true
  1900. await nextTick()
  1901. expect(root.innerHTML).toBe('<span>bar</span>')
  1902. })
  1903. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vapor slot', async () => {
  1904. const foo = ref('foo')
  1905. const show = ref(true)
  1906. const VaporSlot = createVaporSlot()
  1907. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1908. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1909. VdomForwardedSlot,
  1910. 'forwarded fallback',
  1911. )
  1912. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1913. const root = document.createElement('div')
  1914. createApp(App).use(vaporInteropPlugin).mount(root)
  1915. expect(root.innerHTML).toBe('<span>foo</span>')
  1916. foo.value = 'bar'
  1917. await nextTick()
  1918. expect(root.innerHTML).toBe('<span>bar</span>')
  1919. show.value = false
  1920. await nextTick()
  1921. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1922. show.value = true
  1923. await nextTick()
  1924. expect(root.innerHTML).toBe('<span>bar</span>')
  1925. })
  1926. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1927. const foo = ref('foo')
  1928. const show = ref(true)
  1929. const VaporSlot = createVaporSlot()
  1930. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1931. VaporSlot,
  1932. 'vdom fallback',
  1933. )
  1934. const VaporForwardedSlot = createVaporForwardedSlot(
  1935. VdomForwardedSlotWithFallback,
  1936. )
  1937. const App = createTestApp(VaporForwardedSlot, foo, show)
  1938. const root = document.createElement('div')
  1939. createApp(App).use(vaporInteropPlugin).mount(root)
  1940. expect(root.innerHTML).toBe('<span>foo</span>')
  1941. foo.value = 'bar'
  1942. await nextTick()
  1943. expect(root.innerHTML).toBe('<span>bar</span>')
  1944. show.value = false
  1945. await nextTick()
  1946. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1947. show.value = true
  1948. await nextTick()
  1949. expect(root.innerHTML).toBe('<span>bar</span>')
  1950. })
  1951. test('vdom slot(empty) > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1952. const VaporSlot = createVaporSlot()
  1953. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1954. VaporSlot,
  1955. 'vdom fallback',
  1956. )
  1957. const VaporForwardedSlot = createVaporForwardedSlot(
  1958. VdomForwardedSlotWithFallback,
  1959. )
  1960. const App = createEmptyTestApp(VaporForwardedSlot)
  1961. const root = document.createElement('div')
  1962. createApp(App).use(vaporInteropPlugin).mount(root)
  1963. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1964. })
  1965. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vdom slot', async () => {
  1966. const foo = ref('foo')
  1967. const show = ref(true)
  1968. const VdomSlot = createVdomSlot()
  1969. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1970. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1971. const App = createTestApp(VaporForwardedSlot, foo, show)
  1972. const root = document.createElement('div')
  1973. createApp(App).use(vaporInteropPlugin).mount(root)
  1974. expect(root.innerHTML).toBe('<span>foo</span>')
  1975. foo.value = 'bar'
  1976. await nextTick()
  1977. expect(root.innerHTML).toBe('<span>bar</span>')
  1978. show.value = false
  1979. await nextTick()
  1980. expect(root.innerHTML).toBe('<div>fallback</div>')
  1981. show.value = true
  1982. await nextTick()
  1983. expect(root.innerHTML).toBe('<span>bar</span>')
  1984. })
  1985. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vdom slot', async () => {
  1986. const foo = ref('foo')
  1987. const show = ref(true)
  1988. const VdomSlot = createVdomSlot()
  1989. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1990. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1991. VdomForwardedSlot,
  1992. 'vapor fallback',
  1993. )
  1994. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1995. const root = document.createElement('div')
  1996. createApp(App).use(vaporInteropPlugin).mount(root)
  1997. expect(root.innerHTML).toBe('<span>foo</span>')
  1998. foo.value = 'bar'
  1999. await nextTick()
  2000. expect(root.innerHTML).toBe('<span>bar</span>')
  2001. show.value = false
  2002. await nextTick()
  2003. expect(root.innerHTML).toBe('<div>vapor fallback</div>')
  2004. show.value = true
  2005. await nextTick()
  2006. expect(root.innerHTML).toBe('<span>bar</span>')
  2007. })
  2008. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vdom slot', async () => {
  2009. const foo = ref('foo')
  2010. const show = ref(true)
  2011. const VdomSlot = createVdomSlot()
  2012. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  2013. VdomSlot,
  2014. 'vdom fallback',
  2015. )
  2016. const VaporForwardedSlot = createVaporForwardedSlot(
  2017. VdomForwardedSlotWithFallback,
  2018. )
  2019. const App = createTestApp(VaporForwardedSlot, foo, show)
  2020. const root = document.createElement('div')
  2021. createApp(App).use(vaporInteropPlugin).mount(root)
  2022. expect(root.innerHTML).toBe('<span>foo</span>')
  2023. foo.value = 'bar'
  2024. await nextTick()
  2025. expect(root.innerHTML).toBe('<span>bar</span>')
  2026. show.value = false
  2027. await nextTick()
  2028. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  2029. show.value = true
  2030. await nextTick()
  2031. expect(root.innerHTML).toBe('<span>bar</span>')
  2032. })
  2033. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot > vdom slot', async () => {
  2034. const foo = ref('foo')
  2035. const show = ref(true)
  2036. const VdomSlot = createVdomSlot()
  2037. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  2038. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  2039. VdomForwardedSlot,
  2040. 3,
  2041. )
  2042. const App = createTestApp(VaporForwardedSlot, foo, show)
  2043. const root = document.createElement('div')
  2044. createApp(App).use(vaporInteropPlugin).mount(root)
  2045. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  2046. foo.value = 'bar'
  2047. await nextTick()
  2048. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2049. show.value = false
  2050. await nextTick()
  2051. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  2052. show.value = true
  2053. await nextTick()
  2054. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2055. })
  2056. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  2057. const foo = ref('foo')
  2058. const show = ref(true)
  2059. const VdomSlot = createVdomSlot()
  2060. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  2061. VdomSlot,
  2062. 'vdom fallback',
  2063. )
  2064. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  2065. VdomForwardedSlotWithFallback,
  2066. 3,
  2067. )
  2068. const App = createTestApp(VaporForwardedSlot, foo, show)
  2069. const root = document.createElement('div')
  2070. createApp(App).use(vaporInteropPlugin).mount(root)
  2071. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  2072. foo.value = 'bar'
  2073. await nextTick()
  2074. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2075. show.value = false
  2076. await nextTick()
  2077. expect(root.innerHTML).toBe(
  2078. '<div>vdom fallback</div><!--slot--><!--slot-->',
  2079. )
  2080. show.value = true
  2081. await nextTick()
  2082. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2083. })
  2084. test('vdom slot > vdom forwarded slot > vapor slot', async () => {
  2085. const foo = ref('foo')
  2086. const show = ref(true)
  2087. const VaporSlot = createVaporSlot()
  2088. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  2089. const App = createTestApp(VdomForwardedSlot, foo, show)
  2090. const root = document.createElement('div')
  2091. createApp(App).use(vaporInteropPlugin).mount(root)
  2092. expect(root.innerHTML).toBe('<span>foo</span>')
  2093. foo.value = 'bar'
  2094. await nextTick()
  2095. expect(root.innerHTML).toBe('<span>bar</span>')
  2096. show.value = false
  2097. await nextTick()
  2098. expect(root.innerHTML).toBe('<div>fallback</div>')
  2099. show.value = true
  2100. await nextTick()
  2101. expect(root.innerHTML).toBe('<span>bar</span>')
  2102. })
  2103. test('vdom slot > vdom forwarded slot > vapor forwarded slot > vapor slot', async () => {
  2104. const foo = ref('foo')
  2105. const show = ref(true)
  2106. const VaporSlot = createVaporSlot()
  2107. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  2108. const VdomForwardedSlot = createVdomForwardedSlot(VaporForwardedSlot)
  2109. const App = createTestApp(VdomForwardedSlot, foo, show)
  2110. const root = document.createElement('div')
  2111. createApp(App).use(vaporInteropPlugin).mount(root)
  2112. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2113. foo.value = 'bar'
  2114. await nextTick()
  2115. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2116. show.value = false
  2117. await nextTick()
  2118. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  2119. show.value = true
  2120. await nextTick()
  2121. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2122. })
  2123. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot > vdom slot', async () => {
  2124. const foo = ref('foo')
  2125. const show = ref(true)
  2126. const VaporSlot = createVaporSlot()
  2127. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  2128. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  2129. VaporForwardedSlot,
  2130. 3,
  2131. )
  2132. const App = createTestApp(VdomForwardedSlot, foo, show)
  2133. const root = document.createElement('div')
  2134. createApp(App).use(vaporInteropPlugin).mount(root)
  2135. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2136. foo.value = 'bar'
  2137. await nextTick()
  2138. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2139. show.value = false
  2140. await nextTick()
  2141. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  2142. show.value = true
  2143. await nextTick()
  2144. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2145. })
  2146. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  2147. const foo = ref('foo')
  2148. const show = ref(true)
  2149. const VaporSlot = createVaporSlot()
  2150. const VaporForwardedSlot = createVaporForwardedSlot(
  2151. VaporSlot,
  2152. 'vapor fallback',
  2153. )
  2154. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  2155. VaporForwardedSlot,
  2156. 3,
  2157. )
  2158. const App = createTestApp(VdomForwardedSlot, foo, show)
  2159. const root = document.createElement('div')
  2160. createApp(App).use(vaporInteropPlugin).mount(root)
  2161. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2162. foo.value = 'bar'
  2163. await nextTick()
  2164. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2165. show.value = false
  2166. await nextTick()
  2167. expect(root.innerHTML).toBe('<div>vapor fallback</div><!--slot-->')
  2168. show.value = true
  2169. await nextTick()
  2170. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2171. })
  2172. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vdom slot', async () => {
  2173. const foo = ref('foo')
  2174. const show = ref(true)
  2175. const VdomSlot = createVdomSlot()
  2176. const VaporForwardedSlot1 = createMultipleVaporForwardedSlots(
  2177. VdomSlot,
  2178. 2,
  2179. )
  2180. const App = createTestApp(VaporForwardedSlot1, foo, show)
  2181. const root = document.createElement('div')
  2182. createApp(App).use(vaporInteropPlugin).mount(root)
  2183. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2184. foo.value = 'bar'
  2185. await nextTick()
  2186. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2187. show.value = false
  2188. await nextTick()
  2189. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  2190. show.value = true
  2191. await nextTick()
  2192. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2193. })
  2194. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot > vdom slot', async () => {
  2195. const foo = ref('foo')
  2196. const show = ref(true)
  2197. const VdomSlot = createVdomSlot()
  2198. const VaporForwardedSlot2 = createVaporForwardedSlot(VdomSlot)
  2199. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  2200. VaporForwardedSlot2,
  2201. 'vapor1 fallback',
  2202. )
  2203. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  2204. const root = document.createElement('div')
  2205. createApp(App).use(vaporInteropPlugin).mount(root)
  2206. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2207. foo.value = 'bar'
  2208. await nextTick()
  2209. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2210. show.value = false
  2211. await nextTick()
  2212. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  2213. show.value = true
  2214. await nextTick()
  2215. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2216. })
  2217. test('vdom slot > vapor forwarded slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  2218. const foo = ref('foo')
  2219. const show = ref(true)
  2220. const VdomSlot = createVdomSlot()
  2221. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  2222. VdomSlot,
  2223. 'vapor2 fallback',
  2224. )
  2225. const VaporForwardedSlot1 = createVaporForwardedSlot(
  2226. VaporForwardedSlot2WithFallback,
  2227. )
  2228. const App = createTestApp(VaporForwardedSlot1, foo, show)
  2229. const root = document.createElement('div')
  2230. createApp(App).use(vaporInteropPlugin).mount(root)
  2231. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2232. foo.value = 'bar'
  2233. await nextTick()
  2234. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2235. show.value = false
  2236. await nextTick()
  2237. expect(root.innerHTML).toBe('<div>vapor2 fallback</div><!--slot-->')
  2238. show.value = true
  2239. await nextTick()
  2240. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2241. })
  2242. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vapor slot', async () => {
  2243. const foo = ref('foo')
  2244. const show = ref(true)
  2245. const VaporSlot = createVaporSlot()
  2246. const VaporForwardedSlot2 = createVaporForwardedSlot(VaporSlot)
  2247. const VaporForwardedSlot1 =
  2248. createVaporForwardedSlot(VaporForwardedSlot2)
  2249. const App = createTestApp(VaporForwardedSlot1, foo, show)
  2250. const root = document.createElement('div')
  2251. createApp(App).use(vaporInteropPlugin).mount(root)
  2252. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  2253. foo.value = 'bar'
  2254. await nextTick()
  2255. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2256. show.value = false
  2257. await nextTick()
  2258. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  2259. show.value = true
  2260. await nextTick()
  2261. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2262. })
  2263. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  2264. const foo = ref('foo')
  2265. const show = ref(true)
  2266. const VdomSlot = createVdomSlot()
  2267. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  2268. VdomSlot,
  2269. 'vapor2 fallback',
  2270. )
  2271. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  2272. VaporForwardedSlot2WithFallback,
  2273. 'vapor1 fallback',
  2274. )
  2275. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  2276. const root = document.createElement('div')
  2277. createApp(App).use(vaporInteropPlugin).mount(root)
  2278. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  2279. foo.value = 'bar'
  2280. await nextTick()
  2281. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2282. show.value = false
  2283. await nextTick()
  2284. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  2285. show.value = true
  2286. await nextTick()
  2287. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  2288. })
  2289. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vapor slot', async () => {
  2290. const foo = ref('foo')
  2291. const show = ref(true)
  2292. const VaporSlot = createVaporSlot()
  2293. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  2294. VaporSlot,
  2295. 'vapor2 fallback',
  2296. )
  2297. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  2298. VaporForwardedSlot2WithFallback,
  2299. 'vapor1 fallback',
  2300. )
  2301. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  2302. const root = document.createElement('div')
  2303. createApp(App).use(vaporInteropPlugin).mount(root)
  2304. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  2305. foo.value = 'bar'
  2306. await nextTick()
  2307. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2308. show.value = false
  2309. await nextTick()
  2310. expect(root.innerHTML).toBe(
  2311. '<div>vapor1 fallback</div><!--slot--><!--slot-->',
  2312. )
  2313. show.value = true
  2314. await nextTick()
  2315. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  2316. })
  2317. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vapor slot', async () => {
  2318. const foo = ref('foo')
  2319. const show = ref(true)
  2320. const VaporSlot = createVaporSlot()
  2321. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  2322. VaporSlot,
  2323. 'vdom2 fallback',
  2324. )
  2325. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  2326. VdomForwardedSlot2WithFallback,
  2327. 'vdom1 fallback',
  2328. )
  2329. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  2330. const root = document.createElement('div')
  2331. createApp(App).use(vaporInteropPlugin).mount(root)
  2332. expect(root.innerHTML).toBe('<span>foo</span>')
  2333. foo.value = 'bar'
  2334. await nextTick()
  2335. expect(root.innerHTML).toBe('<span>bar</span>')
  2336. show.value = false
  2337. await nextTick()
  2338. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  2339. show.value = true
  2340. await nextTick()
  2341. expect(root.innerHTML).toBe('<span>bar</span>')
  2342. })
  2343. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  2344. const foo = ref('foo')
  2345. const show = ref(true)
  2346. const VdomSlot = createVdomSlot()
  2347. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  2348. VdomSlot,
  2349. 'vdom2 fallback',
  2350. )
  2351. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  2352. VdomForwardedSlot2WithFallback,
  2353. 'vdom1 fallback',
  2354. )
  2355. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  2356. const root = document.createElement('div')
  2357. createApp(App).use(vaporInteropPlugin).mount(root)
  2358. expect(root.innerHTML).toBe('<span>foo</span>')
  2359. foo.value = 'bar'
  2360. await nextTick()
  2361. expect(root.innerHTML).toBe('<span>bar</span>')
  2362. show.value = false
  2363. await nextTick()
  2364. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  2365. show.value = true
  2366. await nextTick()
  2367. expect(root.innerHTML).toBe('<span>bar</span>')
  2368. })
  2369. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) (multiple) > vapor slot', async () => {
  2370. const foo = ref('foo')
  2371. const show = ref(true)
  2372. const VaporSlot = createVaporSlot()
  2373. const VdomForwardedSlot3WithFallback = createVdomForwardedSlot(
  2374. VaporSlot,
  2375. 'vdom3 fallback',
  2376. )
  2377. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  2378. VdomForwardedSlot3WithFallback,
  2379. 'vdom2 fallback',
  2380. )
  2381. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  2382. VdomForwardedSlot2WithFallback,
  2383. 'vdom1 fallback',
  2384. )
  2385. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  2386. const root = document.createElement('div')
  2387. createApp(App).use(vaporInteropPlugin).mount(root)
  2388. expect(root.innerHTML).toBe('<span>foo</span>')
  2389. foo.value = 'bar'
  2390. await nextTick()
  2391. expect(root.innerHTML).toBe('<span>bar</span>')
  2392. show.value = false
  2393. await nextTick()
  2394. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  2395. show.value = true
  2396. await nextTick()
  2397. expect(root.innerHTML).toBe('<span>bar</span>')
  2398. })
  2399. })
  2400. })
  2401. })