|
| 1 | +// NOTE: This test is implemented based on the case of `runtime-core/__test__/componentSlots.spec.ts`. |
| 2 | + |
| 3 | +import { |
| 4 | + createComponent, |
| 5 | + createVaporApp, |
| 6 | + defineComponent, |
| 7 | + getCurrentInstance, |
| 8 | + nextTick, |
| 9 | + ref, |
| 10 | + template, |
| 11 | +} from '../src' |
| 12 | +import { makeRender } from './_utils' |
| 13 | + |
| 14 | +const define = makeRender<any>() |
| 15 | +function renderWithSlots(slots: any): any { |
| 16 | + let instance: any |
| 17 | + const Comp = defineComponent({ |
| 18 | + render() { |
| 19 | + const t0 = template('<div></div>') |
| 20 | + const n0 = t0() |
| 21 | + instance = getCurrentInstance() |
| 22 | + return n0 |
| 23 | + }, |
| 24 | + }) |
| 25 | + |
| 26 | + const { render } = define({ |
| 27 | + render() { |
| 28 | + return createComponent(Comp, {}, slots) |
| 29 | + }, |
| 30 | + }) |
| 31 | + |
| 32 | + render() |
| 33 | + return instance |
| 34 | +} |
| 35 | + |
| 36 | +describe('component: slots', () => { |
| 37 | + test('initSlots: instance.slots should be set correctly', () => { |
| 38 | + const { slots } = renderWithSlots({ _: 1 }) |
| 39 | + expect(slots).toMatchObject({ _: 1 }) |
| 40 | + }) |
| 41 | + |
| 42 | + // NOTE: slot normalization is not supported |
| 43 | + test.todo( |
| 44 | + 'initSlots: should normalize object slots (when value is null, string, array)', |
| 45 | + () => {}, |
| 46 | + ) |
| 47 | + test.todo( |
| 48 | + 'initSlots: should normalize object slots (when value is function)', |
| 49 | + () => {}, |
| 50 | + ) |
| 51 | + |
| 52 | + test('initSlots: instance.slots should be set correctly', () => { |
| 53 | + let instance: any |
| 54 | + const Comp = defineComponent({ |
| 55 | + render() { |
| 56 | + const t0 = template('<div></div>') |
| 57 | + const n0 = t0() |
| 58 | + instance = getCurrentInstance() |
| 59 | + return n0 |
| 60 | + }, |
| 61 | + }) |
| 62 | + |
| 63 | + const { render } = define({ |
| 64 | + render() { |
| 65 | + return createComponent(Comp, {}, { header: () => template('header')() }) |
| 66 | + }, |
| 67 | + }) |
| 68 | + |
| 69 | + render() |
| 70 | + |
| 71 | + expect(instance.slots.header()).toMatchObject( |
| 72 | + document.createTextNode('header'), |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + // runtime-core's "initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)" |
| 77 | + test('initSlots: instance.slots should be set correctly', () => { |
| 78 | + const { slots } = renderWithSlots({ |
| 79 | + default: () => template('<span></span>')(), |
| 80 | + }) |
| 81 | + |
| 82 | + // expect( |
| 83 | + // '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.', |
| 84 | + // ).toHaveBeenWarned() |
| 85 | + |
| 86 | + expect(slots.default()).toMatchObject(document.createElement('span')) |
| 87 | + }) |
| 88 | + |
| 89 | + test('updateSlots: instance.slots should be updated correctly', async () => { |
| 90 | + const flag1 = ref(true) |
| 91 | + |
| 92 | + let instance: any |
| 93 | + const Child = () => { |
| 94 | + instance = getCurrentInstance() |
| 95 | + return template('child')() |
| 96 | + } |
| 97 | + |
| 98 | + const { render } = define({ |
| 99 | + render() { |
| 100 | + return createComponent(Child, {}, { _: 2 as any }, () => [ |
| 101 | + flag1.value |
| 102 | + ? { name: 'one', fn: () => template('<span></span>')() } |
| 103 | + : { name: 'two', fn: () => template('<div></div>')() }, |
| 104 | + ]) |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + render() |
| 109 | + |
| 110 | + expect(instance.slots).toHaveProperty('one') |
| 111 | + expect(instance.slots).not.toHaveProperty('two') |
| 112 | + |
| 113 | + flag1.value = false |
| 114 | + await nextTick() |
| 115 | + |
| 116 | + expect(instance.slots).not.toHaveProperty('one') |
| 117 | + expect(instance.slots).toHaveProperty('two') |
| 118 | + }) |
| 119 | + |
| 120 | + // NOTE: it is not supported |
| 121 | + // test('updateSlots: instance.slots should be updated correctly (when slotType is null)', () => {}) |
| 122 | + |
| 123 | + // runtime-core's "updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)" |
| 124 | + test('updateSlots: instance.slots should be update correctly', async () => { |
| 125 | + const flag1 = ref(true) |
| 126 | + |
| 127 | + let instance: any |
| 128 | + const Child = () => { |
| 129 | + instance = getCurrentInstance() |
| 130 | + return template('child')() |
| 131 | + } |
| 132 | + |
| 133 | + const { render } = define({ |
| 134 | + setup() { |
| 135 | + return createComponent(Child, {}, {}, () => [ |
| 136 | + flag1.value |
| 137 | + ? [{ name: 'header', fn: () => template('header')() }] |
| 138 | + : [{ name: 'footer', fn: () => template('footer')() }], |
| 139 | + ]) |
| 140 | + }, |
| 141 | + }) |
| 142 | + render() |
| 143 | + |
| 144 | + expect(instance.slots).toHaveProperty('header') |
| 145 | + flag1.value = false |
| 146 | + await nextTick() |
| 147 | + |
| 148 | + // expect( |
| 149 | + // '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.', |
| 150 | + // ).toHaveBeenWarned() |
| 151 | + |
| 152 | + expect(instance.slots).toHaveProperty('footer') |
| 153 | + }) |
| 154 | + |
| 155 | + test.todo('should respect $stable flag', async () => { |
| 156 | + // TODO: $stable flag? |
| 157 | + }) |
| 158 | + |
| 159 | + test.todo('should not warn when mounting another app in setup', () => { |
| 160 | + // TODO: warning |
| 161 | + const Comp = defineComponent({ |
| 162 | + render() { |
| 163 | + const i = getCurrentInstance() |
| 164 | + return i!.slots.default!() |
| 165 | + }, |
| 166 | + }) |
| 167 | + const mountComp = () => { |
| 168 | + createVaporApp({ |
| 169 | + render() { |
| 170 | + return createComponent( |
| 171 | + Comp, |
| 172 | + {}, |
| 173 | + { default: () => template('msg')() }, |
| 174 | + )! |
| 175 | + }, |
| 176 | + }) |
| 177 | + } |
| 178 | + const App = { |
| 179 | + setup() { |
| 180 | + mountComp() |
| 181 | + }, |
| 182 | + render() { |
| 183 | + return null! |
| 184 | + }, |
| 185 | + } |
| 186 | + createVaporApp(App).mount(document.createElement('div')) |
| 187 | + expect( |
| 188 | + 'Slot "default" invoked outside of the render function', |
| 189 | + ).not.toHaveBeenWarned() |
| 190 | + }) |
| 191 | +}) |
0 commit comments