Skip to content

Commit

Permalink
Merge pull request #2995 from dodona-edu/fix/url-creation
Browse files Browse the repository at this point in the history
Fix error when trying to create an URL if only path is given
  • Loading branch information
bmesuere authored Aug 18, 2021
2 parents a2db4a4 + bc19543 commit 3fd30f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/assets/javascripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const delay = (function () {
})();

function updateURLParameter(_url, param, paramVal) {
const url = new URL(_url);
const url = new URL(_url, window.location.origin);
if (paramVal) {
url.searchParams.set(param, paramVal);
} else {
Expand All @@ -26,7 +26,7 @@ function updateURLParameter(_url, param, paramVal) {

function updateArrayURLParameter(_url, param, _paramVals) {
const paramVals = new Set(_paramVals); // remove duplicate items
const url = new URL(_url);
const url = new URL(_url, window.location.origin);
url.searchParams.delete(param);
paramVals.forEach(paramVal => {
url.searchParams.append(param, paramVal);
Expand All @@ -35,12 +35,12 @@ function updateArrayURLParameter(_url, param, _paramVals) {
}

function getURLParameter(name, _url) {
const url = new URL(_url ?? window.location.href);
const url = new URL(_url ?? window.location.href, window.location.origin);
return url.searchParams.get(name);
}

function getArrayURLParameter(name, _url) {
const url = new URL(_url ?? window.location.href);
const url = new URL(_url ?? window.location.href, window.location.origin);
return url.searchParams.getAll(name);
}

Expand Down
14 changes: 14 additions & 0 deletions test/javascript/util.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { updateArrayURLParameter, updateURLParameter, getURLParameter, getArrayURLParameter } from "../../app/assets/javascripts/util";

let relativePath;
let relativePathParameter;
let noParameterURL;
let oneParameterURL;
let twoParameterURL;
let multipleValueUrl;

beforeEach(() => {
relativePath = "/test_functions";
relativePathParameter = "/test_functions?param=paramVal"
noParameterURL = "https://example.com/test_functions";
oneParameterURL = "https://example.com/test_functions?param1=paramVal1";
twoParameterURL = "https://example.com/test_functions?param1=paramVal1&param2=paramVal2";
multipleValueUrl = "https://example.com/test_functions?param=paramVal1&param=paramVal2&param=paramVal3";
});

test("return correct parameter value if present", () => {
expect(getURLParameter("param", relativePathParameter)).toBe("paramVal");
expect(getURLParameter("param1", oneParameterURL)).toBe("paramVal1");
expect(getURLParameter("param2", twoParameterURL)).toBe("paramVal2");
expect(getURLParameter("param", multipleValueUrl)).toBe("paramVal1");

expect(getArrayURLParameter("param", relativePathParameter)).toEqual(["paramVal"]);
expect(getArrayURLParameter("param1", twoParameterURL)).toEqual(["paramVal1"]);
expect(getArrayURLParameter("param", multipleValueUrl)).toEqual(["paramVal1", "paramVal2", "paramVal3"]);
});

test("return null or empty list when parameter not present", () => {
expect(getURLParameter("param", relativePath)).toBe(null);
expect(getURLParameter("param", noParameterURL)).toBe(null);
expect(getURLParameter("wrongParam", oneParameterURL)).toBe(null);

expect(getArrayURLParameter("param", relativePath)).toEqual([]);
expect(getArrayURLParameter("param", noParameterURL)).toEqual([]);
expect(getArrayURLParameter("wrongParam", twoParameterURL)).toEqual([]);
});
Expand All @@ -33,6 +41,9 @@ test("update URL parameter", () => {
let updatedURL;

// test updateURLParameter
updatedURL = updateURLParameter(relativePath, "param", "paramval")
expect(updatedURL).toEqual(`${window.location.origin}${relativePath}?param=paramval`);

updatedURL = updateURLParameter(noParameterURL, "param", "paramVal");
expect(updatedURL).toEqual(`${noParameterURL}?param=paramVal`);

Expand All @@ -49,6 +60,9 @@ test("update URL parameter", () => {
expect(updatedURL).toEqual(noParameterURL);

// test updateArrayURLParameter
updatedURL = updateArrayURLParameter(relativePath, "param", ["paramVal1", "paramVal1", "paramVal2"]);
expect(updatedURL).toEqual(`${window.location.origin}${relativePath}?param=paramVal1&param=paramVal2`)

updatedURL = updateArrayURLParameter(noParameterURL, "param", ["paramVal1", "paramVal1", "paramVal2", "paramVal3"]);
expect(updatedURL).toEqual(`${noParameterURL}?param=paramVal1&param=paramVal2&param=paramVal3`);

Expand Down

0 comments on commit 3fd30f7

Please sign in to comment.