Skip to content

Commit

Permalink
support short url and short path (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz2030 authored Nov 7, 2023
1 parent fec1311 commit 82f79a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/utils/constructChangesetUrl.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
export const constructChangesetUrl = (task) => {
if (
process.env.REACT_APP_CHANGESET_URL === "enabled" &&
task?.id &&
task?.parent?.id &&
task?.parent?.enabled &&
task?.parent?.parent?.enabled
) {
return ` ${window.location.origin}/browse/challenges/${task.parent.id}`;
const rootUrl = process.env.REACT_APP_SHORT_URL || window.location.origin
const path = process.env.REACT_APP_SHORT_PATH === 'enabled' ? `/c/${task.parent.id}/t/${task.id}` : `/challenge/${task.parent.id}/task/${task.id}`

return ` ${rootUrl}${path}`;
} else {
return "";
}
Expand Down
32 changes: 25 additions & 7 deletions src/utils/constructChangesetUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("constructChangesetUrl", () => {

beforeEach(() => {
jest.resetModules();
process.env = { ...cachedEnv, REACT_APP_CHANGESET_URL: "enabled" };
process.env = { ...cachedEnv, REACT_APP_CHANGESET_URL: "enabled", REACT_APP_SHORT_URL: '', REACT_APP_SHORT_PATH: 'disabled' };
});

afterAll(() => {
Expand All @@ -18,24 +18,42 @@ describe("constructChangesetUrl", () => {

it("returns empty string if env variable is disabled", () => {
process.env.REACT_APP_CHANGESET_URL = "disabled";
const task = { parent: { enabled: true, id: 1 } };
const task = { parent: { enabled: true, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe("");
});

it("returns empty string if challenge is not enabled", () => {
const task = { parent: { enabled: false, id: 1 } };
const task = { parent: { enabled: false, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe("");
});

it("returns empty string if project is not enabled", () => {
const task = { parent: { parent: { enabled: false }, id: 1 } };
const task = { parent: { parent: { enabled: false }, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe("");
});

it("returns correct url if challenge and project are enabled", () => {
const task = { parent: { enabled: true, parent: { enabled: true }, id: 1 } };
it("returns long url if challenge and project are enabled", () => {
const task = { parent: { enabled: true, parent: { enabled: true }, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe(
" http://localhost/browse/challenges/1"
" http://localhost/challenge/1/task/2"
);
});

it("returns short root url if REACT_APP_SHORT_URL is provided", () => {
process.env.REACT_APP_SHORT_URL = "mpr.lt";
process.env.REACT_APP_SHORT_PATH = "disabled";
const task = { parent: { enabled: true, parent: { enabled: true }, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe(
" mpr.lt/challenge/1/task/2"
);
});

it("returns short root url and short path if REACT_APP_SHORT_URL is provided and REACT_APP_SHORT_PATH is enabled", () => {
process.env.REACT_APP_SHORT_URL = "mpr.lt";
process.env.REACT_APP_SHORT_PATH = "enabled";
const task = { parent: { enabled: true, parent: { enabled: true }, id: 1 }, id: 2 };
expect(constructChangesetUrl(task)).toBe(
" mpr.lt/c/1/t/2"
);
});
});

0 comments on commit 82f79a1

Please sign in to comment.