Files
SCM-Manager/scm-ui/ui-components/src/MarkdownLinkRenderer.test.tsx

131 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-05-14 12:23:27 +02:00
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { isAnchorLink, isExternalLink, isLinkWithProtocol, createLocalLink } from "./MarkdownLinkRenderer";
2020-05-14 12:23:27 +02:00
describe("test isAnchorLink", () => {
it("should return true", () => {
expect(isAnchorLink("#some-thing")).toBe(true);
expect(isAnchorLink("#/some/more/complicated-link")).toBe(true);
});
2020-05-14 12:23:27 +02:00
it("should return false", () => {
expect(isAnchorLink("https://cloudogu.com")).toBe(false);
expect(isAnchorLink("/some/path/link")).toBe(false);
});
});
describe("test isExternalLink", () => {
it("should return true", () => {
expect(isExternalLink("https://cloudogu.com")).toBe(true);
expect(isExternalLink("http://cloudogu.com")).toBe(true);
});
it("should return false", () => {
expect(isExternalLink("some/path/link")).toBe(false);
expect(isExternalLink("/some/path/link")).toBe(false);
expect(isExternalLink("#some-anchor")).toBe(false);
expect(isExternalLink("mailto:trillian@hitchhiker.com")).toBe(false);
});
});
describe("test isLinkWithProtocol", () => {
it("should return true", () => {
expect(isLinkWithProtocol("ldap://[2001:db8::7]/c=GB?objectClass?one")).toBe(true);
expect(isLinkWithProtocol("mailto:trillian@hitchhiker.com")).toBe(true);
expect(isLinkWithProtocol("tel:+1-816-555-1212")).toBe(true);
expect(isLinkWithProtocol("urn:oasis:names:specification:docbook:dtd:xml:4.1.2")).toBe(true);
expect(isLinkWithProtocol("about:config")).toBe(true);
expect(isLinkWithProtocol("http://cloudogu.com")).toBe(true);
2020-05-20 11:18:58 +02:00
expect(isLinkWithProtocol("file:///srv/git/project.git")).toBe(true);
expect(isLinkWithProtocol("ssh://trillian@server/project.git")).toBe(true);
2020-05-15 09:55:51 +02:00
});
it("should return false", () => {
2020-05-20 11:18:58 +02:00
expect(isLinkWithProtocol("some/path/link")).toBe(false);
expect(isLinkWithProtocol("/some/path/link")).toBe(false);
expect(isLinkWithProtocol("#some-anchor")).toBe(false);
});
});
2020-05-15 09:55:51 +02:00
describe("test createLocalLink", () => {
it("should handle relative links", () => {
const localLink = createLocalLink("/src", "/src/README.md", "docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
});
it("should handle absolute links", () => {
const localLink = createLocalLink("/src", "/src/README.md", "/docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
});
it("should handle relative links from locations with trailing slash", () => {
const localLink = createLocalLink("/src", "/src/README.md/", "/docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
});
it("should handle relative links from location outside of base", () => {
const localLink = createLocalLink("/src", "/info/readme", "docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
2020-05-14 12:23:27 +02:00
});
it("should handle absolute links from location outside of base", () => {
const localLink = createLocalLink("/src", "/info/readme", "/docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
2020-05-14 12:23:27 +02:00
});
it("should handle relative links from sub directories", () => {
const localLink = createLocalLink("/src", "/src/docs/index.md", "installation/linux.md");
expect(localLink).toBe("/src/docs/installation/linux.md");
2020-05-14 12:23:27 +02:00
});
it("should handle absolute links from sub directories", () => {
const localLink = createLocalLink("/src", "/src/docs/index.md", "/docs/Home.md");
expect(localLink).toBe("/src/docs/Home.md");
2020-05-14 12:23:27 +02:00
});
it("should resolve .. with in path", () => {
const localLink = createLocalLink("/src", "/src/docs/installation/index.md", "../../README.md");
expect(localLink).toBe("/src/README.md");
});
2020-05-20 11:18:58 +02:00
it("should resolve .. to / if we reached the end", () => {
const localLink = createLocalLink("/", "/index.md", "../../README.md");
expect(localLink).toBe("/README.md");
});
it("should resolve . with in path", () => {
const localLink = createLocalLink("/src", "/src/README.md", "./LICENSE.md");
expect(localLink).toBe("/src/LICENSE.md");
});
2020-05-20 11:18:58 +02:00
it("should resolve . with the current directory", () => {
const localLink = createLocalLink("/", "/README.md", "././LICENSE.md");
expect(localLink).toBe("/LICENSE.md");
});
it("should handle complex path", () => {
const localLink = createLocalLink("/src", "/src/docs/installation/index.md", "./.././../docs/index.md");
expect(localLink).toBe("/src/docs/index.md");
});
2020-05-14 12:23:27 +02:00
});