diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts index cf6e0f171cce..32518cc40ac9 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/extensionRedirects.test.ts @@ -9,46 +9,43 @@ import { createFromExtensionsRedirects, createToExtensionsRedirects, } from '../extensionRedirects'; -import {RedirectMetadata} from '../types'; -const createExtensionValidationTests = ( - extensionRedirectCreatorFn: ( - paths: string[], - extensions: string[], - ) => RedirectMetadata[], -) => { +describe('createToExtensionsRedirects', () => { test('should reject empty extensions', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['.html']); - }).toThrowErrorMatchingInlineSnapshot( - `"Extension ".html" contains a "." (dot) which is not allowed.\nIf the redirect extension system is not good enough for your usecase, you can create redirects yourself with the "createRedirects" plugin option."`, - ); + createToExtensionsRedirects(['/'], ['.html']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); }); + test('should reject extensions with .', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['.html']); - }).toThrowErrorMatchingInlineSnapshot( - `"Extension ".html" contains a "." (dot) which is not allowed.\nIf the redirect extension system is not good enough for your usecase, you can create redirects yourself with the "createRedirects" plugin option."`, - ); + createToExtensionsRedirects(['/'], ['.html']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); }); + test('should reject extensions with /', () => { expect(() => { - extensionRedirectCreatorFn(['/'], ['ht/ml']); - }).toThrowErrorMatchingInlineSnapshot( - `"Extension "ht/ml" contains a "/" (slash) which is not allowed.\nIf the redirect extension system is not good enough for your usecase, you can create redirects yourself with the "createRedirects" plugin option."`, - ); + createToExtensionsRedirects(['/'], ['ht/ml']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); }); + test('should reject extensions with illegal url char', () => { expect(() => { - extensionRedirectCreatorFn(['/'], [',']); - }).toThrowErrorMatchingInlineSnapshot( - `"Extension "," contains invalid URI characters.\nIf the redirect extension system is not good enough for your usecase, you can create redirects yourself with the "createRedirects" plugin option."`, - ); + createToExtensionsRedirects(['/'], [',']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\",\\" contains invalid URI characters. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); }); -}; - -describe('createToExtensionsRedirects', () => { - createExtensionValidationTests(createToExtensionsRedirects); test('should create redirects from html/htm extensions', () => { const ext = ['html', 'htm']; @@ -78,7 +75,41 @@ describe('createToExtensionsRedirects', () => { }); describe('createFromExtensionsRedirects', () => { - createExtensionValidationTests(createFromExtensionsRedirects); + test('should reject empty extensions', () => { + expect(() => { + createFromExtensionsRedirects(['/'], ['.html']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); + }); + + test('should reject extensions with .', () => { + expect(() => { + createFromExtensionsRedirects(['/'], ['.html']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\".html\\" contains a \\".\\" (dot) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); + }); + + test('should reject extensions with /', () => { + expect(() => { + createFromExtensionsRedirects(['/'], ['ht/ml']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\"ht/ml\\" contains a \\"/\\" (slash) which is not allowed. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); + }); + + test('should reject extensions with illegal url char', () => { + expect(() => { + createFromExtensionsRedirects(['/'], [',']); + }).toThrowErrorMatchingInlineSnapshot(` + "Extension \\",\\" contains invalid URI characters. + If the redirect extension system is not good enough for your usecase, you can create redirects yourself with the \\"createRedirects\\" plugin option." + `); + }); test('should create redirects to html/htm extensions', () => { const ext = ['html', 'htm'];