Skip to content

Commit

Permalink
Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonDB committed Aug 17, 2021
1 parent ab70475 commit bc19543
Showing 1 changed file with 14 additions and 0 deletions.
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 bc19543

Please sign in to comment.