From 1771d777758d26c6581d5ae2b9a863b7909dc40c Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 9 Apr 2022 22:29:24 +0800 Subject: [PATCH] add test --- .../src/__tests__/createSitemap.test.ts | 35 ++++++++++++++++++- .../src/createSitemap.ts | 6 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts index 193002e4bef1..c9008bd31004 100644 --- a/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts +++ b/packages/docusaurus-plugin-sitemap/src/__tests__/createSitemap.test.ts @@ -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'; @@ -16,6 +17,7 @@ describe('createSitemap', () => { url: 'https://example.com', } as DocusaurusConfig, ['/', '/test'], + {}, { changefreq: EnumChangefreq.DAILY, priority: 0.7, @@ -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.', )); @@ -40,6 +42,7 @@ describe('createSitemap', () => { url: 'https://example.com', } as DocusaurusConfig, ['/', '/404.html', '/my-page'], + {}, { changefreq: EnumChangefreq.DAILY, priority: 0.7, @@ -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, @@ -78,6 +82,7 @@ describe('createSitemap', () => { trailingSlash: undefined, } as DocusaurusConfig, ['/', '/test', '/nested/test', '/nested/test2/'], + {}, { changefreq: EnumChangefreq.DAILY, priority: 0.7, @@ -98,6 +103,7 @@ describe('createSitemap', () => { trailingSlash: true, } as DocusaurusConfig, ['/', '/test', '/nested/test', '/nested/test2/'], + {}, { changefreq: EnumChangefreq.DAILY, priority: 0.7, @@ -118,6 +124,7 @@ describe('createSitemap', () => { trailingSlash: false, } as DocusaurusConfig, ['/', '/test', '/nested/test', '/nested/test2/'], + {}, { changefreq: EnumChangefreq.DAILY, priority: 0.7, @@ -130,4 +137,30 @@ describe('createSitemap', () => { expect(sitemap).toContain('https://example.com/nested/test'); expect(sitemap).toContain('https://example.com/nested/test2'); }); + + 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'); + }); }); diff --git a/packages/docusaurus-plugin-sitemap/src/createSitemap.ts b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts index 27e2e4f6aa9f..d26152f8e820 100644 --- a/packages/docusaurus-plugin-sitemap/src/createSitemap.ts +++ b/packages/docusaurus-plugin-sitemap/src/createSitemap.ts @@ -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', ); }