added unit tests for ChangesetDiff url decision

This commit is contained in:
Sebastian Sdorra
2020-01-27 14:06:26 +01:00
parent 2dec396dff
commit 82f982305b
3 changed files with 102 additions and 18 deletions

View File

@@ -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} />;
}
}
}