From 82f982305b98e9a470942fc97aa0d722c0a1a7ad Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 27 Jan 2020 14:06:26 +0100 Subject: [PATCH] added unit tests for ChangesetDiff url decision --- .../main/java/sonia/scm/web/VndMediaType.java | 2 +- .../repos/changesets/ChangesetDiff.test.ts | 84 +++++++++++++++++++ .../src/repos/changesets/ChangesetDiff.tsx | 34 ++++---- 3 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 scm-ui/ui-components/src/repos/changesets/ChangesetDiff.test.ts diff --git a/scm-core/src/main/java/sonia/scm/web/VndMediaType.java b/scm-core/src/main/java/sonia/scm/web/VndMediaType.java index f4a3d55878..b5d416b589 100644 --- a/scm-core/src/main/java/sonia/scm/web/VndMediaType.java +++ b/scm-core/src/main/java/sonia/scm/web/VndMediaType.java @@ -29,7 +29,7 @@ public class VndMediaType { public static final String BRANCH = PREFIX + "branch" + SUFFIX; public static final String BRANCH_REQUEST = PREFIX + "branchRequest" + SUFFIX; public static final String DIFF = PLAIN_TEXT_PREFIX + "diff" + PLAIN_TEXT_SUFFIX; - public static final String DIFF_PARSED = PREFIX + "diffParsed" + SUFFIX;; + public static final String DIFF_PARSED = PREFIX + "diffParsed" + SUFFIX; public static final String USER_COLLECTION = PREFIX + "userCollection" + SUFFIX; public static final String GROUP_COLLECTION = PREFIX + "groupCollection" + SUFFIX; public static final String REPOSITORY_COLLECTION = PREFIX + "repositoryCollection" + SUFFIX; diff --git a/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.test.ts b/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.test.ts new file mode 100644 index 0000000000..09918e7d85 --- /dev/null +++ b/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.test.ts @@ -0,0 +1,84 @@ +import { createUrl, isDiffSupported } from "./ChangesetDiff"; + +describe("isDiffSupported tests", () => { + it("should return true if diff link is defined", () => { + const supported = isDiffSupported({ + _links: { + diff: { + href: "http://diff" + } + } + }); + + expect(supported).toBe(true); + }); + + it("should return true if parsed diff link is defined", () => { + const supported = isDiffSupported({ + _links: { + diffParsed: { + href: "http://diff" + } + } + }); + + expect(supported).toBe(true); + }); + + it("should return false if not diff link was provided", () => { + const supported = isDiffSupported({ + _links: {} + }); + + expect(supported).toBe(false); + }); +}); + +describe("createUrl tests", () => { + it("should return the diff url, if only diff url is defined", () => { + const url = createUrl({ + _links: { + diff: { + href: "http://diff" + } + } + }); + + expect(url).toBe("http://diff?format=GIT"); + }); + + it("should return the diff parsed url, if only diff parsed url is defined", () => { + const url = createUrl({ + _links: { + diffParsed: { + href: "http://diff-parsed" + } + } + }); + + expect(url).toBe("http://diff-parsed"); + }); + + it("should return the diff parsed url, if both diff links are defined", () => { + const url = createUrl({ + _links: { + diff: { + href: "http://diff" + }, + diffParsed: { + href: "http://diff-parsed" + } + } + }); + + expect(url).toBe("http://diff-parsed"); + }); + + it("should throw an error if no diff link is defined", () => { + expect(() => + createUrl({ + _links: {} + }) + ).toThrow(); + }); +}); diff --git a/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.tsx b/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.tsx index c89658ce57..cc491224b7 100644 --- a/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.tsx +++ b/scm-ui/ui-components/src/repos/changesets/ChangesetDiff.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { Changeset, Link } from "@scm-manager/ui-types"; +import { Changeset, Link, Collection } from "@scm-manager/ui-types"; import LoadingDiff from "../LoadingDiff"; import Notification from "../../Notification"; import { WithTranslation, withTranslation } from "react-i18next"; @@ -9,27 +9,27 @@ type Props = WithTranslation & { defaultCollapse?: boolean; }; +export const isDiffSupported = (changeset: Collection) => { + return !!changeset._links.diff || !!changeset._links.diffParsed; +}; + +export const createUrl = (changeset: Collection) => { + if (changeset._links.diffParsed) { + return (changeset._links.diffParsed as Link).href; + } else if (changeset._links.diff) { + return (changeset._links.diff as Link).href + "?format=GIT"; + } + throw new Error("diff link is missing"); +}; + class ChangesetDiff extends React.Component { - isDiffSupported(changeset: Changeset) { - return changeset._links.diff || !!changeset._links.diffParsed; - } - - createUrl(changeset: Changeset) { - if (changeset._links.diffParsed) { - return (changeset._links.diffParsed as Link).href; - } else if (changeset._links.diff) { - return (changeset._links.diff as Link).href + "?format=GIT"; - } - throw new Error("diff link is missing"); - } - render() { const { changeset, defaultCollapse, t } = this.props; - if (!this.isDiffSupported(changeset)) { + if (!isDiffSupported(changeset)) { return {t("changeset.diffNotSupported")}; } else { - const url = this.createUrl(changeset); - return ; + const url = createUrl(changeset); + return ; } } }