componentSlots.spec.ts 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
  2. import {
  3. child,
  4. createComponent,
  5. createFor,
  6. createForSlots,
  7. createIf,
  8. createSlot,
  9. createVaporApp,
  10. defineVaporComponent,
  11. 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. })