2
2
3
3
import {
4
4
createComponent ,
5
+ createVaporApp ,
5
6
defineComponent ,
6
7
getCurrentInstance ,
7
8
nextTick ,
8
9
ref ,
9
10
template ,
10
11
} from '../src'
11
- import { createSlots } from '../src/slot '
12
+ import { createSlots } from '../src/apiCreateSlots '
12
13
import { makeRender } from './_utils'
13
14
14
15
const define = makeRender < any > ( )
@@ -69,80 +70,65 @@ describe('component: slots', () => {
69
70
render (
70
71
{ } ,
71
72
createSlots ( {
72
- header : ( ) => {
73
- const t0 = template ( 'header' )
74
- // TODO: single node
75
- return [ t0 ( ) ]
76
- } ,
73
+ header : ( ) => template ( 'header' ) ( ) ,
77
74
} ) ,
78
75
)
79
- expect ( instance . slots . header ( ) ) . toMatchObject ( [
76
+ expect ( instance . slots . header ( ) ) . toMatchObject (
80
77
document . createTextNode ( 'header' ) ,
81
- ] )
78
+ )
82
79
} )
83
80
81
+ // TODO: test case name
84
82
test ( 'initSlots: instance.slots should be set correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)' , ( ) => {
85
83
const { slots } = renderWithSlots (
86
84
createSlots ( {
87
- // TODO: normalize from array
88
- default : ( ) => {
89
- const t0 = template ( '<span></span>' )
90
- return [ t0 ( ) ]
91
- } ,
85
+ // TODO: normalize from array?
86
+ default : ( ) => template ( '<span></span>' ) ( ) ,
92
87
} ) ,
93
88
)
94
89
95
- // TODO: warn
96
90
// expect(
97
91
// '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
98
92
// ).toHaveBeenWarned()
99
93
100
- expect ( slots . default ( ) ) . toMatchObject ( [ document . createElement ( 'span' ) ] )
94
+ expect ( slots . default ( ) ) . toMatchObject ( document . createElement ( 'span' ) )
101
95
} )
102
96
103
- // TODO: dynamic slot
104
- test . todo (
105
- 'updateSlots: instance.slots should be updated correctly' ,
106
- async ( ) => {
107
- const flag1 = ref ( true )
97
+ test ( 'updateSlots: instance.slots should be updated correctly' , async ( ) => {
98
+ const flag1 = ref ( true )
108
99
109
- let instance : any
110
- const Child = ( ) => {
111
- instance = getCurrentInstance ( )
112
- return template ( 'child' ) ( )
113
- }
100
+ let instance : any
101
+ const Child = ( ) => {
102
+ instance = getCurrentInstance ( )
103
+ return template ( 'child' ) ( )
104
+ }
114
105
115
- const { render } = define ( {
116
- render ( ) {
117
- return createComponent (
118
- Child ,
119
- { } ,
120
- createSlots ( {
121
- default : ( ) => {
122
- // TODO: dynamic slot
123
- return flag1 . value
124
- ? [ template ( '<span></span>' ) ( ) ]
125
- : [ template ( '<div></div>' ) ( ) ]
126
- } ,
127
- } ) ,
128
- )
129
- } ,
130
- } )
106
+ const { render } = define ( {
107
+ render ( ) {
108
+ return createComponent (
109
+ Child ,
110
+ { } ,
111
+ createSlots ( { _ : 2 as any } , ( ) => [
112
+ flag1 . value
113
+ ? { name : 'one' , fn : ( ) => template ( '<span></span>' ) ( ) }
114
+ : { name : 'two' , fn : ( ) => template ( '<div></div>' ) ( ) } ,
115
+ ] ) ,
116
+ )
117
+ } ,
118
+ } )
131
119
132
- render ( )
120
+ render ( )
133
121
134
- expect ( instance . slots . default ( ) ) . toMatchObject ( [ ] )
135
- expect ( instance . slots . default ( ) ) . not . toMatchObject ( [ ] )
122
+ expect ( instance . slots ) . toHaveProperty ( 'one' )
123
+ expect ( instance . slots ) . not . toHaveProperty ( 'two' )
136
124
137
- flag1 . value = false
138
- await nextTick ( )
125
+ flag1 . value = false
126
+ await nextTick ( )
139
127
140
- expect ( instance . slots . default ( ) ) . not . toMatchObject ( [ ] )
141
- expect ( instance . slots . default ( ) ) . toMatchObject ( [ ] )
142
- } ,
143
- )
128
+ expect ( instance . slots ) . not . toHaveProperty ( 'one' )
129
+ expect ( instance . slots ) . toHaveProperty ( 'two' )
130
+ } )
144
131
145
- // TODO: dynamic slots
146
132
test . todo (
147
133
'updateSlots: instance.slots should be updated correctly' ,
148
134
async ( ) => {
@@ -164,16 +150,15 @@ describe('component: slots', () => {
164
150
}
165
151
166
152
const { render } = define ( {
167
- render ( ) {
168
- const t0 = template ( '<div></div>' )
169
- const n0 = t0 ( )
170
- // renderComponent(
171
- // Child,
172
- // {},
173
- // createSlots(flag1.value ? oldSlots : newSlots),
174
- // n0 as ParentNode,
175
- // )
176
- return [ ]
153
+ setup ( ) {
154
+ return ( ( ) => {
155
+ return createComponent (
156
+ Child ,
157
+ { } ,
158
+ // TODO: maybe it is not supported
159
+ createSlots ( flag1 . value ? oldSlots : newSlots ) ,
160
+ )
161
+ } ) ( )
177
162
} ,
178
163
} )
179
164
@@ -188,56 +173,78 @@ describe('component: slots', () => {
188
173
} ,
189
174
)
190
175
191
- test . todo (
192
- 'updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)' ,
193
- async ( ) => {
194
- // TODO: dynamic slots
195
- } ,
196
- )
176
+ // TODO: test case name
177
+ test ( 'updateSlots: instance.slots should be update correctly (when vnode.shapeFlag is not SLOTS_CHILDREN)' , async ( ) => {
178
+ const flag1 = ref ( true )
179
+
180
+ let instance : any
181
+ const Child = ( ) => {
182
+ instance = getCurrentInstance ( )
183
+ return template ( 'child' ) ( )
184
+ }
185
+
186
+ const { render } = define ( {
187
+ setup ( ) {
188
+ return createComponent (
189
+ Child ,
190
+ { } ,
191
+ createSlots ( { } , ( ) => [
192
+ flag1 . value
193
+ ? [ { name : 'header' , fn : ( ) => template ( 'header' ) ( ) } ]
194
+ : [ { name : 'footer' , fn : ( ) => template ( 'footer' ) ( ) } ] ,
195
+ ] ) ,
196
+ )
197
+ } ,
198
+ } )
199
+ render ( )
200
+
201
+ expect ( instance . slots ) . toHaveProperty ( 'header' )
202
+ flag1 . value = false
203
+ await nextTick ( )
204
+
205
+ // expect(
206
+ // '[Vue warn]: Non-function value encountered for default slot. Prefer function slots for better performance.',
207
+ // ).toHaveBeenWarned()
208
+
209
+ expect ( instance . slots ) . toHaveProperty ( 'footer' )
210
+ } )
197
211
198
212
test . todo ( 'should respect $stable flag' , async ( ) => {
199
- // TODO: $stable flag
213
+ // TODO: $stable flag?
200
214
} )
201
215
202
216
test . todo ( 'should not warn when mounting another app in setup' , ( ) => {
203
- // TODO: warning and createApp fn
204
- // const Comp = {
205
- // render() {
206
- // const i = getCurrentInstance()
207
- // return i?.slots.default?.()
208
- // },
209
- // }
210
- // const mountComp = () => {
211
- // createApp({
212
- // render() {
213
- // const t0 = template('<div></div>')
214
- // const n0 = t0()
215
- // renderComponent(
216
- // Comp,
217
- // {},
218
- // createSlots({
219
- // default: () => {
220
- // const t0 = template('msg')
221
- // return [t0()]
222
- // },
223
- // }),
224
- // n0,
225
- // )
226
- // return n0
227
- // },
228
- // })
229
- // }
230
- // const App = {
231
- // setup() {
232
- // mountComp()
233
- // },
234
- // render() {
235
- // return null
236
- // },
237
- // }
238
- // createApp(App).mount(document.createElement('div'))
239
- // expect(
240
- // 'Slot "default" invoked outside of the render function',
241
- // ).not.toHaveBeenWarned()
217
+ // TODO: warning
218
+ const Comp = defineComponent ( {
219
+ render ( ) {
220
+ const i = getCurrentInstance ( )
221
+ return i ! . slots . default ! ( )
222
+ } ,
223
+ } )
224
+ const mountComp = ( ) => {
225
+ createVaporApp ( {
226
+ render ( ) {
227
+ return createComponent (
228
+ Comp ,
229
+ { } ,
230
+ createSlots ( {
231
+ default : ( ) => template ( 'msg' ) ( ) ,
232
+ } ) ,
233
+ ) !
234
+ } ,
235
+ } )
236
+ }
237
+ const App = {
238
+ setup ( ) {
239
+ mountComp ( )
240
+ } ,
241
+ render ( ) {
242
+ return null !
243
+ } ,
244
+ }
245
+ createVaporApp ( App ) . mount ( document . createElement ( 'div' ) )
246
+ expect (
247
+ 'Slot "default" invoked outside of the render function' ,
248
+ ) . not . toHaveBeenWarned ( )
242
249
} )
243
250
} )
0 commit comments