Skip to content

Commit faf15d2

Browse files
committed
chore: Add tests for appendParams function and error checking
1 parent a661e9a commit faf15d2

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed

__tests__/appendParams.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { appendParams } from "../src/utils/appendParams"
2+
3+
describe("Util Functions (appendParams)", () => {
4+
it("should append parameters to the URL", () => {
5+
const URL = "https://example.com"
6+
const params = {
7+
param1: "value1",
8+
param2: 123,
9+
param3: true,
10+
param4: undefined,
11+
}
12+
const expectedURL = "https://example.com&param1=value1&param2=123&param3=1"
13+
const result = appendParams(URL, params)
14+
expect(result).toBe(expectedURL)
15+
})
16+
17+
it("should handle boolean parameters correctly", () => {
18+
const URL = "https://example.com"
19+
const params = {
20+
param1: true,
21+
param2: false,
22+
}
23+
const expectedURL = "https://example.com&param1=1&param2=0"
24+
const result = appendParams(URL, params)
25+
expect(result).toBe(expectedURL)
26+
})
27+
28+
it("should handle undefined parameters correctly", () => {
29+
const URL = "https://example.com"
30+
const params = {
31+
param1: "value1",
32+
param2: undefined,
33+
}
34+
const expectedURL = "https://example.com&param1=value1"
35+
const result = appendParams(URL, params)
36+
expect(result).toBe(expectedURL)
37+
})
38+
})

__tests__/errorChecking.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { dateParamErrorChecking } from "../src/utils/errorChecking"
2+
3+
describe("Util Functions (dateParamErrorChecking)", () => {
4+
const currentDate = new Date("2024-05-15T00:00:00Z")
5+
6+
it("should throw an error if start_range_end is less than start_range_begin", () => {
7+
const params = {
8+
start_range_begin: "2024-05-13T00:00:00Z",
9+
start_range_end: "2024-05-12T00:00:00Z", // One day before start_range_begin
10+
}
11+
expect(() => dateParamErrorChecking(params)).toThrow("start_range_end must be greater than start_range_begin.")
12+
})
13+
14+
it("should throw an error if start_range_begin is more than 90 days ago and start_range_end is omitted", () => {
15+
const ninetyOneDaysAgo = new Date(currentDate)
16+
ninetyOneDaysAgo.setDate(ninetyOneDaysAgo.getDate() - 91) // Adjusted to 91 days ago
17+
const params = {
18+
start_range_begin: ninetyOneDaysAgo.toISOString(),
19+
}
20+
21+
expect(() => dateParamErrorChecking(params)).toThrow(
22+
"start_range_begin must be within the last 90 days if start_range_end is omitted."
23+
)
24+
})
25+
26+
it("should throw an error if finish_range_end is less than finish_range_begin", () => {
27+
const params = {
28+
finish_range_begin: "2024-05-13T00:00:00Z",
29+
finish_range_end: "2024-05-12T00:00:00Z", // One day before finish_range_begin
30+
}
31+
expect(() => dateParamErrorChecking(params)).toThrow("finish_range_end must be greater than finish_range_begin.")
32+
})
33+
34+
it("should throw an error if finish_range_begin is more than 90 days ago and finish_range_end is omitted", () => {
35+
const ninetyOneDaysAgo = new Date(currentDate)
36+
ninetyOneDaysAgo.setDate(ninetyOneDaysAgo.getDate() - 91) // Adjusted to 91 days ago
37+
const params = {
38+
finish_range_begin: ninetyOneDaysAgo.toISOString(),
39+
}
40+
expect(() => dateParamErrorChecking(params)).toThrow(
41+
"finish_range_begin must be within the last 90 days if finish_range_end is omitted."
42+
)
43+
})
44+
45+
it("should return 'PASS' if all error checks pass", () => {
46+
const params = {
47+
start_range_begin: "2024-05-01T00:00:00Z",
48+
start_range_end: "2024-05-14T00:00:00Z", // One day after currentDate
49+
finish_range_begin: "2024-05-01T00:00:00Z",
50+
finish_range_end: "2024-05-14T00:00:00Z", // One day after currentDate
51+
}
52+
expect(dateParamErrorChecking(params)).toBe("PASS")
53+
})
54+
})

src/utils/errorChecking.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,50 @@ export const dateParamErrorChecking = ({
99
finish_range_begin?: string
1010
finish_range_end?: string
1111
}) => {
12-
// Error checking for start_range_begin and start_range_end
12+
const currentDate = new Date()
13+
const ninetyDaysAgo = new Date(currentDate)
14+
ninetyDaysAgo.setDate(ninetyDaysAgo.getDate() - 90)
15+
1316
if (start_range_begin && start_range_end) {
17+
if (!isValidDate(start_range_begin) || !isValidDate(start_range_end)) {
18+
throw new Error("Invalid date format provided for start_range_begin or start_range_end.")
19+
}
1420
const startBeginDate = new Date(start_range_begin)
1521
const startEndDate = new Date(start_range_end)
16-
const ninetyDaysAgo = new Date()
17-
ninetyDaysAgo.setDate(ninetyDaysAgo.getDate() - 90)
18-
1922
if (startBeginDate >= startEndDate) {
2023
throw new Error("start_range_end must be greater than start_range_begin.")
2124
}
25+
}
26+
27+
if (start_range_begin && !start_range_end) {
28+
const startBeginDate = new Date(start_range_begin)
2229
if (startBeginDate < ninetyDaysAgo) {
2330
throw new Error("start_range_begin must be within the last 90 days if start_range_end is omitted.")
2431
}
2532
}
2633

27-
// Error checking for finish_range_begin and finish_range_end
2834
if (finish_range_begin && finish_range_end) {
35+
if (!isValidDate(finish_range_begin) || !isValidDate(finish_range_end)) {
36+
throw new Error("Invalid date format provided for finish_range_begin or finish_range_end.")
37+
}
2938
const finishBeginDate = new Date(finish_range_begin)
3039
const finishEndDate = new Date(finish_range_end)
31-
const ninetyDaysAgo = new Date()
32-
ninetyDaysAgo.setDate(ninetyDaysAgo.getDate() - 90)
33-
3440
if (finishBeginDate >= finishEndDate) {
3541
throw new Error("finish_range_end must be greater than finish_range_begin.")
3642
}
43+
}
44+
45+
if (finish_range_begin && !finish_range_end) {
46+
const finishBeginDate = new Date(finish_range_begin)
3747
if (finishBeginDate < ninetyDaysAgo) {
3848
throw new Error("finish_range_begin must be within the last 90 days if finish_range_end is omitted.")
3949
}
4050
}
4151

4252
return "PASS"
4353
}
54+
55+
const isValidDate = (dateString: string) => {
56+
const regex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/ // ISO 8601 format without milliseconds and UTC timezone
57+
return regex.test(dateString) && !isNaN(Date.parse(dateString))
58+
}

0 commit comments

Comments
 (0)