Skip to content

Commit 74b9eb7

Browse files
committed
test: post template layout tests. closes #303
1 parent 99ff757 commit 74b9eb7

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed

site/tests/templates/post.test.js

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import Template, { Head } from '../../src/templates/post';
4+
import { useStaticQuery } from 'gatsby';
5+
6+
7+
jest.mock('../../src/components/footer', () => () => <nav data-testid="footer">Footer Mock</nav>);
8+
9+
// Mock sub-components so that you can inspect their props and rendered output.
10+
jest.mock('../../src/components/page-head', () => (props) => {
11+
return (
12+
<div
13+
data-testid="pagehead"
14+
data-title={props.title}
15+
data-description={props.description}
16+
data-image={props.image}
17+
data-tweet={props.tweet}
18+
data-readingTime={props.readingTime}
19+
data-words={props.words}
20+
>
21+
Page Head Mock
22+
</div>
23+
);
24+
});
25+
26+
// --- Setup our dummy site metadata for useStaticQuery ---
27+
const dummySiteMetadata = {
28+
defaultTitle: 'Default Title',
29+
description: 'Default Description',
30+
author: 'Test Author',
31+
siteUrl: 'http://example.com',
32+
};
33+
34+
// Mock useStaticQuery so that PageHead and Template get our dummy site metadata.
35+
beforeAll(() => {
36+
useStaticQuery.mockReturnValue({
37+
site: { siteMetadata: dummySiteMetadata },
38+
});
39+
});
40+
41+
// --- Dummy location ---
42+
const dummyLocation = {
43+
pathname: '/2021/08/05/test-post/',
44+
};
45+
46+
describe('Post.js Template', () => {
47+
48+
let dummyHeadData;
49+
50+
beforeEach(() => {
51+
// reset the dummy head data before each test.
52+
dummyHeadData = {
53+
markdownRemark: {
54+
frontmatter: {
55+
title: 'Test Head Post Title',
56+
excerpt: 'Test Head excerpt.',
57+
twitter_excerpt: 'Test Head Twitter excerpt.',
58+
featureimage: 'dummyFeatureImageData',
59+
},
60+
timeToRead: 5,
61+
wordCount: { words: 500 },
62+
},
63+
};
64+
});
65+
66+
67+
68+
describe('Head meta info export', () => {
69+
it('renders Head with the correct props', () => {
70+
render(<Head data={dummyHeadData} pageContext={{}} />);
71+
72+
// Check that the PageHead component rendered with the correct props.
73+
const pageHead = screen.getByTestId('pagehead');
74+
expect(pageHead).toBeInTheDocument();
75+
expect(pageHead).toHaveAttribute('data-title', 'Test Head Post Title');
76+
expect(pageHead).toHaveAttribute('data-description', 'Test Head excerpt.');
77+
expect(pageHead).toHaveAttribute('data-image', 'dummyFeatureImageData');
78+
expect(pageHead).toHaveAttribute('data-tweet', 'Test Head Twitter excerpt.');
79+
expect(pageHead).toHaveAttribute('data-readingTime', '5');
80+
expect(pageHead).toHaveAttribute('data-words', '500');
81+
});
82+
83+
it('handles excerpt on markdownRemark instead of frontmatter', () => {
84+
// Add excerpt to markdownRemark instead of frontmatter.
85+
const dummyHeadDataWithExcerpt = {
86+
markdownRemark: {
87+
...dummyHeadData.markdownRemark,
88+
excerpt: 'Test Head excerpt from markdownRemark.',
89+
},
90+
};
91+
92+
delete dummyHeadDataWithExcerpt.markdownRemark.frontmatter.excerpt;
93+
94+
render(<Head data={dummyHeadDataWithExcerpt} pageContext={{}} />);
95+
96+
// Check that the PageHead component rendered with the correct props.
97+
const pageHead = screen.getByTestId('pagehead');
98+
expect(pageHead).toBeInTheDocument();
99+
expect(pageHead).toHaveAttribute('data-description', 'Test Head excerpt from markdownRemark.');
100+
});
101+
102+
it('handles missing excerpt', () => {
103+
// Remove excerpt from frontmatter and markdownRemark.
104+
const dummyHeadDataWithoutExcerpt = {
105+
markdownRemark: {
106+
...dummyHeadData.markdownRemark,
107+
},
108+
};
109+
110+
delete dummyHeadDataWithoutExcerpt.markdownRemark.frontmatter.excerpt;
111+
delete dummyHeadDataWithoutExcerpt.markdownRemark.excerpt;
112+
113+
render(<Head data={dummyHeadDataWithoutExcerpt} pageContext={{}} />);
114+
115+
// Check that the PageHead component rendered with the correct props.
116+
const pageHead = screen.getByTestId('pagehead');
117+
expect(pageHead).toBeInTheDocument();
118+
expect(pageHead).toHaveAttribute('data-description', '');
119+
});
120+
121+
it('handles missing twitter_excerpt by using excerpt', () => {
122+
// Remove twitter_excerpt from frontmatter.
123+
const dummyHeadDataWithoutTwitterExcerpt = {
124+
markdownRemark: {
125+
...dummyHeadData.markdownRemark,
126+
},
127+
};
128+
129+
delete dummyHeadDataWithoutTwitterExcerpt.markdownRemark.frontmatter.twitter_excerpt;
130+
131+
render(<Head data={dummyHeadDataWithoutTwitterExcerpt} pageContext={{}} />);
132+
133+
// Check that the PageHead component rendered with the correct props.
134+
const pageHead = screen.getByTestId('pagehead');
135+
expect(pageHead).toBeInTheDocument();
136+
expect(pageHead).toHaveAttribute('data-description', 'Test Head excerpt.');
137+
expect(pageHead).toHaveAttribute('data-tweet', 'Test Head excerpt.');
138+
});
139+
140+
it('handles missing featureimage', () => {
141+
// Remove featureimage from frontmatter.
142+
const dummyHeadDataWithoutFeatureImage = {
143+
markdownRemark: {
144+
...dummyHeadData.markdownRemark,
145+
},
146+
};
147+
148+
delete dummyHeadDataWithoutFeatureImage.markdownRemark.frontmatter.featureimage;
149+
150+
render(<Head data={dummyHeadDataWithoutFeatureImage} pageContext={{}} />);
151+
152+
// Check that the PageHead component rendered with the correct props.
153+
const pageHead = screen.getByTestId('pagehead');
154+
expect(pageHead).toBeInTheDocument();
155+
expect(pageHead).toHaveAttribute('data-image', '');
156+
});
157+
158+
});
159+
160+
describe('Template export', () => {
161+
162+
let dummyTemplateData;
163+
164+
beforeEach(() => {
165+
// reset the dummy data before each test.
166+
dummyTemplateData = {
167+
markdownRemark: {
168+
frontmatter: {
169+
title: 'Test Post Title',
170+
date: "2021-08-05T00:00:00.000Z",
171+
excerpt: 'Test excerpt.',
172+
slug: 'test-post',
173+
small_title: false,
174+
large_title: true,
175+
featureimage: 'dummyFeatureImageData',
176+
imageby: 'Photographer Name',
177+
imagelink: 'http://example.com/image-link',
178+
},
179+
fields: {
180+
taglist: ['tag1', 'tag2'],
181+
excerpt: 'Fallback excerpt from fields.',
182+
},
183+
html: '<p>Test HTML content</p>',
184+
related: {
185+
post_similarity: [
186+
// Provide one or two dummy related posts (if needed)
187+
{ post: { frontmatter: { slug: 'related-1', title: 'Related 1', date: '2021-07-01', excerpt: 'Excerpt 1', listimage_position: '50% 50%', listimage: 'dummyRelatedImage1' } } },
188+
{ post: { frontmatter: { slug: 'related-2', title: 'Related 2', date: '2021-06-01', excerpt: 'Excerpt 2', listimage_position: '50% 50%', listimage: 'dummyRelatedImage2' } } },
189+
],
190+
},
191+
timeToRead: 6,
192+
wordCount: { words: 600 },
193+
},
194+
};
195+
});
196+
197+
it('renders Layout with the correct props and content', () => {
198+
// Render the Template component with dummy data and location.
199+
render(<Template data={dummyTemplateData} location={dummyLocation} />);
200+
201+
// check that the Layout component rendered HTML content.
202+
const contentSection = document.querySelector('section.content');
203+
expect(contentSection).toBeInTheDocument();
204+
expect(contentSection.innerHTML).toBe(dummyTemplateData.markdownRemark.html);
205+
206+
});
207+
208+
it('renders layout even if featureimage is missing', () => {
209+
// Remove featureimage from frontmatter.
210+
const dummyTemplateDataWithoutFeatureImage = {
211+
markdownRemark: {
212+
...dummyTemplateData.markdownRemark,
213+
},
214+
};
215+
216+
delete dummyTemplateDataWithoutFeatureImage.markdownRemark.frontmatter.featureimage;
217+
218+
// Render the Template component with dummy data and location.
219+
render(<Template data={dummyTemplateDataWithoutFeatureImage} location={dummyLocation} />);
220+
221+
// check that the Layout component rendered HTML content.
222+
const contentSection = document.querySelector('section.content');
223+
expect(contentSection).toBeInTheDocument();
224+
expect(contentSection.innerHTML).toBe(dummyTemplateData.markdownRemark.html);
225+
});
226+
227+
228+
});
229+
});
230+

0 commit comments

Comments
 (0)