Skip to content

Commit

Permalink
improve tests for createSitemap / trailingSlash
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jun 29, 2021
1 parent 7ce8bf7 commit a491d7c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import createSitemap from '../createSitemap';
import {DocusaurusConfig} from '@docusaurus/types';
import {EnumChangefreq} from 'sitemap';

describe('createSitemap', () => {
test('simple site', async () => {
Expand All @@ -16,7 +17,7 @@ describe('createSitemap', () => {
} as DocusaurusConfig,
['/', '/test'],
{
changefreq: 'daily',
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
trailingSlash: false,
},
Expand All @@ -41,28 +42,87 @@ describe('createSitemap', () => {
} as DocusaurusConfig,
['/', '/404.html', '/mypage'],
{
changefreq: 'daily',
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
trailingSlash: false,
},
);
expect(sitemap).not.toContain('404');
});

test('keep trailing slash unchanged', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: undefined,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

expect(sitemap).toContain('<loc>https://example.com/</loc>');
expect(sitemap).toContain('<loc>https://example.com/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test2/</loc>');
});

test('add trailing slash', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: true,
} as DocusaurusConfig,
['/', '/test'],
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

expect(sitemap).toContain('<loc>https://example.com/</loc>');
expect(sitemap).toContain('<loc>https://example.com/test/</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test/</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test2/</loc>');
});

test('remove trailing slash', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: false,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
},
);

expect(sitemap).toContain('<loc>https://example.com/</loc>');
expect(sitemap).toContain('<loc>https://example.com/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test2</loc>');
});

test('add trailing slash (deprecated plugin option)', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{
changefreq: 'daily',
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
trailingSlash: true,
},
);

expect(sitemap).toContain('<loc>https://example.com/</loc>');
expect(sitemap).toContain('<loc>https://example.com/test/</loc>');
expect(sitemap).not.toContain('<loc>https://example.com/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test/</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test2/</loc>');
});
});
5 changes: 2 additions & 3 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default async function createSitemap(
throw new Error('URL in docusaurus.config.js cannot be empty/undefined.');
}
const {changefreq, priority} = options;
const trailingSlash = options.trailingSlash || siteConfig.trailingSlash;

const sitemapStream = new SitemapStream({
hostname,
Expand All @@ -30,10 +29,10 @@ export default async function createSitemap(
function applySitemapTrailingSlash(routePath: string): string {
// kept for retrocompatibility
// TODO remove deprecated trailingSlash option before 2022
if (trailingSlash) {
if (options.trailingSlash) {
return addTrailingSlash(routePath);
} else {
return applyTrailingSlash(routePath, trailingSlash);
return applyTrailingSlash(routePath, siteConfig.trailingSlash);
}
}

Expand Down

0 comments on commit a491d7c

Please sign in to comment.