1+ import type { JSX } from 'solid-js' ;
2+
3+ import { MetaProvider } from '@solidjs/meta' ;
14import { render } from '@solidjs/testing-library' ;
25import { expect , describe , test , vi } from 'vitest' ;
3- import { MetaProvider } from '@solidjs/meta' ;
4- import type { JSX } from 'solid-js' ;
6+
57import Head from './Head' ;
68
79// Mock the AppConfig to avoid import issues
@@ -11,91 +13,99 @@ vi.mock('@/constants/AppConfig', () => ({
1113 description : 'Test description' ,
1214 og : {
1315 image : {
14- url : 'https://example.com/default-og-image.jpg'
15- }
16- }
17- }
16+ url : 'https://example.com/default-og-image.jpg' ,
17+ } ,
18+ } ,
19+ } ,
1820} ) ) ;
1921
2022describe ( 'Head component' , ( ) => {
2123 const renderWithMeta = ( component : ( ) => JSX . Element ) => {
22- return render ( ( ) => (
23- < MetaProvider >
24- { component ( ) }
25- </ MetaProvider >
26- ) ) ;
24+ return render ( ( ) => < MetaProvider > { component ( ) } </ MetaProvider > ) ;
2725 } ;
2826
2927 test ( 'should render with default title and description' , ( ) => {
3028 const { unmount } = renderWithMeta ( ( ) => < Head /> ) ;
31-
29+
3230 // Check if document head contains the expected meta tags
3331 expect ( document . title ) . toBe ( 'Test Site' ) ;
34-
35- const metaDescription = document . querySelector ( 'meta[name="description"]' ) ;
32+
33+ // eslint-disable-next-line testing-library/no-node-access
34+ const metaDescription = document . head . querySelector (
35+ 'meta[name="description"]' ,
36+ ) ;
3637 expect ( metaDescription ?. getAttribute ( 'content' ) ) . toBe ( 'Test description' ) ;
37-
38+
3839 unmount ( ) ;
3940 } ) ;
4041
4142 test ( 'should render with custom title and description' , ( ) => {
4243 const customTitle = 'Custom Page' ;
4344 const customDescription = 'Custom page description' ;
44-
45+
4546 const { unmount } = renderWithMeta ( ( ) => (
4647 < Head title = { customTitle } description = { customDescription } />
4748 ) ) ;
48-
49+
4950 expect ( document . title ) . toBe ( 'Custom Page | Test Site' ) ;
50-
51- const metaDescription = document . querySelector ( 'meta[name="description"]' ) ;
51+
52+ // eslint-disable-next-line testing-library/no-node-access
53+ const metaDescription = document . head . querySelector (
54+ 'meta[name="description"]' ,
55+ ) ;
5256 expect ( metaDescription ?. getAttribute ( 'content' ) ) . toBe ( customDescription ) ;
53-
57+
5458 unmount ( ) ;
5559 } ) ;
5660
5761 test ( 'should use website type when canonical is undefined' , ( ) => {
5862 const { unmount } = renderWithMeta ( ( ) => < Head /> ) ;
59-
60- const ogType = document . querySelector ( 'meta[property="og:type"]' ) ;
63+
64+ // eslint-disable-next-line testing-library/no-node-access
65+ const ogType = document . head . querySelector ( 'meta[property="og:type"]' ) ;
6166 expect ( ogType ?. getAttribute ( 'content' ) ) . toBe ( 'website' ) ;
62-
67+
6368 unmount ( ) ;
6469 } ) ;
6570
6671 test ( 'should use article type when canonical is provided' , ( ) => {
6772 const { unmount } = renderWithMeta ( ( ) => < Head canonical = "/test" /> ) ;
68-
69- const ogType = document . querySelector ( 'meta[property="og:type"]' ) ;
73+
74+ // eslint-disable-next-line testing-library/no-node-access
75+ const ogType = document . head . querySelector ( 'meta[property="og:type"]' ) ;
7076 expect ( ogType ?. getAttribute ( 'content' ) ) . toBe ( 'article' ) ;
71-
77+
7278 unmount ( ) ;
7379 } ) ;
7480
7581 test ( 'should use custom OG image when provided' , ( ) => {
7682 const customOgImage = {
7783 og : {
7884 image : {
79- url : '/custom-image.jpg'
80- }
81- }
85+ url : '/custom-image.jpg' ,
86+ } ,
87+ } ,
8288 } ;
83-
89+
8490 const { unmount } = renderWithMeta ( ( ) => < Head { ...customOgImage } /> ) ;
85-
86- const ogImage = document . querySelector ( 'meta[property="og:image"]' ) ;
91+
92+ // eslint-disable-next-line testing-library/no-node-access
93+ const ogImage = document . head . querySelector ( 'meta[property="og:image"]' ) ;
8794 // The actual implementation prepends siteUrl, but since siteUrl is empty in test, it just returns the path
8895 expect ( ogImage ?. getAttribute ( 'content' ) ) . toContain ( 'custom-image.jpg' ) ;
89-
96+
9097 unmount ( ) ;
9198 } ) ;
9299
93100 test ( 'should use default OG image when custom image is not provided' , ( ) => {
94101 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-
102+
103+ // eslint-disable-next-line testing-library/no-node-access
104+ const ogImage = document . head . querySelector ( 'meta[property="og:image"]' ) ;
105+ expect ( ogImage ?. getAttribute ( 'content' ) ) . toBe (
106+ 'https://example.com/default-og-image.jpg' ,
107+ ) ;
108+
99109 unmount ( ) ;
100110 } ) ;
101- } ) ;
111+ } ) ;
0 commit comments