Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 9, 2022
1 parent 93bbc70 commit 1771d77
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import createSitemap from '../createSitemap';
import type {DocusaurusConfig} from '@docusaurus/types';
import {EnumChangefreq} from 'sitemap';
Expand All @@ -16,6 +17,7 @@ describe('createSitemap', () => {
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/test'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -29,7 +31,7 @@ describe('createSitemap', () => {

it('empty site', () =>
expect(async () => {
await createSitemap({} as DocusaurusConfig, [], {});
await createSitemap({} as DocusaurusConfig, [], {}, {});
}).rejects.toThrow(
'URL in docusaurus.config.js cannot be empty/undefined.',
));
Expand All @@ -40,6 +42,7 @@ describe('createSitemap', () => {
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/404.html', '/my-page'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -55,6 +58,7 @@ describe('createSitemap', () => {
url: 'https://example.com',
} as DocusaurusConfig,
['/', '/search/', '/tags/', '/search/foo', '/tags/foo/bar'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -78,6 +82,7 @@ describe('createSitemap', () => {
trailingSlash: undefined,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -98,6 +103,7 @@ describe('createSitemap', () => {
trailingSlash: true,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -118,6 +124,7 @@ describe('createSitemap', () => {
trailingSlash: false,
} as DocusaurusConfig,
['/', '/test', '/nested/test', '/nested/test2/'],
{},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
Expand All @@ -130,4 +137,30 @@ describe('createSitemap', () => {
expect(sitemap).toContain('<loc>https://example.com/nested/test</loc>');
expect(sitemap).toContain('<loc>https://example.com/nested/test2</loc>');
});

it('filters pages with noindex', async () => {
const sitemap = await createSitemap(
{
url: 'https://example.com',
trailingSlash: false,
} as DocusaurusConfig,
['/', '/noindex', '/nested/test', '/nested/test2/'],
{
'/noindex': {
meta: {
toComponent: () => [
React.createElement('meta', {name: 'robots', content: 'noindex'}),
],
},
},
},
{
changefreq: EnumChangefreq.DAILY,
priority: 0.7,
ignorePatterns: [],
},
);

expect(sitemap).not.toContain('/noindex');
});
});
6 changes: 4 additions & 2 deletions packages/docusaurus-plugin-sitemap/src/createSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default async function createSitemap(
return false;
}
// https://github.com/staylor/react-helmet-async/pull/167
const meta = helmet[route]?.meta.toComponent() as unknown as ReactElement[];
return !meta.some(
const meta = helmet[route]?.meta.toComponent() as unknown as
| ReactElement[]
| undefined;
return !meta?.some(
(tag) => tag.props.name === 'robots' && tag.props.content === 'noindex',
);
}
Expand Down

0 comments on commit 1771d77

Please sign in to comment.