Add first frontend tests for helpers.ts
This commit is contained in:
43
frontend/leeds-beer-quest/src/tests/helpers.spec.ts
Normal file
43
frontend/leeds-beer-quest/src/tests/helpers.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user