1+ import { render } from '@solidjs/testing-library' ;
2+ import { expect , describe , test , vi } from 'vitest' ;
3+ import { MetaProvider } from '@solidjs/meta' ;
4+ import type { JSX } from 'solid-js' ;
5+ import Head from './Head' ;
6+
7+ // Mock the AppConfig to avoid import issues
8+ vi . mock ( '@/constants/AppConfig' , ( ) => ( {
9+ AppConfig : {
10+ title : 'Test Site' ,
11+ description : 'Test description' ,
12+ og : {
13+ image : {
14+ url : 'https://example.com/default-og-image.jpg'
15+ }
16+ }
17+ }
18+ } ) ) ;
19+
20+ describe ( 'Head component' , ( ) => {
21+ const renderWithMeta = ( component : ( ) => JSX . Element ) => {
22+ return render ( ( ) => (
23+ < MetaProvider >
24+ { component ( ) }
25+ </ MetaProvider >
26+ ) ) ;
27+ } ;
28+
29+ test ( 'should render with default title and description' , ( ) => {
30+ const { unmount } = renderWithMeta ( ( ) => < Head /> ) ;
31+
32+ // Check if document head contains the expected meta tags
33+ expect ( document . title ) . toBe ( 'Test Site' ) ;
34+
35+ const metaDescription = document . querySelector ( 'meta[name="description"]' ) ;
36+ expect ( metaDescription ?. getAttribute ( 'content' ) ) . toBe ( 'Test description' ) ;
37+
38+ unmount ( ) ;
39+ } ) ;
40+
41+ test ( 'should render with custom title and description' , ( ) => {
42+ const customTitle = 'Custom Page' ;
43+ const customDescription = 'Custom page description' ;
44+
45+ const { unmount } = renderWithMeta ( ( ) => (
46+ < Head title = { customTitle } description = { customDescription } />
47+ ) ) ;
48+
49+ expect ( document . title ) . toBe ( 'Custom Page | Test Site' ) ;
50+
51+ const metaDescription = document . querySelector ( 'meta[name="description"]' ) ;
52+ expect ( metaDescription ?. getAttribute ( 'content' ) ) . toBe ( customDescription ) ;
53+
54+ unmount ( ) ;
55+ } ) ;
56+
57+ test ( 'should use website type when canonical is undefined' , ( ) => {
58+ const { unmount } = renderWithMeta ( ( ) => < Head /> ) ;
59+
60+ const ogType = document . querySelector ( 'meta[property="og:type"]' ) ;
61+ expect ( ogType ?. getAttribute ( 'content' ) ) . toBe ( 'website' ) ;
62+
63+ unmount ( ) ;
64+ } ) ;
65+
66+ test ( 'should use article type when canonical is provided' , ( ) => {
67+ const { unmount } = renderWithMeta ( ( ) => < Head canonical = "/test" /> ) ;
68+
69+ const ogType = document . querySelector ( 'meta[property="og:type"]' ) ;
70+ expect ( ogType ?. getAttribute ( 'content' ) ) . toBe ( 'article' ) ;
71+
72+ unmount ( ) ;
73+ } ) ;
74+
75+ test ( 'should use custom OG image when provided' , ( ) => {
76+ const customOgImage = {
77+ og : {
78+ image : {
79+ url : '/custom-image.jpg'
80+ }
81+ }
82+ } ;
83+
84+ const { unmount } = renderWithMeta ( ( ) => < Head { ...customOgImage } /> ) ;
85+
86+ const ogImage = document . querySelector ( 'meta[property="og:image"]' ) ;
87+ // The actual implementation prepends siteUrl, but since siteUrl is empty in test, it just returns the path
88+ expect ( ogImage ?. getAttribute ( 'content' ) ) . toContain ( 'custom-image.jpg' ) ;
89+
90+ unmount ( ) ;
91+ } ) ;
92+
93+ test ( 'should use default OG image when custom image is not provided' , ( ) => {
94+ const { unmount } = renderWithMeta ( ( ) => < Head /> ) ;
95+
96+ const ogImage = document . querySelector ( 'meta[property="og:image"]' ) ;
97+ expect ( ogImage ?. getAttribute ( 'content' ) ) . toBe ( 'https://example.com/default-og-image.jpg' ) ;
98+
99+ unmount ( ) ;
100+ } ) ;
101+ } ) ;
0 commit comments