|
| 1 | +import { DateRange } from "../../src/dateRange"; |
| 2 | +import { chunkMonthExtended } from "../../src/utils"; |
| 3 | +import { DateTime } from "luxon"; |
| 4 | +import { describe, expect, expectTypeOf, it, test } from "vitest"; |
| 5 | + |
| 6 | +describe("chunkMonthExtended", () => { |
| 7 | + describe("input validation", () => { |
| 8 | + test("throws an error if no arguments are specified", () => { |
| 9 | + // @ts-expect-error: testing invalid input |
| 10 | + expect(() => chunkMonthExtended()).toThrowError(); |
| 11 | + }); |
| 12 | + test("throws an error if dateRange argument isn't instance of DateRage", () => { |
| 13 | + // @ts-expect-error: testing invalid input |
| 14 | + expect(() => chunkMonthExtended("invalid")).toThrowError(); |
| 15 | + }); |
| 16 | + test("throws an error if dateRange argument is valid DateRange instance but generated range is not type MONTH-EXTENDED", () => { |
| 17 | + expect(() => |
| 18 | + chunkMonthExtended(new DateRange().getMonthExact()), |
| 19 | + ).toThrowError(); |
| 20 | + }); |
| 21 | + test.each(["invalid", 1, {}, []])( |
| 22 | + "throws an error if the second argument is not valid. Tested value: %j", |
| 23 | + (value) => { |
| 24 | + expect(() => |
| 25 | + chunkMonthExtended(new DateRange().getMonthExtended(), { |
| 26 | + // @ts-expect-error: testing invalid input |
| 27 | + nullNextPrevDates: value, |
| 28 | + }), |
| 29 | + ).toThrowError(); |
| 30 | + }, |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("functionality", () => { |
| 35 | + const dateStrings = [ |
| 36 | + "2023-01-01", |
| 37 | + "1990-05-24", |
| 38 | + "2015-12-31", |
| 39 | + "2008-07-14", |
| 40 | + "2021-04-26", |
| 41 | + "2003-09-11", |
| 42 | + "2019-10-30", |
| 43 | + "2006-02-28", |
| 44 | + "2014-06-15", |
| 45 | + "2020-08-08", |
| 46 | + ]; |
| 47 | + |
| 48 | + describe("With nullExtendedDates option set to false (default)", () => { |
| 49 | + describe.each(dateStrings)("with refDate: %s", (dateString) => { |
| 50 | + test("returns an array of DateTime arrays, each with a length of 7", () => { |
| 51 | + const chunk = chunkMonthExtended( |
| 52 | + new DateRange().getMonthExtended({ |
| 53 | + refDate: new Date(dateString), |
| 54 | + }), |
| 55 | + ); |
| 56 | + // check if the result is an array |
| 57 | + expectTypeOf(chunk).toBeArray(); |
| 58 | + // check if each item in the chunk is an array of 7 DateTime objects |
| 59 | + chunk.forEach((dates) => { |
| 60 | + expectTypeOf(dates).toBeArray(); |
| 61 | + expect(dates.length).toBe(7); |
| 62 | + dates.forEach((date) => { |
| 63 | + expect(date).toBeInstanceOf(DateTime); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test("chunk contains all dateTimes from the passed dateRange", () => { |
| 69 | + const dateRange = new DateRange().getMonthExtended({ |
| 70 | + refDate: new Date(dateString), |
| 71 | + }); |
| 72 | + const dateTimes = dateRange.toDateTimes(); |
| 73 | + const chunk = chunkMonthExtended(dateRange); |
| 74 | + |
| 75 | + let index = 0; |
| 76 | + // loop through the chunk array and compare each element with the corresponding element in the dateTimes array |
| 77 | + chunk.forEach((dates) => { |
| 78 | + dates.forEach((date) => { |
| 79 | + expect(date).toEqual(dateTimes[index]); |
| 80 | + index++; |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + // check if chunk has the same number of elements as the dateTimes |
| 85 | + expect(index).toBe(dateTimes.length); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("with nullExtendedDates option set to true", () => { |
| 91 | + describe.each(dateStrings)("with refDate: %s", (dateString) => { |
| 92 | + test("returns an array of DateTime or null arrays, each with a length of 7", () => { |
| 93 | + const chunk = chunkMonthExtended( |
| 94 | + new DateRange().getMonthExtended({ refDate: new Date(dateString) }), |
| 95 | + { nullNextPrevDates: true }, |
| 96 | + ); |
| 97 | + // check if the result is an array |
| 98 | + expectTypeOf(chunk).toBeArray(); |
| 99 | + // check if each item in the chunk is an array of 7 elements (DateTime or null) |
| 100 | + chunk.forEach((dates) => { |
| 101 | + expectTypeOf(dates).toBeArray(); |
| 102 | + expect(dates.length).toBe(7); |
| 103 | + dates.forEach((date) => { |
| 104 | + date === null |
| 105 | + ? expectTypeOf(date).toBeNull() |
| 106 | + : expect(date).toBeInstanceOf(DateTime); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + test("chunk contains only current month dates, previous and next dates are null", () => { |
| 112 | + const dateRange = new DateRange().getMonthExtended(); |
| 113 | + const currentMonth = dateRange.refDate.month; |
| 114 | + const dateTimes = dateRange.toDateTimes(); |
| 115 | + const chunk = chunkMonthExtended(dateRange, { |
| 116 | + nullNextPrevDates: true, |
| 117 | + }); |
| 118 | + |
| 119 | + // get the indexes of the dates that are not in the current month |
| 120 | + const getNextPrevIndexes = dateRange.dateTimes |
| 121 | + .map((dateTime, index) => { |
| 122 | + if (dateTime.month !== currentMonth) return index; |
| 123 | + }) |
| 124 | + .filter((index) => index !== undefined); |
| 125 | + |
| 126 | + let index = 0; |
| 127 | + // loop through the chunk array and compare each element with the corresponding element in the dateTimes array |
| 128 | + chunk.forEach((dates) => { |
| 129 | + dates.forEach((date) => { |
| 130 | + if (getNextPrevIndexes.includes(index)) { |
| 131 | + expect(date).toBeNull(); |
| 132 | + } else { |
| 133 | + expect(date).toEqual(dateTimes[index]); |
| 134 | + } |
| 135 | + index++; |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + // check if chunk has the same number of elements as the dateTimes |
| 140 | + expect(index).toBe(dateTimes.length); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments