Added rewriteDocLinks test
This commit is contained in:
@@ -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(`
|
||||
<a href="http://example.org/">Test</a>
|
||||
<a href="../example">Test</a>
|
||||
<a href="example-docs">Test</a>
|
||||
<a href="example-docs/example-page">Test Sub Page</a>
|
||||
`);
|
||||
|
||||
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(
|
||||
`
|
||||
<a href="http://example.org/">Test</a>
|
||||
<a href="../example">Test</a>
|
||||
<a href="example-docs">Test</a>
|
||||
<a href="example-docs/example-page">Test Sub Page</a>
|
||||
`,
|
||||
{
|
||||
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',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -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 = <T extends Element>(
|
||||
list: Array<T>,
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user