Skip to content

Commit b087ac3

Browse files
test[faustwp-core]: (#1940) add test coverage for sitemapIndexPath option (#2321)
* test[faustwp-core]: (#1940) add test coverage for sitemapIndexPath option Add three tests to createSitemaps.test.ts verifying sitemapIndexPath behavior in createRootSitemapIndex(): - Fetches from custom sitemapIndexPath when provided - Falls back to default /sitemap.xml when not provided - Trims leading/trailing slashes from sitemapIndexPath Closes #1940 * chore: add changeset for sitemapIndexPath test coverage * test[faustwp-core]: prettier formatting --------- Co-authored-by: latenighthackathon <latenighthackathon@users.noreply.github.com>
1 parent 103c79a commit b087ac3

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@faustwp/core": patch
3+
---
4+
5+
test[faustwp-core]: add test coverage for sitemapIndexPath option in createRootSitemapIndex

packages/faustwp-core/tests/server/sitemaps/createSitemaps.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,82 @@ describe('createRootSitemapIndex', () => {
242242

243243
expect(createSitemapIndexSpy).toHaveBeenCalledWith(expectedSitemaps);
244244
});
245+
246+
it('fetches from custom sitemapIndexPath when provided', async () => {
247+
const fetchSpy = jest
248+
.spyOn(global, 'fetch')
249+
.mockImplementationOnce((url) => {
250+
// Verify the custom path was used in the fetch URL
251+
expect(url).toBe('http://headless.local/custom-sitemap-index.xml');
252+
return Promise.resolve({
253+
ok: true,
254+
status: 200,
255+
text: () => Promise.resolve(validSitemapIndex1RecordXML),
256+
}) as Promise<Response>;
257+
});
258+
259+
const req = {
260+
url: 'http://localhost:3000/sitemap.xml',
261+
} as NextRequest;
262+
263+
const config: GetSitemapPropsConfig = {
264+
frontendUrl: 'http://localhost:3000',
265+
sitemapIndexPath: '/custom-sitemap-index.xml',
266+
};
267+
268+
await createSitemaps.createRootSitemapIndex(req, config);
269+
270+
expect(fetchSpy).toHaveBeenCalledWith(
271+
'http://headless.local/custom-sitemap-index.xml',
272+
);
273+
});
274+
275+
it('fetches from default /sitemap.xml when sitemapIndexPath is not provided', async () => {
276+
const fetchSpy = jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
277+
return Promise.resolve({
278+
ok: true,
279+
status: 200,
280+
text: () => Promise.resolve(validSitemapIndex1RecordXML),
281+
}) as Promise<Response>;
282+
});
283+
284+
const req = {
285+
url: 'http://localhost:3000/sitemap.xml',
286+
} as NextRequest;
287+
288+
const config: GetSitemapPropsConfig = {
289+
frontendUrl: 'http://localhost:3000',
290+
};
291+
292+
await createSitemaps.createRootSitemapIndex(req, config);
293+
294+
expect(fetchSpy).toHaveBeenCalledWith('http://headless.local/sitemap.xml');
295+
});
296+
297+
it('trims slashes from sitemapIndexPath', async () => {
298+
const fetchSpy = jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
299+
return Promise.resolve({
300+
ok: true,
301+
status: 200,
302+
text: () => Promise.resolve(validSitemapIndex1RecordXML),
303+
}) as Promise<Response>;
304+
});
305+
306+
const req = {
307+
url: 'http://localhost:3000/sitemap.xml',
308+
} as NextRequest;
309+
310+
const config: GetSitemapPropsConfig = {
311+
frontendUrl: 'http://localhost:3000',
312+
sitemapIndexPath: '/yoast-sitemap.xml/',
313+
};
314+
315+
await createSitemaps.createRootSitemapIndex(req, config);
316+
317+
expect(fetchSpy).toHaveBeenCalledWith(
318+
'http://headless.local/yoast-sitemap.xml',
319+
);
320+
});
245321
});
246322

247323
describe('createPagesSitemap()', () => {

0 commit comments

Comments
 (0)