test: add utils unit tests
This commit is contained in:
parent
094b581949
commit
172e8edf78
44
utils/__tests__/colors.test.ts
Normal file
44
utils/__tests__/colors.test.ts
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
55
utils/__tests__/dates.test.ts
Normal file
55
utils/__tests__/dates.test.ts
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
17
utils/__tests__/strings.test.ts
Normal file
17
utils/__tests__/strings.test.ts
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -28,8 +28,8 @@ export const getNowDate = (): Date => {
|
|||||||
* Get the week number [1-52] for a given date.
|
* Get the week number [1-52] for a given date.
|
||||||
* @param {Date} date
|
* @param {Date} date
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
* @example getWeekNumber(new Date(2020, 0, 1)) // 1
|
* @example getWeekNumber(new Date("2020-01-01")) // 1
|
||||||
* @example getWeekNumber(new Date(2020, 0, 8)) // 2
|
* @example getWeekNumber(new Date("2020-01-08")) // 2
|
||||||
*/
|
*/
|
||||||
export const getWeekNumber = (date: Date): number => {
|
export const getWeekNumber = (date: Date): number => {
|
||||||
const dateCopy = new Date(date.getTime())
|
const dateCopy = new Date(date.getTime())
|
||||||
|
Reference in New Issue
Block a user