Last active
January 16, 2023 14:08
-
-
Save gokhantaskan/edcb16ff766393be62ba41d16c518343 to your computer and use it in GitHub Desktop.
testing date-related functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @jest-environment jsdom | |
* @jest-environment-options {"url": "https://website.com"} | |
*/ | |
import { defaultPeriod } from "../../src/utils/date"; | |
let windowSpy: ReturnType<typeof jest.spyOn>; | |
describe("test date.ts", () => { | |
describe("defaultPeriod()", () => { | |
beforeEach(() => { | |
windowSpy = jest.spyOn(window, "window", "get"); | |
}); | |
afterEach(() => { | |
windowSpy.mockRestore(); | |
}); | |
test("sets start to yesterday & end to currentMonthStart", () => { | |
const date = "2021-01-10"; | |
const start = "2021-01-01"; | |
const end = "2021-01-09"; | |
const search = window.location.search; | |
jest | |
.useFakeTimers() | |
.setSystemTime(new Date(date)); | |
if (!search) { | |
expect(defaultPeriod()).toMatchObject({ start, end }); | |
} | |
}); | |
test("sets the search params", () => { | |
const start = "2021-01-01"; | |
const end = "2021-01-10"; | |
windowSpy.mockImplementation(() => ({ | |
location: { | |
search: `?start=${start}&end=${end}`, | |
}, | |
})); | |
expect(defaultPeriod()).toMatchObject({ start, end }); | |
}); | |
test("sets the whole previous month if today is the first day of the month", () => { | |
const date = "2021-02-01"; | |
const start = "2021-01-01"; | |
const end = "2021-01-31"; | |
jest | |
.useFakeTimers() | |
.setSystemTime(new Date(date)); | |
expect(defaultPeriod()).toMatchObject({ start, end }); | |
}); | |
test("sets the first day of the month if today is the second day of the month", () => { | |
const date = "2021-01-02"; | |
const currentMonthStart = "2021-01-01"; | |
jest | |
.useFakeTimers() | |
.setSystemTime(new Date(date)); | |
expect(defaultPeriod()).toMatchObject({ start: currentMonthStart, end: currentMonthStart }); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dayjs from "dayjs"; | |
export function formatDate(date: Date | string, format = "ddd, D MMM YYYY, HH:mm Z"): string { | |
return dayjs(date).format(format); | |
} | |
export function representDate(date: Date | string, size: "month" | "day" | "hour" = "hour", includeTimezone = true): string { | |
switch (size) { | |
case "month": | |
return dayjs(date).format("MMM YYYY"); | |
case "day": | |
return dayjs(date).format("ddd, D MMM YYYY"); | |
case "hour": | |
return dayjs(date).format(`ddd, D MMM YYYY, HH:mm ${includeTimezone && "Z"}`); | |
default: | |
return dayjs(date).format(`ddd, D MMM YYYY, HH:mm ${includeTimezone && "Z"}`); | |
} | |
} | |
export function defaultPeriod() { | |
let period: { start: string, end: string }; | |
const dateFormat = "YYYY-MM-DD"; | |
const queryObject: Record<string, string> = {}; | |
const queryParams = window.location.search | |
.slice(1) // remove "?" from the query string | |
.split("&"); | |
queryParams.forEach(param => { | |
queryObject[param.split("=")[0]] = param.split("=")[1]; | |
}); | |
const today = dayjs().format(dateFormat); | |
const yesterday = dayjs().subtract(1, "day").format(dateFormat); | |
const currentMonthStart = dayjs().format("YYYY-MM-[01]"); | |
// ? Today is the first day of the current month | |
if (dayjs(today).valueOf() === dayjs(currentMonthStart).valueOf()) { | |
const prevMonthStart = dayjs(currentMonthStart).subtract(1, "month").format(dateFormat); | |
const prevMonthEnd = dayjs(currentMonthStart).subtract(1, "day").format(dateFormat); | |
period = { | |
start: prevMonthStart, | |
end: prevMonthEnd, | |
}; | |
// ? Yesterday was the first day of the current month | |
} else if (dayjs(yesterday).valueOf() === dayjs(currentMonthStart).valueOf()) { | |
period = { | |
start: currentMonthStart, | |
end: currentMonthStart, | |
}; | |
} else { | |
period = { | |
start: currentMonthStart, | |
end: yesterday, | |
}; | |
} | |
if ( | |
queryObject.start && | |
queryObject.end && | |
new Date(queryObject.start).getTime() <= new Date(queryObject.end).getTime() && | |
dayjs(queryObject.start, dateFormat, true).isValid() && | |
dayjs(queryObject.end, dateFormat, true).isValid() | |
) { | |
period = { | |
start: queryObject.start, | |
end: queryObject.end, | |
}; | |
} | |
return period; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment