Add first frontend tests for helpers.ts

This commit is contained in:
Stedoss
2022-11-01 21:06:07 +00:00
parent 45fef12590
commit ba4771614d
4 changed files with 9099 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
import { ArrayToArrayUrlParams } from "../lib/helpers";
import { expect, describe, it } from '@jest/globals';
const exampleParamName = "testParamName";
const exampleTestParams = ["one", "two", "three", "four", "five"];
describe("Given the ArrayToArrayUrlParams function", () => {
describe("and the param array is empty", () => {
it("should return empty string", () => {
const paramResult = ArrayToArrayUrlParams("testParam", []);
expect(paramResult).toHaveLength(0);
expect(paramResult).toBe("");
});
});
describe("and the params are not the first in the param line", () => {
it("should return & as the first char, not ?", () => {
const paramResult = ArrayToArrayUrlParams("testParam", exampleTestParams, false);
expect(paramResult[0]).toBe("&");
});
});
describe("and the param array has values", () => {
it("should not return a string ending with &", () => {
const paramResult = ArrayToArrayUrlParams("testParam", exampleTestParams);
expect(paramResult[paramResult.length - 1]).not.toBe("&");
});
it.each([
[exampleTestParams, `?${exampleParamName}=${exampleTestParams[0]}&${exampleParamName}=${exampleTestParams[1]}&${exampleParamName}=${exampleTestParams[2]}&${exampleParamName}=${exampleTestParams[3]}&${exampleParamName}=${exampleTestParams[4]}`],
[["a", "b", "c"], `?${exampleParamName}=a&${exampleParamName}=b&${exampleParamName}=c`],
[["onlyParam"], `?${exampleParamName}=onlyParam`]
])("should return a url parameter string with the included data", (params: string[], expectedResult: string) => {
const paramResult = ArrayToArrayUrlParams(exampleParamName, params);
expect(paramResult).toBe(expectedResult);
});
});
});