From 60cb8b08793ec33eec9f84002a7acdbe621c47a6 Mon Sep 17 00:00:00 2001 From: Sebastian Qvarfordt Date: Tue, 30 Jun 2020 13:16:57 +0200 Subject: [PATCH] Added rewriteDocLinks test --- ...ener.test.tsx => addEventListener.test.ts} | 0 .../transformers/rewriteDocLinks.test.ts | 57 +++++++++++++++++++ .../reader/transformers/rewriteDocLinks.ts | 4 +- plugins/techdocs/src/test-utils/index.ts | 2 +- 4 files changed, 59 insertions(+), 4 deletions(-) rename plugins/techdocs/src/reader/transformers/{addEventListener.test.tsx => addEventListener.test.ts} (100%) create mode 100644 plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts diff --git a/plugins/techdocs/src/reader/transformers/addEventListener.test.tsx b/plugins/techdocs/src/reader/transformers/addEventListener.test.ts similarity index 100% rename from plugins/techdocs/src/reader/transformers/addEventListener.test.tsx rename to plugins/techdocs/src/reader/transformers/addEventListener.test.ts diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts new file mode 100644 index 0000000000..bcc45937b5 --- /dev/null +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.test.ts @@ -0,0 +1,57 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createTestShadowDom, getSample } from '../../test-utils'; +import { rewriteDocLinks } from '../transformers'; + +describe('rewriteDocLinks', () => { + it('contains relative paths', () => { + const shadowDom = createTestShadowDom(` + Test + Test + Test + Test Sub Page + `); + + expect(getSample(shadowDom, 'a', 'href', 6)).toEqual([ + 'http://example.org/', + '../example', + 'example-docs', + 'example-docs/example-page', + ]); + }); + + it('contains transformed absolute paths', () => { + const shadowDom = createTestShadowDom( + ` + Test + Test + Test + Test Sub Page + `, + { + transformers: [rewriteDocLinks()], + }, + ); + + expect(getSample(shadowDom, 'a', 'href', 6)).toEqual([ + 'http://example.org/', + 'http://localhost/example', + 'http://localhost/example-docs', + 'http://localhost/example-docs/example-page', + ]); + }); +}); diff --git a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts index 3f80dbe4e9..7b8495b6b7 100644 --- a/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts +++ b/plugins/techdocs/src/reader/transformers/rewriteDocLinks.ts @@ -17,9 +17,7 @@ import URLParser from '../urlParser'; import type { Transformer } from './index'; -type AddBaseUrlOptions = {}; - -export const rewriteDocLinks = ({}: AddBaseUrlOptions): Transformer => { +export const rewriteDocLinks = (): Transformer => { return dom => { const updateDom = ( list: Array, diff --git a/plugins/techdocs/src/test-utils/index.ts b/plugins/techdocs/src/test-utils/index.ts index 4cf9766b66..98472d75a4 100644 --- a/plugins/techdocs/src/test-utils/index.ts +++ b/plugins/techdocs/src/test-utils/index.ts @@ -51,8 +51,8 @@ export const getSample = ( shadowDom: ShadowRoot, elementName: string, elementAttribute: string, + sampleSize = 2, ) => { - const sampleSize = 2; const rootElement = shadowDom.children[0]; return Array.from(rootElement.getElementsByTagName(elementName))