| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- // NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`.
- import {
- createComponent,
- createForSlots,
- createIf,
- createSlot,
- createVaporApp,
- defineVaporComponent,
- insert,
- prepend,
- renderEffect,
- setText,
- template,
- } from '../src'
- import { currentInstance, nextTick, ref } from '@vue/runtime-dom'
- import { makeRender } from './_utils'
- import type { DynamicSlot } from '../src/componentSlots'
- const define = makeRender<any>()
- function renderWithSlots(slots: any): any {
- let instance: any
- const Comp = defineVaporComponent({
- setup() {
- const t0 = template('<div></div>')
- const n0 = t0()
- instance = currentInstance
- return n0
- },
- })
- const { render } = define({
- render() {
- return createComponent(Comp, {}, slots)
- },
- })
- render()
- return instance
- }
- describe('component: slots', () => {
- test('initSlots: instance.slots should be set correctly', () => {
- const { slots } = renderWithSlots({
- default: () => template('<span></span>')(),
- })
- expect(slots.default()).toMatchObject(document.createElement('span'))
- })
- test('updateSlots: instance.slots should be updated correctly', async () => {
- const flag1 = ref(true)
- let instance: any
- const Child = () => {
- instance = currentInstance
- return template('child')()
- }
- const { render } = define({
- render() {
- return createComponent(
- Child,
- {},
- {
- $: [
- () =>
- flag1.value
- ? { name: 'one', fn: () => template('<span></span>')() }
- : { name: 'two', fn: () => template('<div></div>')() },
- ],
- },
- )
- },
- })
- render()
- expect(instance.slots).toHaveProperty('one')
- expect(instance.slots).not.toHaveProperty('two')
- flag1.value = false
- await nextTick()
- expect(instance.slots).not.toHaveProperty('one')
- expect(instance.slots).toHaveProperty('two')
- })
- test('should work with createFlorSlots', async () => {
- const loop = ref([1, 2, 3])
- let instance: any
- const Child = () => {
- instance = currentInstance
- return template('child')()
- }
- const { render } = define({
- setup() {
- return createComponent(Child, null, {
- $: [
- () =>
- createForSlots(loop.value, (item, i) => ({
- name: item,
- fn: () => template(item + i)(),
- })),
- ],
- })
- },
- })
- render()
- expect(instance.slots).toHaveProperty('1')
- expect(instance.slots).toHaveProperty('2')
- expect(instance.slots).toHaveProperty('3')
- loop.value.push(4)
- await nextTick()
- expect(instance.slots).toHaveProperty('4')
- loop.value.shift()
- await nextTick()
- expect(instance.slots).not.toHaveProperty('1')
- })
- // passes but no warning for slot invocation in vapor currently
- test.todo('should not warn when mounting another app in setup', () => {
- const Comp = defineVaporComponent({
- setup(_, { slots }) {
- return slots.default!()
- },
- })
- const mountComp = () => {
- createVaporApp({
- render() {
- return createComponent(
- Comp,
- {},
- { default: () => template('msg')() },
- )!
- },
- })
- }
- const App = {
- setup() {
- mountComp()
- return []
- },
- }
- createVaporApp(App).mount(document.createElement('div'))
- expect(
- 'Slot "default" invoked outside of the render function',
- ).not.toHaveBeenWarned()
- })
- describe('createSlot', () => {
- test('slot should be rendered correctly', () => {
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div>')()
- insert(createSlot('header'), n0 as any as ParentNode)
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: () => template('header')(),
- })
- }).render()
- expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
- })
- test('slot should be rendered correctly with slot props', async () => {
- const src = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- insert(
- createSlot('header', { title: () => src.value }),
- n0 as any as ParentNode,
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: props => {
- const el = template('<h1></h1>')()
- renderEffect(() => {
- setText(el, props.title)
- })
- return el
- },
- })
- }).render()
- expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
- src.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
- })
- test('dynamic slot props', async () => {
- let props: any
- const bindObj = ref<Record<string, any>>({ foo: 1, baz: 'qux' })
- const Comp = defineVaporComponent(() =>
- createSlot('default', { $: [() => bindObj.value] }),
- )
- define(() =>
- createComponent(Comp, null, {
- default: _props => ((props = _props), []),
- }),
- ).render()
- expect(props).toEqual({ foo: 1, baz: 'qux' })
- bindObj.value.foo = 2
- await nextTick()
- expect(props).toEqual({ foo: 2, baz: 'qux' })
- delete bindObj.value.baz
- await nextTick()
- expect(props).toEqual({ foo: 2 })
- })
- test('dynamic slot props with static slot props', async () => {
- let props: any
- const foo = ref(0)
- const bindObj = ref<Record<string, any>>({ foo: 100, baz: 'qux' })
- const Comp = defineVaporComponent(() =>
- createSlot('default', {
- foo: () => foo.value,
- $: [() => bindObj.value],
- }),
- )
- define(() =>
- createComponent(Comp, null, {
- default: _props => ((props = _props), []),
- }),
- ).render()
- expect(props).toEqual({ foo: 100, baz: 'qux' })
- foo.value = 2
- await nextTick()
- expect(props).toEqual({ foo: 100, baz: 'qux' })
- delete bindObj.value.foo
- await nextTick()
- expect(props).toEqual({ foo: 2, baz: 'qux' })
- })
- test('dynamic slot should be rendered correctly with slot props', async () => {
- const val = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(
- n0 as any as ParentNode,
- createSlot('header', { title: () => val.value }),
- )
- return n0
- })
- const { host } = define(() => {
- // dynamic slot
- return createComponent(Comp, null, {
- $: [
- () => ({
- name: 'header',
- fn: (props: any) => {
- const el = template('<h1></h1>')()
- renderEffect(() => {
- setText(el, props.title)
- })
- return el
- },
- }),
- ],
- })
- }).render()
- expect(host.innerHTML).toBe('<div><h1>header</h1><!--slot--></div>')
- val.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1>footer</h1><!--slot--></div>')
- })
- test('dynamic slot outlet should be render correctly with slot props', async () => {
- const val = ref('header')
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(
- n0 as any as ParentNode,
- createSlot(
- () => val.value, // dynamic slot outlet name
- ),
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, null, {
- header: () => template('header')(),
- footer: () => template('footer')(),
- })
- }).render()
- expect(host.innerHTML).toBe('<div>header<!--slot--></div>')
- val.value = 'footer'
- await nextTick()
- expect(host.innerHTML).toBe('<div>footer<!--slot--></div>')
- })
- test('fallback should be render correctly', () => {
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- insert(
- createSlot('header', undefined, () => template('fallback')()),
- n0 as any as ParentNode,
- )
- return n0
- })
- const { host } = define(() => {
- return createComponent(Comp, {}, {})
- }).render()
- expect(host.innerHTML).toBe('<div>fallback<!--slot--></div>')
- })
- test('dynamic slot should be updated correctly', async () => {
- const flag1 = ref(true)
- const Child = defineVaporComponent(() => {
- const temp0 = template('<p></p>')
- const el0 = temp0()
- const el1 = temp0()
- const slot1 = createSlot('one', null, () => template('one fallback')())
- const slot2 = createSlot('two', null, () => template('two fallback')())
- insert(slot1, el0 as any as ParentNode)
- insert(slot2, el1 as any as ParentNode)
- return [el0, el1]
- })
- const { host } = define(() => {
- return createComponent(Child, null, {
- $: [
- () =>
- flag1.value
- ? { name: 'one', fn: () => template('one content')() }
- : { name: 'two', fn: () => template('two content')() },
- ],
- })
- }).render()
- expect(host.innerHTML).toBe(
- '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
- )
- flag1.value = false
- await nextTick()
- expect(host.innerHTML).toBe(
- '<p>one fallback<!--slot--></p><p>two content<!--slot--></p>',
- )
- flag1.value = true
- await nextTick()
- expect(host.innerHTML).toBe(
- '<p>one content<!--slot--></p><p>two fallback<!--slot--></p>',
- )
- })
- test('dynamic slot outlet should be updated correctly', async () => {
- const slotOutletName = ref('one')
- const Child = defineVaporComponent(() => {
- const temp0 = template('<p>')
- const el0 = temp0()
- const slot1 = createSlot(
- () => slotOutletName.value,
- undefined,
- () => template('fallback')(),
- )
- insert(slot1, el0 as any as ParentNode)
- return el0
- })
- const { host } = define(() => {
- return createComponent(
- Child,
- {},
- {
- one: () => template('one content')(),
- two: () => template('two content')(),
- },
- )
- }).render()
- expect(host.innerHTML).toBe('<p>one content<!--slot--></p>')
- slotOutletName.value = 'two'
- await nextTick()
- expect(host.innerHTML).toBe('<p>two content<!--slot--></p>')
- slotOutletName.value = 'none'
- await nextTick()
- expect(host.innerHTML).toBe('<p>fallback<!--slot--></p>')
- })
- test('non-exist slot', async () => {
- const Child = defineVaporComponent(() => {
- const el0 = template('<p>')()
- const slot = createSlot('not-exist', undefined)
- insert(slot, el0 as any as ParentNode)
- return el0
- })
- const { host } = define(() => {
- return createComponent(Child)
- }).render()
- expect(host.innerHTML).toBe('<p><!--slot--></p>')
- })
- test('use fallback when inner content changes', async () => {
- const Child = {
- setup() {
- return createSlot('default', null, () =>
- document.createTextNode('fallback'),
- )
- },
- }
- const toggle = ref(true)
- const { html } = define({
- setup() {
- return createComponent(Child, null, {
- default: () => {
- return createIf(
- () => toggle.value,
- () => {
- return document.createTextNode('content')
- },
- )
- },
- })
- },
- }).render()
- expect(html()).toBe('content<!--if--><!--slot-->')
- toggle.value = false
- await nextTick()
- expect(html()).toBe('fallback<!--if--><!--slot-->')
- toggle.value = true
- await nextTick()
- expect(html()).toBe('content<!--if--><!--slot-->')
- })
- test('dynamic slot work with v-if', async () => {
- const val = ref('header')
- const toggle = ref(false)
- const Comp = defineVaporComponent(() => {
- const n0 = template('<div></div>')()
- prepend(n0 as any as ParentNode, createSlot('header', null))
- return n0
- })
- const { host } = define(() => {
- // dynamic slot
- return createComponent(Comp, null, {
- $: [
- () =>
- (toggle.value
- ? {
- name: val.value,
- fn: () => {
- return template('<h1></h1>')()
- },
- }
- : void 0) as DynamicSlot,
- ],
- })
- }).render()
- expect(host.innerHTML).toBe('<div><!--slot--></div>')
- toggle.value = true
- await nextTick()
- expect(host.innerHTML).toBe('<div><h1></h1><!--slot--></div>')
- })
- })
- })
|