-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathpage-metadata.test.js
More file actions
137 lines (115 loc) · 5.04 KB
/
page-metadata.test.js
File metadata and controls
137 lines (115 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {render} from '@testing-library/react'
import Metadata from './page-metadata'
jest.mock('../../components/seo', () => {
return function MockSeo(props) {
return <div data-testid="seo" data-props={JSON.stringify(props)} />
}
})
describe('Metadata', () => {
test('returns null when no product is provided', () => {
const {container} = render(<Metadata />)
expect(container.firstChild).toBeNull()
})
test('returns null when product is null', () => {
const {container} = render(<Metadata product={null} />)
expect(container.firstChild).toBeNull()
})
test('renders Seo component with basic product data', () => {
const product = {
pageTitle: 'Test Product',
pageDescription: 'Test Description',
pageKeywords: 'test, product, keywords'
}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.title).toBe('Test Product')
expect(props.description).toBe('Test Description')
expect(props.keywords).toBe('test, product, keywords')
expect(props.metaTags).toEqual([])
})
test('uses pageMetaTags when available', () => {
const product = {
pageTitle: 'Test Product',
pageDescription: 'Default Description',
pageKeywords: 'default, keywords',
pageMetaTags: [
{id: 'description', value: 'Meta Tag Description'},
{id: 'keywords', value: 'meta, tag, keywords'},
{id: 'custom', value: 'custom value'}
]
}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.title).toBe('Test Product')
expect(props.description).toBe('Meta Tag Description')
expect(props.keywords).toBe('meta, tag, keywords')
expect(props.metaTags).toEqual(product.pageMetaTags)
})
test('falls back to product fields when pageMetaTags do not contain description or keywords', () => {
const product = {
pageTitle: 'Test Product',
pageDescription: 'Fallback Description',
pageKeywords: 'fallback, keywords',
pageMetaTags: [{id: 'custom', value: 'custom value'}]
}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.title).toBe('Test Product')
expect(props.description).toBe('Fallback Description')
expect(props.keywords).toBe('fallback, keywords')
expect(props.metaTags).toEqual(product.pageMetaTags)
})
test('handles missing product fields gracefully', () => {
const product = {}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.title).toBeUndefined()
expect(props.description).toBeUndefined()
expect(props.keywords).toBeUndefined()
expect(props.metaTags).toEqual([])
})
test('handles empty pageMetaTags array', () => {
const product = {
pageTitle: 'Test Product',
pageDescription: 'Test Description',
pageKeywords: 'test, keywords',
pageMetaTags: []
}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.title).toBe('Test Product')
expect(props.description).toBe('Test Description')
expect(props.keywords).toBe('test, keywords')
expect(props.metaTags).toEqual([])
})
test('prioritizes pageMetaTags over product fields for description and keywords', () => {
const product = {
pageTitle: 'Test Product',
pageDescription: 'Product Description',
pageKeywords: 'product, keywords',
pageMetaTags: [
{id: 'description', value: 'Meta Description'},
{id: 'keywords', value: 'meta, keywords'},
{id: 'other', value: 'other value'}
]
}
const {getByTestId} = render(<Metadata product={product} />)
const seoElement = getByTestId('seo')
const props = JSON.parse(seoElement.getAttribute('data-props'))
expect(props.description).toBe('Meta Description')
expect(props.keywords).toBe('meta, keywords')
expect(props.metaTags).toEqual(product.pageMetaTags)
})
})