From 172e8edf78f27b2b281591cc798f7b2beb4bcd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Wed, 1 May 2024 14:03:25 +0200 Subject: [PATCH] test: add utils unit tests --- utils/__tests__/colors.test.ts | 44 ++++++++++++++++++++++++++ utils/__tests__/dates.test.ts | 55 +++++++++++++++++++++++++++++++++ utils/__tests__/strings.test.ts | 17 ++++++++++ utils/dates.ts | 4 +-- 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 utils/__tests__/colors.test.ts create mode 100644 utils/__tests__/dates.test.ts create mode 100644 utils/__tests__/strings.test.ts diff --git a/utils/__tests__/colors.test.ts b/utils/__tests__/colors.test.ts new file mode 100644 index 0000000..2bbcad1 --- /dev/null +++ b/utils/__tests__/colors.test.ts @@ -0,0 +1,44 @@ +import { getColorRGBAFromHex } from "../colors" + +describe("utils/colors", () => { + describe("getColorRGBAFromHex", () => { + it("should return the correct rgba value when given a hex color and opacity (black 0)", () => { + // Arrange - Given + const hexColor = "#000000" + const opacity = 0 + + // Act - When + const result = getColorRGBAFromHex({ hexColor, opacity }) + + // Assert - Then + const expected = "rgba(0, 0, 0, 0)" + expect(result).toEqual(expected) + }) + + it("should return the correct rgba value when given a hex color and opacity (red 255)", () => { + // Arrange - Given + const hexColor = "#FF0000" + const opacity = 0.5 + + // Act - When + const result = getColorRGBAFromHex({ hexColor, opacity }) + + // Assert - Then + const expected = "rgba(255, 0, 0, 0.5)" + expect(result).toEqual(expected) + }) + + it("should return the correct rgba value when given a hex color with 3 characters and opacity (red 255)", () => { + // Arrange - Given + const hexColor = "#F00" + const opacity = 0.5 + + // Act - When + const result = getColorRGBAFromHex({ hexColor, opacity }) + + // Assert - Then + const expected = "rgba(255, 0, 0, 0.5)" + expect(result).toEqual(expected) + }) + }) +}) diff --git a/utils/__tests__/dates.test.ts b/utils/__tests__/dates.test.ts new file mode 100644 index 0000000..6f28037 --- /dev/null +++ b/utils/__tests__/dates.test.ts @@ -0,0 +1,55 @@ +import { getISODate, getWeekNumber } from "../dates" + +describe("utils/dates", () => { + describe("getISODate", () => { + it("should return the correct date in ISO format (e.g: 2012-05-23)", () => { + // Arrange - Given + const date = new Date("2012-05-23") + + // Act - When + const result = getISODate(date) + + // Assert - Then + const expected = "2012-05-23" + expect(result).toEqual(expected) + }) + }) + + describe("getWeekNumber", () => { + it("should return the correct week number for a given date (e.g: 2020-01-01)", () => { + // Arrange - Given + const date = new Date("2020-01-01") + + // Act - When + const result = getWeekNumber(date) + + // Assert - Then + const expected = 1 + expect(result).toEqual(expected) + }) + + it("should return the correct week number for a given date (e.g: 2020-01-08)", () => { + // Arrange - Given + const date = new Date("2020-01-08") + + // Act - When + const result = getWeekNumber(date) + + // Assert - Then + const expected = 2 + expect(result).toEqual(expected) + }) + + it("should return the correct week number for a given date (e.g: 2020-12-31)", () => { + // Arrange - Given + const date = new Date("2020-12-31") + + // Act - When + const result = getWeekNumber(date) + + // Assert - Then + const expected = 53 + expect(result).toEqual(expected) + }) + }) +}) diff --git a/utils/__tests__/strings.test.ts b/utils/__tests__/strings.test.ts new file mode 100644 index 0000000..4d9e331 --- /dev/null +++ b/utils/__tests__/strings.test.ts @@ -0,0 +1,17 @@ +import { capitalize } from "../strings" + +describe("utils/strings", () => { + describe("capitalize", () => { + it("should capitalize the first letter of a string", () => { + // Arrange - Given + const string = "hello world" + + // Act - When + const result = capitalize(string) + + // Assert - Then + const expected = "Hello world" + expect(result).toEqual(expected) + }) + }) +}) diff --git a/utils/dates.ts b/utils/dates.ts index 4b0124a..31ab1f4 100644 --- a/utils/dates.ts +++ b/utils/dates.ts @@ -28,8 +28,8 @@ export const getNowDate = (): Date => { * Get the week number [1-52] for a given date. * @param {Date} date * @returns {number} - * @example getWeekNumber(new Date(2020, 0, 1)) // 1 - * @example getWeekNumber(new Date(2020, 0, 8)) // 2 + * @example getWeekNumber(new Date("2020-01-01")) // 1 + * @example getWeekNumber(new Date("2020-01-08")) // 2 */ export const getWeekNumber = (date: Date): number => { const dateCopy = new Date(date.getTime())