@@ -2,8 +2,8 @@ import { parse } from 'acorn-loose'
2
2
import { describe , expect , it } from 'vitest'
3
3
import { UseSeoMetaTransform } from '../src/unplugin/UseSeoMetaTransform'
4
4
5
- async function transform ( code : string | string [ ] , id = 'some-id.js' ) {
6
- const plugin = UseSeoMetaTransform . vite ( ) as any
5
+ async function transform ( code : string | string [ ] , id = 'some-id.js' , opts : any = { } ) {
6
+ const plugin = UseSeoMetaTransform . vite ( opts ) as any
7
7
const res = await plugin . transform . call (
8
8
{ parse : ( code : string ) => parse ( code , { ecmaVersion : 2022 , sourceType : 'module' , allowImportExportEverywhere : true , allowAwaitOutsideFunction : true } ) } ,
9
9
Array . isArray ( code ) ? code . join ( '\n' ) : code ,
@@ -46,9 +46,8 @@ describe('useSeoMetaTransform', () => {
46
46
'useSeoMeta({ title: \'Hello 2\', description: \'World 2\' })' ,
47
47
] )
48
48
expect ( code ) . toMatchInlineSnapshot ( `
49
- "import { useHead } from 'unhead'
50
- import { something } from 'other-module'
51
- import { useSeoMeta } from 'unhead'
49
+ "import { something } from 'other-module'
50
+ import { useHead } from 'unhead'
52
51
useHead({
53
52
title: 'Hello',
54
53
meta: [
@@ -73,7 +72,6 @@ describe('useSeoMetaTransform', () => {
73
72
] )
74
73
expect ( code ) . toMatchInlineSnapshot ( `
75
74
"import { useHead } from 'unhead'
76
- import { useSeoMeta } from 'unhead'
77
75
import { ref } from 'vue'
78
76
const someValue = { value: 'test' }
79
77
useHead({
@@ -101,7 +99,6 @@ describe('useSeoMetaTransform', () => {
101
99
] )
102
100
expect ( code ) . toMatchInlineSnapshot ( `
103
101
"import { useHead } from '@unhead/vue'
104
- import { useSeoMeta } from '@unhead/vue'
105
102
useHead({
106
103
meta: [
107
104
{ charset: 'utf-8' },
@@ -126,7 +123,6 @@ describe('useSeoMetaTransform', () => {
126
123
] )
127
124
expect ( code ) . toMatchInlineSnapshot ( `
128
125
"import { useHead } from 'unhead'
129
- import { useSeoMeta } from 'unhead'
130
126
useHead({
131
127
meta: [
132
128
{ charset: 'utf-8' },
@@ -147,9 +143,31 @@ describe('useSeoMetaTransform', () => {
147
143
alt: 'My amazing image',
148
144
},
149
145
],
146
+ twitterImage: [
147
+ {
148
+ url: 'https://example.com/image.png',
149
+ width: 800,
150
+ height: 600,
151
+ alt: 'My amazing image',
152
+ },
153
+ ],
150
154
})` ,
151
155
] )
152
- expect ( code ) . toMatchInlineSnapshot ( 'undefined' )
156
+ expect ( code ) . toMatchInlineSnapshot ( `
157
+ "import { useHead } from 'unhead'
158
+ useHead({
159
+ meta: [
160
+ { property: 'og:image:url', content: 'https://example.com/image.png' },
161
+ { property: 'og:image:width', content: 800 },
162
+ { property: 'og:image:height', content: 600 },
163
+ { property: 'og:image:alt', content: 'My amazing image' },
164
+ { name: 'twitter:image:url', content: 'https://example.com/image.png' },
165
+ { name: 'twitter:image:width', content: 800 },
166
+ { name: 'twitter:image:height', content: 600 },
167
+ { name: 'twitter:image:alt', content: 'My amazing image' },
168
+ ]
169
+ })"
170
+ ` )
153
171
} )
154
172
155
173
it ( 'respects how users import library' , async ( ) => {
@@ -160,7 +178,6 @@ describe('useSeoMetaTransform', () => {
160
178
expect ( code ) . toBeDefined ( )
161
179
expect ( code ) . toMatchInlineSnapshot ( `
162
180
"import { useHead } from 'unhead'
163
- import { useSeoMeta as usm } from 'unhead'
164
181
useHead({
165
182
title: 'Hello',
166
183
meta: [
@@ -179,7 +196,6 @@ describe('useSeoMetaTransform', () => {
179
196
expect ( code ) . toBeDefined ( )
180
197
expect ( code ) . toMatchInlineSnapshot ( `
181
198
"import { useHead } from 'unhead'
182
- import { useSeoMeta as usm, useHead } from 'unhead'
183
199
useHead({ title: 'test', })
184
200
useHead({
185
201
meta: [
@@ -197,7 +213,6 @@ describe('useSeoMetaTransform', () => {
197
213
expect ( code ) . toBeDefined ( )
198
214
expect ( code ) . toMatchInlineSnapshot ( `
199
215
"import { useHead } from 'unhead'
200
- import { useSeoMeta } from 'unhead'
201
216
useHead({
202
217
meta: [
203
218
{ name: 'description', content: 'World' },
@@ -213,9 +228,7 @@ describe('useSeoMetaTransform', () => {
213
228
] )
214
229
expect ( code ) . toBeDefined ( )
215
230
expect ( code ) . toMatchInlineSnapshot ( `
216
- "import { useHead } from 'unhead'
217
- import { useServerHead } from 'unhead'
218
- import { useServerSeoMeta } from 'unhead'
231
+ "import { useServerHead } from 'unhead'
219
232
useServerHead({
220
233
meta: [
221
234
{ name: 'description', content: 'World' },
@@ -231,9 +244,7 @@ describe('useSeoMetaTransform', () => {
231
244
] )
232
245
expect ( code ) . toBeDefined ( )
233
246
expect ( code ) . toMatchInlineSnapshot ( `
234
- "import { useHead } from 'unhead'
235
- import { useServerHead } from 'unhead'
236
- import { useServerSeoMeta, useServerHead, useHead, SomethingRandom } from 'unhead'
247
+ "import { useServerHead, useHead, SomethingRandom } from 'unhead'
237
248
useHead({
238
249
title: 'Hello',
239
250
});
@@ -541,10 +552,9 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
541
552
542
553
expect ( code ) . toBeDefined ( )
543
554
expect ( code ) . toMatchInlineSnapshot ( `
544
- "import { useHead } from '@unhead/vue'
545
-
555
+ "
546
556
import { defineComponent as _defineComponent } from "vue";
547
- import { useHead, useSeoMeta } from "@unhead/vue";
557
+ import { useHead } from "@unhead/vue";
548
558
549
559
const _sfc_main = /* @__PURE__ */ _defineComponent({
550
560
__name: "app",
@@ -580,10 +590,9 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
580
590
581
591
expect ( code ) . toBeDefined ( )
582
592
expect ( code ) . toMatchInlineSnapshot ( `
583
- "import { useHead } from '@unhead/vue'
584
-
593
+ "
585
594
import { defineComponent as _defineComponent } from "vue";
586
- import { useSeoMeta as SEOMETA } from "@unhead/vue";
595
+ import { useHead } from "@unhead/vue";
587
596
588
597
const _sfc_main = /* @__PURE__ */ _defineComponent({
589
598
__name: "app",
@@ -597,4 +606,130 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
597
606
});"
598
607
` )
599
608
} )
609
+
610
+ it ( 'handles empty meta objects' , async ( ) => {
611
+ const code = await transform ( [
612
+ 'import { useSeoMeta } from \'unhead\'' ,
613
+ 'useSeoMeta({})' ,
614
+ ] )
615
+ expect ( code ) . toBeDefined ( )
616
+ expect ( code ) . toMatchInlineSnapshot ( `
617
+ "import { useHead } from 'unhead'
618
+ useHead({
619
+ })"
620
+ ` )
621
+ } )
622
+
623
+ it ( 'handles complex meta properties' , async ( ) => {
624
+ const code = await transform ( [
625
+ 'import { useSeoMeta } from \'unhead\'' ,
626
+ 'useSeoMeta({' ,
627
+ ' ogTitle: "My Page",' ,
628
+ ' ogDescription: "My Description",' ,
629
+ ' ogImage: "https://example.com/image.jpg",' ,
630
+ ' twitterCard: "summary_large_image"' ,
631
+ '})' ,
632
+ ] )
633
+ expect ( code ) . toBeDefined ( )
634
+ expect ( code ) . toMatchInlineSnapshot ( `
635
+ "import { useHead } from 'unhead'
636
+ useHead({
637
+ meta: [
638
+ { property: 'og:title', content: "My Page" },
639
+ { property: 'og:description', content: "My Description" },
640
+ { property: 'og:image', content: "https://example.com/image.jpg" },
641
+ { name: 'twitter:card', content: "summary_large_image" },
642
+ ]
643
+ })"
644
+ ` )
645
+ } )
646
+
647
+ it ( 'handles template literals' , async ( ) => {
648
+ const code = await transform ( [
649
+ 'import { useSeoMeta } from \'unhead\'' ,
650
+ 'const name = "World"' ,
651
+ 'useSeoMeta({' ,
652
+ ' title: `Hello ${name}`,' ,
653
+ ' description: `Welcome to ${name}`' ,
654
+ '})'
655
+ + 'console.log(useSeoMeta)' ,
656
+ ] )
657
+ expect ( code ) . toBeDefined ( )
658
+ expect ( code ) . toMatchInlineSnapshot ( `
659
+ "import { useHead, useSeoMeta } from 'unhead'
660
+ const name = "World"
661
+ useHead({
662
+ title: \`Hello \${name}\`,
663
+ meta: [
664
+ { name: 'description', content: \`Welcome to \${name}\` },
665
+ ]
666
+ })console.log(useSeoMeta)"
667
+ ` )
668
+ } )
669
+
670
+ it ( 'handles multiple imports and transformations' , async ( ) => {
671
+ const code = await transform ( [
672
+ 'import { useSeoMeta, useServerSeoMeta } from \'unhead\'' ,
673
+ 'useSeoMeta({ title: "Client" })' ,
674
+ 'useServerSeoMeta({ description: "Server" })' ,
675
+ ] )
676
+ expect ( code ) . toBeDefined ( )
677
+ expect ( code ) . toMatchInlineSnapshot ( `
678
+ "import { useHead, useServerHead } from 'unhead'
679
+ useHead({
680
+ title: "Client",
681
+ })
682
+ useServerHead({
683
+ meta: [
684
+ { name: 'description', content: "Server" },
685
+ ]
686
+ })"
687
+ ` )
688
+ } )
689
+
690
+ it ( 'handles conditional meta values' , async ( ) => {
691
+ const code = await transform ( [
692
+ 'import { useSeoMeta } from \'unhead\'' ,
693
+ 'const condition = true' ,
694
+ 'useSeoMeta({' ,
695
+ ' title: condition ? "True Title" : "False Title",' ,
696
+ ' description: condition && "Conditional Description"' ,
697
+ '})' ,
698
+ ] )
699
+ expect ( code ) . toBeDefined ( )
700
+ expect ( code ) . toMatchInlineSnapshot ( `
701
+ "import { useHead } from 'unhead'
702
+ const condition = true
703
+ useHead({
704
+ title: condition ? "True Title" : "False Title",
705
+ meta: [
706
+ { name: 'description', content: condition && "Conditional Description" },
707
+ ]
708
+ })"
709
+ ` )
710
+ } )
711
+
712
+ it ( 'handles #import' , async ( ) => {
713
+ const code = await transform ( [
714
+ 'import { useSeoMeta } from \'#imports\'' ,
715
+ 'const condition = true' ,
716
+ 'useSeoMeta({' ,
717
+ ' title: condition ? "True Title" : "False Title",' ,
718
+ ' description: condition && "Conditional Description"' ,
719
+ '})' ,
720
+ ] , 'some-id.js' , {
721
+ importSpecifiers : [ '#imports' ] ,
722
+ } )
723
+ expect ( code ) . toBeDefined ( )
724
+ expect ( code ) . toMatchInlineSnapshot ( `
725
+ "import { useHead } from '#imports'
726
+ const condition = true
727
+ useHead({
728
+ title: condition ? "True Title" : "False Title",
729
+ meta: [
730
+ { name: 'description', content: condition && "Conditional Description" },
731
+ ]
732
+ })"
733
+ ` )
734
+ } )
600
735
} )
0 commit comments