|
1 |
| -import { defineComponent, h } from 'vue' |
| 1 | +import { h } from 'vue' |
2 | 2 |
|
3 | 3 | import { mount } from '../../src'
|
4 |
| -import WithProps from '../components/WithProps.vue' |
| 4 | +import Hello from '../components/Hello.vue' |
| 5 | +import ComponentWithSlots from '../components/ComponentWithSlots.vue' |
5 | 6 |
|
6 | 7 | describe('slots', () => {
|
7 |
| - it('supports default slot', () => { |
8 |
| - const ItemWithSlots = defineComponent({ |
9 |
| - name: 'ItemWithSlots', |
10 |
| - render() { |
11 |
| - return h('div', {}, this.$slots.default()) |
12 |
| - } |
| 8 | + describe('normal slots', () => { |
| 9 | + it('supports providing a plain string text in slot', () => { |
| 10 | + const defaultString = 'Rendered in Default' |
| 11 | + let namedString = 'Rendered in Named' |
| 12 | + const wrapper = mount(ComponentWithSlots, { |
| 13 | + slots: { |
| 14 | + default: defaultString, |
| 15 | + named: namedString |
| 16 | + } |
| 17 | + }) |
| 18 | + expect(wrapper.find('.default').text()).toBe(defaultString) |
| 19 | + expect(wrapper.find('.named').text()).toBe(namedString) |
13 | 20 | })
|
14 | 21 |
|
15 |
| - const wrapper = mount(ItemWithSlots, { |
16 |
| - slots: { |
17 |
| - default: h('span', {}, 'Default Slot') |
18 |
| - } |
| 22 | + it('supports providing an html string into a slot', () => { |
| 23 | + const defaultSlot = '<div><p class="defaultNested">Content</p></div>' |
| 24 | + const namedSlot = '<div><p class="namedNested">Content</p></div>' |
| 25 | + |
| 26 | + const wrapper = mount(ComponentWithSlots, { |
| 27 | + slots: { |
| 28 | + default: defaultSlot, |
| 29 | + named: namedSlot |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + expect(wrapper.find('.defaultNested').exists()).toBe(true) |
| 34 | + expect(wrapper.find('.namedNested').exists()).toBe(true) |
19 | 35 | })
|
20 | 36 |
|
21 |
| - expect(wrapper.html()).toBe('<div><span>Default Slot</span></div>') |
22 |
| - }) |
| 37 | + it('supports providing a render function to slot', () => { |
| 38 | + const wrapper = mount(ComponentWithSlots, { |
| 39 | + slots: { |
| 40 | + default: h('span', {}, 'Default'), |
| 41 | + named: h('span', {}, 'Named') |
| 42 | + } |
| 43 | + }) |
23 | 44 |
|
24 |
| - it('supports named slots', () => { |
25 |
| - const ItemWithNamedSlot = defineComponent({ |
26 |
| - render() { |
27 |
| - return h('div', {}, this.$slots.foo()) |
28 |
| - } |
| 45 | + expect(wrapper.find('.default').html()).toEqual( |
| 46 | + '<div class="default"><span>Default</span></div>' |
| 47 | + ) |
| 48 | + expect(wrapper.find('.named').html()).toEqual( |
| 49 | + '<div class="named"><span>Named</span></div>' |
| 50 | + ) |
29 | 51 | })
|
30 | 52 |
|
31 |
| - const wrapper = mount(ItemWithNamedSlot, { |
32 |
| - slots: { |
33 |
| - foo: h('span', {}, 'Foo') |
34 |
| - } |
| 53 | + it('does not render slots that do not exist', () => { |
| 54 | + const wrapper = mount(ComponentWithSlots, { |
| 55 | + slots: { |
| 56 | + notExisting: () => h('span', {}, 'NotExistingText') |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + expect(wrapper.text()).not.toContain('NotExistingText') |
35 | 61 | })
|
36 | 62 |
|
37 |
| - expect(wrapper.html()).toBe('<div><span>Foo</span></div>') |
| 63 | + it('supports passing a SFC', () => { |
| 64 | + const wrapper = mount(ComponentWithSlots, { |
| 65 | + slots: { |
| 66 | + named: Hello |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + expect(wrapper.find('.named').html()).toBe( |
| 71 | + '' + |
| 72 | + '<div class="named">' + |
| 73 | + '<div id="root">' + |
| 74 | + '<div id="msg"></div>' + |
| 75 | + '</div>' + |
| 76 | + '</div>' |
| 77 | + ) |
| 78 | + }) |
38 | 79 | })
|
39 | 80 |
|
40 |
| - it('supports default and named slots together', () => { |
41 |
| - const Component = defineComponent({ |
42 |
| - render() { |
43 |
| - return h('div', {}, [ |
44 |
| - h('div', {}, this.$slots.foo()), |
45 |
| - h('div', {}, this.$slots.default()) |
46 |
| - ]) |
47 |
| - } |
| 81 | + describe('scoped slots', () => { |
| 82 | + it('allows providing a plain text string', () => { |
| 83 | + const wrapper = mount(ComponentWithSlots, { |
| 84 | + slots: { |
| 85 | + scoped: 'Just a plain string' |
| 86 | + } |
| 87 | + }) |
| 88 | + expect(wrapper.find('.scoped').text()).toEqual('Just a plain string') |
48 | 89 | })
|
49 | 90 |
|
50 |
| - const wrapper = mount(Component, { |
51 |
| - slots: { |
52 |
| - default: 'Default', |
53 |
| - foo: h('h1', {}, 'Named Slot') |
54 |
| - } |
| 91 | + it('allows passing a function that returns a render function', () => { |
| 92 | + const wrapper = mount(ComponentWithSlots, { |
| 93 | + slots: { |
| 94 | + scoped: (params) => h('div', {}, JSON.stringify(params)) |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + expect(wrapper.find('.scoped').text()).toEqual( |
| 99 | + '{"boolean":true,"string":"string","object":{"foo":"foo"}}' |
| 100 | + ) |
55 | 101 | })
|
56 | 102 |
|
57 |
| - expect(wrapper.html()).toBe( |
58 |
| - '<div><div><h1>Named Slot</h1></div><div>Default</div></div>' |
59 |
| - ) |
60 |
| - }) |
| 103 | + it('allows passing a function to store variables for assertion', () => { |
| 104 | + let assertParams |
61 | 105 |
|
62 |
| - it('supports passing a SFC', () => { |
63 |
| - const wrapper = mount( |
64 |
| - { |
65 |
| - template: `<div><slot name="foo" msg="Hello" /></div>` |
66 |
| - }, |
67 |
| - { |
| 106 | + const wrapper = mount(ComponentWithSlots, { |
68 | 107 | slots: {
|
69 |
| - foo: WithProps |
| 108 | + scoped: (params) => { |
| 109 | + assertParams = params |
| 110 | + // always return something |
| 111 | + return 'foo' |
| 112 | + } |
70 | 113 | }
|
71 |
| - } |
72 |
| - ) |
| 114 | + }) |
73 | 115 |
|
74 |
| - expect(wrapper.html()).toBe('<div><p>Hello</p></div>') |
| 116 | + expect(assertParams).toEqual({ |
| 117 | + boolean: true, |
| 118 | + string: 'string', |
| 119 | + object: { foo: 'foo' } |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('allows passing a scoped slot via string with no destructuring using the # syntax', () => { |
| 124 | + const wrapper = mount(ComponentWithSlots, { |
| 125 | + slots: { |
| 126 | + scoped: `<template #scoped="params"><div>Just a plain {{ params.boolean }} {{ params.string }}</div></template>` |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + expect(wrapper.find('.scoped').text()).toEqual('Just a plain true string') |
| 131 | + }) |
| 132 | + |
| 133 | + it('allows passing a scoped slot via a string with destructuring using the # syntax', () => { |
| 134 | + const wrapper = mount(ComponentWithSlots, { |
| 135 | + slots: { |
| 136 | + scoped: `<template #scoped="{string, boolean}"><div>Just a plain {{ boolean }} {{ string }}</div></template>` |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + expect(wrapper.find('.scoped').text()).toEqual('Just a plain true string') |
| 141 | + }) |
| 142 | + |
| 143 | + it('allows passing a scoped slot via string with no destructuring using the v-slot syntax ', () => { |
| 144 | + const wrapper = mount(ComponentWithSlots, { |
| 145 | + slots: { |
| 146 | + scoped: `<template v-slot:scoped="params"><div>Just a plain {{ params.boolean }} {{ params.string }}</div></template>` |
| 147 | + } |
| 148 | + }) |
| 149 | + |
| 150 | + expect(wrapper.find('.scoped').text()).toEqual('Just a plain true string') |
| 151 | + }) |
| 152 | + |
| 153 | + it('allows passing a scoped slot via string with no destructuring without template tag', () => { |
| 154 | + const wrapper = mount(ComponentWithSlots, { |
| 155 | + slots: { |
| 156 | + scoped: `<div>Just a plain {{ params.boolean }} {{ params.string }}</div>` |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + expect(wrapper.find('.scoped').text()).toEqual('Just a plain true string') |
| 161 | + }) |
75 | 162 | })
|
76 | 163 | })
|
0 commit comments