componentSlots.spec.ts 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
  2. import {
  3. child,
  4. createComponent,
  5. createFor,
  6. createForSlots,
  7. createIf,
  8. createSlot,
  9. createVaporApp,
  10. defineVaporComponent,
  11. insert,
  12. prepend,
  13. renderEffect,
  14. setInsertionState,
  15. template,
  16. vaporInteropPlugin,
  17. withVaporCtx,
  18. } from '../src'
  19. import {
  20. type Ref,
  21. createApp,
  22. createSlots,
  23. currentInstance,
  24. h,
  25. nextTick,
  26. ref,
  27. renderSlot,
  28. toDisplayString,
  29. } from '@vue/runtime-dom'
  30. import { makeRender } from './_utils'
  31. import type { DynamicSlot } from '../src/componentSlots'
  32. import { setElementText, setText } from '../src/dom/prop'
  33. const define = makeRender<any>()
  34. function renderWithSlots(slots: any): any {
  35. let instance: any
  36. const Comp = defineVaporComponent({
  37. setup() {
  38. const t0 = template('<div></div>')
  39. const n0 = t0()
  40. instance = currentInstance
  41. return n0
  42. },
  43. })
  44. const { render } = define({
  45. render() {
  46. return createComponent(Comp, {}, slots)
  47. },
  48. })
  49. render()
  50. return instance
  51. }
  52. describe('component: slots', () => {
  53. test('initSlots: instance.slots should be set correctly', () => {
  54. const { slots } = renderWithSlots({
  55. default: () => template('<span></span>')(),
  56. })
  57. expect(slots.default()).toMatchObject(document.createElement('span'))
  58. })
  59. test('updateSlots: instance.slots should be updated correctly', async () => {
  60. const flag1 = ref(true)
  61. let instance: any
  62. const Child = () => {
  63. instance = currentInstance
  64. return template('child')()
  65. }
  66. const { render } = define({
  67. render() {
  68. return createComponent(
  69. Child,
  70. {},
  71. {
  72. $: [
  73. () =>
  74. flag1.value
  75. ? { name: 'one', fn: () => template('<span></span>')() }
  76. : { name: 'two', fn: () => template('<div></div>')() },
  77. ],
  78. },
  79. )
  80. },
  81. })
  82. render()
  83. expect(instance.slots).toHaveProperty('one')
  84. expect(instance.slots).not.toHaveProperty('two')
  85. flag1.value = false
  86. await nextTick()
  87. expect(instance.slots).not.toHaveProperty('one')
  88. expect(instance.slots).toHaveProperty('two')
  89. })
  90. test('should work with createFlorSlots', async () => {
  91. const loop = ref([1, 2, 3])
  92. let instance: any
  93. const Child = () => {
  94. instance = currentInstance
  95. return template('child')()
  96. }
  97. const { render } = define({
  98. setup() {
  99. return createComponent(Child, null, {
  100. $: [
  101. () =>
  102. createForSlots(loop.value, (item, i) => ({
  103. name: item,
  104. fn: () => template(item + i)(),
  105. })),
  106. ],
  107. })
  108. },
  109. })
  110. render()
  111. expect(instance.slots).toHaveProperty('1')
  112. expect(instance.slots).toHaveProperty('2')
  113. expect(instance.slots).toHaveProperty('3')
  114. loop.value.push(4)
  115. await nextTick()
  116. expect(instance.slots).toHaveProperty('4')
  117. loop.value.shift()
  118. await nextTick()
  119. expect(instance.slots).not.toHaveProperty('1')
  120. })
  121. // passes but no warning for slot invocation in vapor currently
  122. test.todo('should not warn when mounting another app in setup', () => {
  123. const Comp = defineVaporComponent({
  124. setup(_, { slots }) {
  125. return slots.default!()
  126. },
  127. })
  128. const mountComp = () => {
  129. createVaporApp({
  130. render() {
  131. return createComponent(
  132. Comp,
  133. {},
  134. { default: () => template('msg')() },
  135. )!
  136. },
  137. })
  138. }
  139. const App = {
  140. setup() {
  141. mountComp()
  142. return []
  143. },
  144. }
  145. createVaporApp(App).mount(document.createElement('div'))
  146. expect(
  147. 'Slot "default" invoked outside of the render function',
  148. ).not.toHaveBeenWarned()
  149. })
  150. describe('createSlot', () => {
  151. test('slot should be rendered correctly', () => {
  152. const Comp = defineVaporComponent(() => {
  153. const n0 = template('<div>')()
  154. insert(createSlot('header'), n0 as any as ParentNode)
  155. return n0
  156. })
  157. const { host } = define(() => {
  158. return createComponent(Comp, null, {
  159. header: () => 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 n2 = createComponent(
  664. Child,
  665. null,
  666. {
  667. foo: withVaporCtx(() => {
  668. return createSlot('foo', null)
  669. }),
  670. },
  671. true,
  672. )
  673. return n2
  674. },
  675. })
  676. const foo = ref('foo')
  677. const { host } = define({
  678. setup() {
  679. const n2 = createComponent(
  680. Parent,
  681. null,
  682. {
  683. foo: () => {
  684. const n0 = template(' ')() as any
  685. renderEffect(() => setText(n0, foo.value))
  686. return n0
  687. },
  688. },
  689. true,
  690. )
  691. return n2
  692. },
  693. }).render()
  694. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->')
  695. foo.value = 'bar'
  696. await nextTick()
  697. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->')
  698. })
  699. test('mixed with non-forwarded slot', async () => {
  700. const Child = defineVaporComponent({
  701. setup() {
  702. return [createSlot('foo', null)]
  703. },
  704. })
  705. const Parent = defineVaporComponent({
  706. setup() {
  707. const n2 = createComponent(Child, null, {
  708. foo: withVaporCtx(() => {
  709. const n0 = createSlot('foo', null)
  710. return n0
  711. }),
  712. })
  713. const n3 = createSlot('default', null)
  714. return [n2, n3]
  715. },
  716. })
  717. const foo = ref('foo')
  718. const { host } = define({
  719. setup() {
  720. const n2 = createComponent(
  721. Parent,
  722. null,
  723. {
  724. foo: () => {
  725. const n0 = template(' ')() as any
  726. renderEffect(() => setText(n0, foo.value))
  727. return n0
  728. },
  729. default: () => {
  730. const n3 = template(' ')() as any
  731. renderEffect(() => setText(n3, foo.value))
  732. return n3
  733. },
  734. },
  735. true,
  736. )
  737. return n2
  738. },
  739. }).render()
  740. expect(host.innerHTML).toBe('foo<!--slot--><!--slot-->foo<!--slot-->')
  741. foo.value = 'bar'
  742. await nextTick()
  743. expect(host.innerHTML).toBe('bar<!--slot--><!--slot-->bar<!--slot-->')
  744. })
  745. test('forwarded slot with fallback', async () => {
  746. const Child = defineVaporComponent({
  747. setup() {
  748. return createSlot('default', null, () => template('child fallback')())
  749. },
  750. })
  751. const Parent = defineVaporComponent({
  752. setup() {
  753. const n2 = createComponent(Child, null, {
  754. default: withVaporCtx(() => {
  755. const n0 = createSlot('default', null, () => {
  756. return template('<!-- <div></div> -->')()
  757. })
  758. return n0
  759. }),
  760. })
  761. return n2
  762. },
  763. })
  764. const { html } = define({
  765. setup() {
  766. return createComponent(Parent, null, {
  767. default: () => template('<!-- <div>App</div> -->')(),
  768. })
  769. },
  770. }).render()
  771. expect(html()).toBe('child fallback<!--slot--><!--slot-->')
  772. })
  773. test('forwarded slot with fallback (v-if)', async () => {
  774. const Child = defineVaporComponent({
  775. setup() {
  776. return createSlot('default', null, () => template('child fallback')())
  777. },
  778. })
  779. const show = ref(false)
  780. const Parent = defineVaporComponent({
  781. setup() {
  782. const n2 = createComponent(Child, null, {
  783. default: withVaporCtx(() => {
  784. const n0 = createSlot('default', null, () => {
  785. const n2 = createIf(
  786. () => show.value,
  787. () => {
  788. const n4 = template('<div>if content</div>')()
  789. return n4
  790. },
  791. )
  792. return n2
  793. })
  794. return n0
  795. }),
  796. })
  797. return n2
  798. },
  799. })
  800. const { html } = define({
  801. setup() {
  802. return createComponent(Parent, null, {
  803. default: () => template('<!-- <div>App</div> -->')(),
  804. })
  805. },
  806. }).render()
  807. expect(html()).toBe('child fallback<!--if--><!--slot--><!--slot-->')
  808. show.value = true
  809. await nextTick()
  810. expect(html()).toBe(
  811. '<div>if content</div><!--if--><!--slot--><!--slot-->',
  812. )
  813. })
  814. test('forwarded slot with fallback (v-for)', async () => {
  815. const Child = defineVaporComponent({
  816. setup() {
  817. return createSlot('default', null, () => template('child fallback')())
  818. },
  819. })
  820. const items = ref<number[]>([])
  821. const Parent = defineVaporComponent({
  822. setup() {
  823. const n2 = createComponent(Child, null, {
  824. default: withVaporCtx(() => {
  825. const n0 = createSlot('default', null, () => {
  826. const n2 = createFor(
  827. () => items.value,
  828. for_item0 => {
  829. const n4 = template('<span> </span>')() as any
  830. const x4 = child(n4) as any
  831. renderEffect(() =>
  832. setText(x4, toDisplayString(for_item0.value)),
  833. )
  834. return n4
  835. },
  836. )
  837. return n2
  838. })
  839. return n0
  840. }),
  841. })
  842. return n2
  843. },
  844. })
  845. const { html } = define({
  846. setup() {
  847. return createComponent(Parent, null, {
  848. default: () => template('<!-- <div>App</div> -->')(),
  849. })
  850. },
  851. }).render()
  852. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  853. items.value.push(1)
  854. await nextTick()
  855. expect(html()).toBe('<span>1</span><!--for--><!--slot--><!--slot-->')
  856. items.value.pop()
  857. await nextTick()
  858. expect(html()).toBe('child fallback<!--for--><!--slot--><!--slot-->')
  859. })
  860. describe('vdom interop', () => {
  861. const createVaporSlot = (fallbackText = 'fallback') => {
  862. return defineVaporComponent({
  863. setup() {
  864. const n0 = createSlot('foo', null, () => {
  865. const n2 = template(`<div>${fallbackText}</div>`)()
  866. return n2
  867. })
  868. return n0
  869. },
  870. })
  871. }
  872. const createVdomSlot = (fallbackText = 'fallback') => {
  873. return {
  874. render(this: any) {
  875. return renderSlot(this.$slots, 'foo', {}, () => [
  876. h('div', fallbackText),
  877. ])
  878. },
  879. }
  880. }
  881. const createVaporForwardedSlot = (
  882. targetComponent: any,
  883. fallbackText?: string,
  884. ) => {
  885. return defineVaporComponent({
  886. setup() {
  887. const n2 = createComponent(
  888. targetComponent,
  889. null,
  890. {
  891. foo: withVaporCtx(() => {
  892. return fallbackText
  893. ? createSlot('foo', null, () => {
  894. const n2 = template(`<div>${fallbackText}</div>`)()
  895. return n2
  896. })
  897. : createSlot('foo', null)
  898. }),
  899. },
  900. true,
  901. )
  902. return n2
  903. },
  904. })
  905. }
  906. const createVdomForwardedSlot = (
  907. targetComponent: any,
  908. fallbackText?: string,
  909. ) => {
  910. return {
  911. render(this: any) {
  912. return h(targetComponent, null, {
  913. foo: () => [
  914. fallbackText
  915. ? renderSlot(this.$slots, 'foo', {}, () => [
  916. h('div', fallbackText),
  917. ])
  918. : renderSlot(this.$slots, 'foo'),
  919. ],
  920. _: 3 /* FORWARDED */,
  921. })
  922. },
  923. }
  924. }
  925. const createMultipleVaporForwardedSlots = (
  926. targetComponent: any,
  927. count: number,
  928. ) => {
  929. let current = targetComponent
  930. for (let i = 0; i < count; i++) {
  931. current = createVaporForwardedSlot(current)
  932. }
  933. return current
  934. }
  935. const createMultipleVdomForwardedSlots = (
  936. targetComponent: any,
  937. count: number,
  938. ) => {
  939. let current = targetComponent
  940. for (let i = 0; i < count; i++) {
  941. current = createVdomForwardedSlot(current)
  942. }
  943. return current
  944. }
  945. const createTestApp = (
  946. rootComponent: any,
  947. foo: Ref<string>,
  948. show: Ref<Boolean>,
  949. ) => {
  950. return {
  951. setup() {
  952. return () =>
  953. h(
  954. rootComponent,
  955. null,
  956. createSlots({ _: 2 /* DYNAMIC */ } as any, [
  957. show.value
  958. ? {
  959. name: 'foo',
  960. fn: () => [h('span', foo.value)],
  961. key: '0',
  962. }
  963. : undefined,
  964. ]),
  965. )
  966. },
  967. }
  968. }
  969. const createEmptyTestApp = (rootComponent: any) => {
  970. return {
  971. setup() {
  972. return () => h(rootComponent)
  973. },
  974. }
  975. }
  976. test('vdom slot > vapor forwarded slot > vapor slot', async () => {
  977. const foo = ref('foo')
  978. const show = ref(true)
  979. const VaporSlot = createVaporSlot()
  980. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  981. const App = createTestApp(VaporForwardedSlot, foo, show)
  982. const root = document.createElement('div')
  983. createApp(App).use(vaporInteropPlugin).mount(root)
  984. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  985. foo.value = 'bar'
  986. await nextTick()
  987. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  988. show.value = false
  989. await nextTick()
  990. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  991. })
  992. test('vdom slot > vapor forwarded slot(with fallback) > vapor slot', async () => {
  993. const foo = ref('foo')
  994. const show = ref(true)
  995. const VaporSlot = createVaporSlot()
  996. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  997. VaporSlot,
  998. 'forwarded fallback',
  999. )
  1000. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1001. const root = document.createElement('div')
  1002. createApp(App).use(vaporInteropPlugin).mount(root)
  1003. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1004. foo.value = 'bar'
  1005. await nextTick()
  1006. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1007. show.value = false
  1008. await nextTick()
  1009. expect(root.innerHTML).toBe('<div>forwarded fallback</div><!--slot-->')
  1010. })
  1011. test('vdom slot > vapor forwarded slot > vdom slot', async () => {
  1012. const foo = ref('foo')
  1013. const show = ref(true)
  1014. const VdomSlot = createVdomSlot()
  1015. const VaporForwardedSlot = createVaporForwardedSlot(VdomSlot)
  1016. const App = createTestApp(VaporForwardedSlot, foo, show)
  1017. const root = document.createElement('div')
  1018. createApp(App).use(vaporInteropPlugin).mount(root)
  1019. expect(root.innerHTML).toBe('<span>foo</span>')
  1020. foo.value = 'bar'
  1021. await nextTick()
  1022. expect(root.innerHTML).toBe('<span>bar</span>')
  1023. show.value = false
  1024. await nextTick()
  1025. expect(root.innerHTML).toBe('<div>fallback</div>')
  1026. show.value = true
  1027. await nextTick()
  1028. expect(root.innerHTML).toBe('<span>bar</span>')
  1029. })
  1030. test('vdom slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1031. const foo = ref('foo')
  1032. const show = ref(true)
  1033. const VdomSlot = createVdomSlot()
  1034. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1035. VdomSlot,
  1036. 'forwarded fallback',
  1037. )
  1038. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1039. const root = document.createElement('div')
  1040. createApp(App).use(vaporInteropPlugin).mount(root)
  1041. expect(root.innerHTML).toBe('<span>foo</span>')
  1042. foo.value = 'bar'
  1043. await nextTick()
  1044. expect(root.innerHTML).toBe('<span>bar</span>')
  1045. show.value = false
  1046. await nextTick()
  1047. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1048. })
  1049. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vapor slot', async () => {
  1050. const foo = ref('foo')
  1051. const show = ref(true)
  1052. const VaporSlot = createVaporSlot()
  1053. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1054. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1055. const App = createTestApp(VaporForwardedSlot, foo, show)
  1056. const root = document.createElement('div')
  1057. createApp(App).use(vaporInteropPlugin).mount(root)
  1058. expect(root.innerHTML).toBe('<span>foo</span>')
  1059. foo.value = 'bar'
  1060. await nextTick()
  1061. expect(root.innerHTML).toBe('<span>bar</span>')
  1062. show.value = false
  1063. await nextTick()
  1064. expect(root.innerHTML).toBe('<div>fallback</div>')
  1065. show.value = true
  1066. await nextTick()
  1067. expect(root.innerHTML).toBe('<span>bar</span>')
  1068. })
  1069. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vapor slot', async () => {
  1070. const foo = ref('foo')
  1071. const show = ref(true)
  1072. const VaporSlot = createVaporSlot()
  1073. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1074. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1075. VdomForwardedSlot,
  1076. 'forwarded fallback',
  1077. )
  1078. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1079. const root = document.createElement('div')
  1080. createApp(App).use(vaporInteropPlugin).mount(root)
  1081. expect(root.innerHTML).toBe('<span>foo</span>')
  1082. foo.value = 'bar'
  1083. await nextTick()
  1084. expect(root.innerHTML).toBe('<span>bar</span>')
  1085. show.value = false
  1086. await nextTick()
  1087. expect(root.innerHTML).toBe('<div>forwarded fallback</div>')
  1088. show.value = true
  1089. await nextTick()
  1090. expect(root.innerHTML).toBe('<span>bar</span>')
  1091. })
  1092. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1093. const foo = ref('foo')
  1094. const show = ref(true)
  1095. const VaporSlot = createVaporSlot()
  1096. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1097. VaporSlot,
  1098. 'vdom fallback',
  1099. )
  1100. const VaporForwardedSlot = createVaporForwardedSlot(
  1101. VdomForwardedSlotWithFallback,
  1102. )
  1103. const App = createTestApp(VaporForwardedSlot, foo, show)
  1104. const root = document.createElement('div')
  1105. createApp(App).use(vaporInteropPlugin).mount(root)
  1106. expect(root.innerHTML).toBe('<span>foo</span>')
  1107. foo.value = 'bar'
  1108. await nextTick()
  1109. expect(root.innerHTML).toBe('<span>bar</span>')
  1110. show.value = false
  1111. await nextTick()
  1112. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1113. show.value = true
  1114. await nextTick()
  1115. expect(root.innerHTML).toBe('<span>bar</span>')
  1116. })
  1117. test('vdom slot(empty) > vapor forwarded slot > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1118. const VaporSlot = createVaporSlot()
  1119. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1120. VaporSlot,
  1121. 'vdom fallback',
  1122. )
  1123. const VaporForwardedSlot = createVaporForwardedSlot(
  1124. VdomForwardedSlotWithFallback,
  1125. )
  1126. const App = createEmptyTestApp(VaporForwardedSlot)
  1127. const root = document.createElement('div')
  1128. createApp(App).use(vaporInteropPlugin).mount(root)
  1129. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1130. })
  1131. test('vdom slot > vapor forwarded slot > vdom forwarded slot > vdom slot', async () => {
  1132. const foo = ref('foo')
  1133. const show = ref(true)
  1134. const VdomSlot = createVdomSlot()
  1135. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1136. const VaporForwardedSlot = createVaporForwardedSlot(VdomForwardedSlot)
  1137. const App = createTestApp(VaporForwardedSlot, foo, show)
  1138. const root = document.createElement('div')
  1139. createApp(App).use(vaporInteropPlugin).mount(root)
  1140. expect(root.innerHTML).toBe('<span>foo</span>')
  1141. foo.value = 'bar'
  1142. await nextTick()
  1143. expect(root.innerHTML).toBe('<span>bar</span>')
  1144. show.value = false
  1145. await nextTick()
  1146. expect(root.innerHTML).toBe('<div>fallback</div>')
  1147. show.value = true
  1148. await nextTick()
  1149. expect(root.innerHTML).toBe('<span>bar</span>')
  1150. })
  1151. test('vdom slot > vapor forwarded slot(with fallback) > vdom forwarded slot > vdom slot', async () => {
  1152. const foo = ref('foo')
  1153. const show = ref(true)
  1154. const VdomSlot = createVdomSlot()
  1155. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1156. const VaporForwardedSlotWithFallback = createVaporForwardedSlot(
  1157. VdomForwardedSlot,
  1158. 'vapor fallback',
  1159. )
  1160. const App = createTestApp(VaporForwardedSlotWithFallback, foo, show)
  1161. const root = document.createElement('div')
  1162. createApp(App).use(vaporInteropPlugin).mount(root)
  1163. expect(root.innerHTML).toBe('<span>foo</span>')
  1164. foo.value = 'bar'
  1165. await nextTick()
  1166. expect(root.innerHTML).toBe('<span>bar</span>')
  1167. show.value = false
  1168. await nextTick()
  1169. expect(root.innerHTML).toBe('<div>vapor fallback</div>')
  1170. show.value = true
  1171. await nextTick()
  1172. expect(root.innerHTML).toBe('<span>bar</span>')
  1173. })
  1174. test('vdom slot > vapor forwarded slot > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1175. const foo = ref('foo')
  1176. const show = ref(true)
  1177. const VdomSlot = createVdomSlot()
  1178. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1179. VdomSlot,
  1180. 'vdom fallback',
  1181. )
  1182. const VaporForwardedSlot = createVaporForwardedSlot(
  1183. VdomForwardedSlotWithFallback,
  1184. )
  1185. const App = createTestApp(VaporForwardedSlot, foo, show)
  1186. const root = document.createElement('div')
  1187. createApp(App).use(vaporInteropPlugin).mount(root)
  1188. expect(root.innerHTML).toBe('<span>foo</span>')
  1189. foo.value = 'bar'
  1190. await nextTick()
  1191. expect(root.innerHTML).toBe('<span>bar</span>')
  1192. show.value = false
  1193. await nextTick()
  1194. expect(root.innerHTML).toBe('<div>vdom fallback</div>')
  1195. show.value = true
  1196. await nextTick()
  1197. expect(root.innerHTML).toBe('<span>bar</span>')
  1198. })
  1199. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot > vdom slot', async () => {
  1200. const foo = ref('foo')
  1201. const show = ref(true)
  1202. const VdomSlot = createVdomSlot()
  1203. const VdomForwardedSlot = createVdomForwardedSlot(VdomSlot)
  1204. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1205. VdomForwardedSlot,
  1206. 3,
  1207. )
  1208. const App = createTestApp(VaporForwardedSlot, foo, show)
  1209. const root = document.createElement('div')
  1210. createApp(App).use(vaporInteropPlugin).mount(root)
  1211. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1212. foo.value = 'bar'
  1213. await nextTick()
  1214. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1215. show.value = false
  1216. await nextTick()
  1217. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1218. show.value = true
  1219. await nextTick()
  1220. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1221. })
  1222. test('vdom slot > vapor forwarded slot (multiple) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1223. const foo = ref('foo')
  1224. const show = ref(true)
  1225. const VdomSlot = createVdomSlot()
  1226. const VdomForwardedSlotWithFallback = createVdomForwardedSlot(
  1227. VdomSlot,
  1228. 'vdom fallback',
  1229. )
  1230. const VaporForwardedSlot = createMultipleVaporForwardedSlots(
  1231. VdomForwardedSlotWithFallback,
  1232. 3,
  1233. )
  1234. const App = createTestApp(VaporForwardedSlot, foo, show)
  1235. const root = document.createElement('div')
  1236. createApp(App).use(vaporInteropPlugin).mount(root)
  1237. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1238. foo.value = 'bar'
  1239. await nextTick()
  1240. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1241. show.value = false
  1242. await nextTick()
  1243. expect(root.innerHTML).toBe(
  1244. '<div>vdom fallback</div><!--slot--><!--slot-->',
  1245. )
  1246. show.value = true
  1247. await nextTick()
  1248. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1249. })
  1250. test('vdom slot > vdom forwarded slot > vapor slot', async () => {
  1251. const foo = ref('foo')
  1252. const show = ref(true)
  1253. const VaporSlot = createVaporSlot()
  1254. const VdomForwardedSlot = createVdomForwardedSlot(VaporSlot)
  1255. const App = createTestApp(VdomForwardedSlot, foo, show)
  1256. const root = document.createElement('div')
  1257. createApp(App).use(vaporInteropPlugin).mount(root)
  1258. expect(root.innerHTML).toBe('<span>foo</span>')
  1259. foo.value = 'bar'
  1260. await nextTick()
  1261. expect(root.innerHTML).toBe('<span>bar</span>')
  1262. show.value = false
  1263. await nextTick()
  1264. expect(root.innerHTML).toBe('<div>fallback</div>')
  1265. show.value = true
  1266. await nextTick()
  1267. expect(root.innerHTML).toBe('<span>bar</span>')
  1268. })
  1269. test('vdom slot > vdom forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1270. const foo = ref('foo')
  1271. const show = ref(true)
  1272. const VaporSlot = createVaporSlot()
  1273. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1274. const VdomForwardedSlot = createVdomForwardedSlot(VaporForwardedSlot)
  1275. const App = createTestApp(VdomForwardedSlot, foo, show)
  1276. const root = document.createElement('div')
  1277. createApp(App).use(vaporInteropPlugin).mount(root)
  1278. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1279. foo.value = 'bar'
  1280. await nextTick()
  1281. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1282. show.value = false
  1283. await nextTick()
  1284. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1285. show.value = true
  1286. await nextTick()
  1287. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1288. })
  1289. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot > vdom slot', async () => {
  1290. const foo = ref('foo')
  1291. const show = ref(true)
  1292. const VaporSlot = createVaporSlot()
  1293. const VaporForwardedSlot = createVaporForwardedSlot(VaporSlot)
  1294. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1295. VaporForwardedSlot,
  1296. 3,
  1297. )
  1298. const App = createTestApp(VdomForwardedSlot, foo, show)
  1299. const root = document.createElement('div')
  1300. createApp(App).use(vaporInteropPlugin).mount(root)
  1301. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1302. foo.value = 'bar'
  1303. await nextTick()
  1304. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1305. show.value = false
  1306. await nextTick()
  1307. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1308. show.value = true
  1309. await nextTick()
  1310. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1311. })
  1312. test('vdom slot > vdom forwarded slot (multiple) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1313. const foo = ref('foo')
  1314. const show = ref(true)
  1315. const VaporSlot = createVaporSlot()
  1316. const VaporForwardedSlot = createVaporForwardedSlot(
  1317. VaporSlot,
  1318. 'vapor fallback',
  1319. )
  1320. const VdomForwardedSlot = createMultipleVdomForwardedSlots(
  1321. VaporForwardedSlot,
  1322. 3,
  1323. )
  1324. const App = createTestApp(VdomForwardedSlot, foo, show)
  1325. const root = document.createElement('div')
  1326. createApp(App).use(vaporInteropPlugin).mount(root)
  1327. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1328. foo.value = 'bar'
  1329. await nextTick()
  1330. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1331. show.value = false
  1332. await nextTick()
  1333. expect(root.innerHTML).toBe('<div>vapor fallback</div><!--slot-->')
  1334. show.value = true
  1335. await nextTick()
  1336. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1337. })
  1338. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vdom slot', async () => {
  1339. const foo = ref('foo')
  1340. const show = ref(true)
  1341. const VdomSlot = createVdomSlot()
  1342. const VaporForwardedSlot1 = createMultipleVaporForwardedSlots(
  1343. VdomSlot,
  1344. 2,
  1345. )
  1346. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1347. const root = document.createElement('div')
  1348. createApp(App).use(vaporInteropPlugin).mount(root)
  1349. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1350. foo.value = 'bar'
  1351. await nextTick()
  1352. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1353. show.value = false
  1354. await nextTick()
  1355. expect(root.innerHTML).toBe('<div>fallback</div><!--slot-->')
  1356. show.value = true
  1357. await nextTick()
  1358. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1359. })
  1360. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot > vdom slot', async () => {
  1361. const foo = ref('foo')
  1362. const show = ref(true)
  1363. const VdomSlot = createVdomSlot()
  1364. const VaporForwardedSlot2 = createVaporForwardedSlot(VdomSlot)
  1365. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1366. VaporForwardedSlot2,
  1367. 'vapor1 fallback',
  1368. )
  1369. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1370. const root = document.createElement('div')
  1371. createApp(App).use(vaporInteropPlugin).mount(root)
  1372. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1373. foo.value = 'bar'
  1374. await nextTick()
  1375. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1376. show.value = false
  1377. await nextTick()
  1378. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1379. show.value = true
  1380. await nextTick()
  1381. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1382. })
  1383. test('vdom slot > vapor forwarded slot > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1384. const foo = ref('foo')
  1385. const show = ref(true)
  1386. const VdomSlot = createVdomSlot()
  1387. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1388. VdomSlot,
  1389. 'vapor2 fallback',
  1390. )
  1391. const VaporForwardedSlot1 = createVaporForwardedSlot(
  1392. VaporForwardedSlot2WithFallback,
  1393. )
  1394. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1395. const root = document.createElement('div')
  1396. createApp(App).use(vaporInteropPlugin).mount(root)
  1397. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1398. foo.value = 'bar'
  1399. await nextTick()
  1400. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1401. show.value = false
  1402. await nextTick()
  1403. expect(root.innerHTML).toBe('<div>vapor2 fallback</div><!--slot-->')
  1404. show.value = true
  1405. await nextTick()
  1406. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1407. })
  1408. test('vdom slot > vapor forwarded slot > vapor forwarded slot > vapor slot', async () => {
  1409. const foo = ref('foo')
  1410. const show = ref(true)
  1411. const VaporSlot = createVaporSlot()
  1412. const VaporForwardedSlot2 = createVaporForwardedSlot(VaporSlot)
  1413. const VaporForwardedSlot1 =
  1414. createVaporForwardedSlot(VaporForwardedSlot2)
  1415. const App = createTestApp(VaporForwardedSlot1, foo, show)
  1416. const root = document.createElement('div')
  1417. createApp(App).use(vaporInteropPlugin).mount(root)
  1418. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1419. foo.value = 'bar'
  1420. await nextTick()
  1421. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1422. show.value = false
  1423. await nextTick()
  1424. expect(root.innerHTML).toBe('<div>fallback</div><!--slot--><!--slot-->')
  1425. show.value = true
  1426. await nextTick()
  1427. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1428. })
  1429. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vdom slot', async () => {
  1430. const foo = ref('foo')
  1431. const show = ref(true)
  1432. const VdomSlot = createVdomSlot()
  1433. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1434. VdomSlot,
  1435. 'vapor2 fallback',
  1436. )
  1437. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1438. VaporForwardedSlot2WithFallback,
  1439. 'vapor1 fallback',
  1440. )
  1441. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1442. const root = document.createElement('div')
  1443. createApp(App).use(vaporInteropPlugin).mount(root)
  1444. expect(root.innerHTML).toBe('<span>foo</span><!--slot-->')
  1445. foo.value = 'bar'
  1446. await nextTick()
  1447. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1448. show.value = false
  1449. await nextTick()
  1450. expect(root.innerHTML).toBe('<div>vapor1 fallback</div><!--slot-->')
  1451. show.value = true
  1452. await nextTick()
  1453. expect(root.innerHTML).toBe('<span>bar</span><!--slot-->')
  1454. })
  1455. test('vdom slot > vapor forwarded slot(with fallback) > vapor forwarded slot(with fallback) > vapor slot', async () => {
  1456. const foo = ref('foo')
  1457. const show = ref(true)
  1458. const VaporSlot = createVaporSlot()
  1459. const VaporForwardedSlot2WithFallback = createVaporForwardedSlot(
  1460. VaporSlot,
  1461. 'vapor2 fallback',
  1462. )
  1463. const VaporForwardedSlot1WithFallback = createVaporForwardedSlot(
  1464. VaporForwardedSlot2WithFallback,
  1465. 'vapor1 fallback',
  1466. )
  1467. const App = createTestApp(VaporForwardedSlot1WithFallback, foo, show)
  1468. const root = document.createElement('div')
  1469. createApp(App).use(vaporInteropPlugin).mount(root)
  1470. expect(root.innerHTML).toBe('<span>foo</span><!--slot--><!--slot-->')
  1471. foo.value = 'bar'
  1472. await nextTick()
  1473. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1474. show.value = false
  1475. await nextTick()
  1476. expect(root.innerHTML).toBe(
  1477. '<div>vapor1 fallback</div><!--slot--><!--slot-->',
  1478. )
  1479. show.value = true
  1480. await nextTick()
  1481. expect(root.innerHTML).toBe('<span>bar</span><!--slot--><!--slot-->')
  1482. })
  1483. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vapor slot', async () => {
  1484. const foo = ref('foo')
  1485. const show = ref(true)
  1486. const VaporSlot = createVaporSlot()
  1487. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1488. VaporSlot,
  1489. 'vdom2 fallback',
  1490. )
  1491. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1492. VdomForwardedSlot2WithFallback,
  1493. 'vdom1 fallback',
  1494. )
  1495. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1496. const root = document.createElement('div')
  1497. createApp(App).use(vaporInteropPlugin).mount(root)
  1498. expect(root.innerHTML).toBe('<span>foo</span>')
  1499. foo.value = 'bar'
  1500. await nextTick()
  1501. expect(root.innerHTML).toBe('<span>bar</span>')
  1502. show.value = false
  1503. await nextTick()
  1504. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1505. show.value = true
  1506. await nextTick()
  1507. expect(root.innerHTML).toBe('<span>bar</span>')
  1508. })
  1509. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) > vdom slot', async () => {
  1510. const foo = ref('foo')
  1511. const show = ref(true)
  1512. const VdomSlot = createVdomSlot()
  1513. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1514. VdomSlot,
  1515. 'vdom2 fallback',
  1516. )
  1517. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1518. VdomForwardedSlot2WithFallback,
  1519. 'vdom1 fallback',
  1520. )
  1521. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1522. const root = document.createElement('div')
  1523. createApp(App).use(vaporInteropPlugin).mount(root)
  1524. expect(root.innerHTML).toBe('<span>foo</span>')
  1525. foo.value = 'bar'
  1526. await nextTick()
  1527. expect(root.innerHTML).toBe('<span>bar</span>')
  1528. show.value = false
  1529. await nextTick()
  1530. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1531. show.value = true
  1532. await nextTick()
  1533. expect(root.innerHTML).toBe('<span>bar</span>')
  1534. })
  1535. test('vdom slot > vdom forwarded slot(with fallback) > vdom forwarded slot(with fallback) (multiple) > vapor slot', async () => {
  1536. const foo = ref('foo')
  1537. const show = ref(true)
  1538. const VaporSlot = createVaporSlot()
  1539. const VdomForwardedSlot3WithFallback = createVdomForwardedSlot(
  1540. VaporSlot,
  1541. 'vdom3 fallback',
  1542. )
  1543. const VdomForwardedSlot2WithFallback = createVdomForwardedSlot(
  1544. VdomForwardedSlot3WithFallback,
  1545. 'vdom2 fallback',
  1546. )
  1547. const VdomForwardedSlot1WithFallback = createVdomForwardedSlot(
  1548. VdomForwardedSlot2WithFallback,
  1549. 'vdom1 fallback',
  1550. )
  1551. const App = createTestApp(VdomForwardedSlot1WithFallback, foo, show)
  1552. const root = document.createElement('div')
  1553. createApp(App).use(vaporInteropPlugin).mount(root)
  1554. expect(root.innerHTML).toBe('<span>foo</span>')
  1555. foo.value = 'bar'
  1556. await nextTick()
  1557. expect(root.innerHTML).toBe('<span>bar</span>')
  1558. show.value = false
  1559. await nextTick()
  1560. expect(root.innerHTML).toBe('<div>vdom1 fallback</div>')
  1561. show.value = true
  1562. await nextTick()
  1563. expect(root.innerHTML).toBe('<span>bar</span>')
  1564. })
  1565. })
  1566. test('consecutive slots with insertion state', async () => {
  1567. const { component: Child } = define({
  1568. setup() {
  1569. const n2 = template('<div><div>baz</div></div>', true)() as any
  1570. setInsertionState(n2, 0)
  1571. createSlot('default', null)
  1572. setInsertionState(n2, 0)
  1573. createSlot('foo', null)
  1574. return n2
  1575. },
  1576. })
  1577. const { html } = define({
  1578. setup() {
  1579. return createComponent(Child, null, {
  1580. default: () => template('default')(),
  1581. foo: () => template('foo')(),
  1582. })
  1583. },
  1584. }).render()
  1585. expect(html()).toBe(
  1586. `<div>` +
  1587. `default<!--slot-->` +
  1588. `foo<!--slot-->` +
  1589. `<div>baz</div>` +
  1590. `</div>`,
  1591. )
  1592. })
  1593. })
  1594. })