mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
added unit tests for ChangesetDiff url decision
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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<Props> {
|
||||
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 <Notification type="danger">{t("changeset.diffNotSupported")}</Notification>;
|
||||
} else {
|
||||
const url = this.createUrl(changeset);
|
||||
return <LoadingDiff url={url} defaultCollapse={defaultCollapse} sideBySide={false}/>;
|
||||
const url = createUrl(changeset);
|
||||
return <LoadingDiff url={url} defaultCollapse={defaultCollapse} sideBySide={false} />;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user