@@ -6,7 +6,11 @@ import {
66 transformVBind ,
77 transformVOn ,
88} from '../../src'
9- import { NodeTypes } from '@vue/compiler-core'
9+ import {
10+ type BindingMetadata ,
11+ BindingTypes ,
12+ NodeTypes ,
13+ } from '@vue/compiler-core'
1014
1115const compileWithElementTransform = makeCompile ( {
1216 nodeTransforms : [ transformElement , transformChildren ] ,
@@ -17,14 +21,182 @@ const compileWithElementTransform = makeCompile({
1721} )
1822
1923describe ( 'compiler: element transform' , ( ) => {
20- test . todo ( 'basic' )
24+ describe ( 'component' , ( ) => {
25+ test ( 'import + resolve component' , ( ) => {
26+ const { code, ir, vaporHelpers } = compileWithElementTransform ( `<Foo/>` )
27+ expect ( code ) . toMatchSnapshot ( )
28+ expect ( vaporHelpers ) . contains . all . keys (
29+ 'resolveComponent' ,
30+ 'createComponent' ,
31+ )
32+ expect ( ir . block . operation ) . toMatchObject ( [
33+ {
34+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
35+ id : 0 ,
36+ tag : 'Foo' ,
37+ resolve : true ,
38+ root : true ,
39+ props : [ [ ] ] ,
40+ } ,
41+ ] )
42+ } )
43+
44+ test . todo ( 'resolve implicitly self-referencing component' , ( ) => {
45+ const { code, vaporHelpers } = compileWithElementTransform ( `<Example/>` , {
46+ filename : `/foo/bar/Example.vue?vue&type=template` ,
47+ } )
48+ expect ( code ) . toMatchSnapshot ( )
49+ expect ( vaporHelpers ) . toContain ( 'resolveComponent' )
50+ } )
51+
52+ test ( 'resolve component from setup bindings' , ( ) => {
53+ const { code, ir, vaporHelpers } = compileWithElementTransform (
54+ `<Example/>` ,
55+ {
56+ bindingMetadata : {
57+ Example : BindingTypes . SETUP_MAYBE_REF ,
58+ } ,
59+ } ,
60+ )
61+ expect ( code ) . toMatchSnapshot ( )
62+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
63+ expect ( ir . block . operation ) . toMatchObject ( [
64+ {
65+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
66+ tag : 'Example' ,
67+ resolve : false ,
68+ } ,
69+ ] )
70+ } )
71+
72+ test ( 'resolve component from setup bindings (inline)' , ( ) => {
73+ const { code, vaporHelpers } = compileWithElementTransform ( `<Example/>` , {
74+ inline : true ,
75+ bindingMetadata : {
76+ Example : BindingTypes . SETUP_MAYBE_REF ,
77+ } ,
78+ } )
79+ expect ( code ) . toMatchSnapshot ( )
80+ expect ( code ) . contains ( `unref(Example)` )
81+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
82+ expect ( vaporHelpers ) . toContain ( 'unref' )
83+ } )
84+
85+ test ( 'resolve component from setup bindings (inline const)' , ( ) => {
86+ const { code, vaporHelpers } = compileWithElementTransform ( `<Example/>` , {
87+ inline : true ,
88+ bindingMetadata : {
89+ Example : BindingTypes . SETUP_CONST ,
90+ } ,
91+ } )
92+ expect ( code ) . toMatchSnapshot ( )
93+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
94+ } )
95+
96+ test ( 'resolve namespaced component from setup bindings' , ( ) => {
97+ const { code, vaporHelpers } = compileWithElementTransform (
98+ `<Foo.Example/>` ,
99+ {
100+ bindingMetadata : {
101+ Foo : BindingTypes . SETUP_MAYBE_REF ,
102+ } ,
103+ } ,
104+ )
105+ expect ( code ) . toMatchSnapshot ( )
106+ expect ( code ) . contains ( `_ctx.Foo.Example` )
107+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
108+ } )
109+
110+ test ( 'resolve namespaced component from setup bindings (inline const)' , ( ) => {
111+ const { code, vaporHelpers } = compileWithElementTransform (
112+ `<Foo.Example/>` ,
113+ {
114+ inline : true ,
115+ bindingMetadata : {
116+ Foo : BindingTypes . SETUP_CONST ,
117+ } ,
118+ } ,
119+ )
120+ expect ( code ) . toMatchSnapshot ( )
121+ expect ( code ) . contains ( `Foo.Example` )
122+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
123+ } )
124+
125+ test ( 'resolve namespaced component from props bindings (inline)' , ( ) => {
126+ const { code, vaporHelpers } = compileWithElementTransform (
127+ `<Foo.Example/>` ,
128+ {
129+ inline : true ,
130+ bindingMetadata : {
131+ Foo : BindingTypes . PROPS ,
132+ } ,
133+ } ,
134+ )
135+ expect ( code ) . toMatchSnapshot ( )
136+ expect ( code ) . contains ( `Foo.Example` )
137+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
138+ } )
139+
140+ test ( 'resolve namespaced component from props bindings (non-inline)' , ( ) => {
141+ const { code, vaporHelpers } = compileWithElementTransform (
142+ `<Foo.Example/>` ,
143+ {
144+ inline : false ,
145+ bindingMetadata : {
146+ Foo : BindingTypes . PROPS ,
147+ } ,
148+ } ,
149+ )
150+ expect ( code ) . toMatchSnapshot ( )
151+ expect ( code ) . contains ( '_ctx.Foo.Example' )
152+ expect ( vaporHelpers ) . not . toContain ( 'resolveComponent' )
153+ } )
154+
155+ test ( 'do not resolve component from non-script-setup bindings' , ( ) => {
156+ const bindingMetadata : BindingMetadata = {
157+ Example : BindingTypes . SETUP_MAYBE_REF ,
158+ }
159+ Object . defineProperty ( bindingMetadata , '__isScriptSetup' , {
160+ value : false ,
161+ } )
162+ const { code, ir, vaporHelpers } = compileWithElementTransform (
163+ `<Example/>` ,
164+ { bindingMetadata } ,
165+ )
166+ expect ( code ) . toMatchSnapshot ( )
167+ expect ( vaporHelpers ) . toContain ( 'resolveComponent' )
168+ expect ( ir . block . operation ) . toMatchObject ( [
169+ {
170+ type : IRNodeTypes . CREATE_COMPONENT_NODE ,
171+ id : 0 ,
172+ tag : 'Example' ,
173+ resolve : true ,
174+ } ,
175+ ] )
176+ } )
177+ } )
21178
22179 test ( 'static props' , ( ) => {
23180 const { code, ir } = compileWithElementTransform (
24181 `<div id="foo" class="bar" />` ,
25182 )
183+
184+ const template = '<div id="foo" class="bar"></div>'
185+ expect ( code ) . toMatchSnapshot ( )
186+ expect ( code ) . contains ( JSON . stringify ( template ) )
187+ expect ( ir . template ) . toMatchObject ( [ template ] )
188+ expect ( ir . block . effect ) . lengthOf ( 0 )
189+ } )
190+
191+ test ( 'props + children' , ( ) => {
192+ const { code, ir } = compileWithElementTransform (
193+ `<div id="foo"><span/></div>` ,
194+ )
195+
196+ const template = '<div id="foo"><span></span></div>'
26197 expect ( code ) . toMatchSnapshot ( )
27- expect ( code ) . contains ( '<div id=\\"foo\\" class=\\"bar\\"></div>"' )
198+ expect ( code ) . contains ( JSON . stringify ( template ) )
199+ expect ( ir . template ) . toMatchObject ( [ template ] )
28200 expect ( ir . block . effect ) . lengthOf ( 0 )
29201 } )
30202
@@ -220,7 +392,7 @@ describe('compiler: element transform', () => {
220392 )
221393 } )
222394
223- test . todo ( 'props merging: event handlers' , ( ) => {
395+ test ( 'props merging: event handlers' , ( ) => {
224396 const { code, ir } = compileWithElementTransform (
225397 `<div @click.foo="a" @click.bar="b" />` ,
226398 )
@@ -235,23 +407,31 @@ describe('compiler: element transform', () => {
235407 content : 'click' ,
236408 isStatic : true ,
237409 } ,
238- events : [
239- {
240- // IREvent: value, modifiers, keyOverride...
241- value : {
242- type : NodeTypes . SIMPLE_EXPRESSION ,
243- content : `a` ,
244- isStatic : false ,
245- } ,
246- } ,
247- {
248- value : {
249- type : NodeTypes . SIMPLE_EXPRESSION ,
250- content : `b` ,
251- isStatic : false ,
252- } ,
253- } ,
254- ] ,
410+ value : {
411+ type : NodeTypes . SIMPLE_EXPRESSION ,
412+ content : 'a' ,
413+ isStatic : false ,
414+ } ,
415+ keyOverride : undefined ,
416+ delegate : true ,
417+ effect : false ,
418+ } ,
419+ {
420+ type : IRNodeTypes . SET_EVENT ,
421+ element : 0 ,
422+ key : {
423+ type : NodeTypes . SIMPLE_EXPRESSION ,
424+ content : 'click' ,
425+ isStatic : true ,
426+ } ,
427+ value : {
428+ type : NodeTypes . SIMPLE_EXPRESSION ,
429+ content : 'b' ,
430+ isStatic : false ,
431+ } ,
432+ keyOverride : undefined ,
433+ delegate : true ,
434+ effect : false ,
255435 } ,
256436 ] )
257437 } )
0 commit comments