|
| 1 | +import { describe, it, expect } from "@jest/globals"; |
| 2 | +import { getDateRangeSeries } from "./getDateRangeSeries"; |
| 3 | + |
| 4 | +describe("getDateRangeSeries", () => { |
| 5 | + it("should return a series of dates as ISO strings for days", () => { |
| 6 | + const startDate = new Date("2024-01-01"); |
| 7 | + const endDate = new Date("2024-01-05"); |
| 8 | + const unit = "days"; |
| 9 | + |
| 10 | + const result = getDateRangeSeries({ startDate, endDate }, unit); |
| 11 | + |
| 12 | + expect(result).toEqual([ |
| 13 | + "2024-01-01T00:00:00.000Z", |
| 14 | + "2024-01-02T00:00:00.000Z", |
| 15 | + "2024-01-03T00:00:00.000Z", |
| 16 | + "2024-01-04T00:00:00.000Z", |
| 17 | + ]); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should handle a series for hours", () => { |
| 21 | + const startDate = new Date("2024-01-01T00:00:00.000Z"); |
| 22 | + const endDate = new Date("2024-01-01T03:00:00.000Z"); |
| 23 | + const unit = "hours"; |
| 24 | + |
| 25 | + const result = getDateRangeSeries({ startDate, endDate }, unit); |
| 26 | + |
| 27 | + expect(result).toEqual([ |
| 28 | + "2024-01-01T00:00:00.000Z", |
| 29 | + "2024-01-01T01:00:00.000Z", |
| 30 | + "2024-01-01T02:00:00.000Z", |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should handle a series for minutes", () => { |
| 35 | + const startDate = new Date("2024-01-01T00:00:00.000Z"); |
| 36 | + const endDate = new Date("2024-01-01T00:05:00.000Z"); |
| 37 | + const unit = "minutes"; |
| 38 | + |
| 39 | + const result = getDateRangeSeries({ startDate, endDate }, unit); |
| 40 | + |
| 41 | + expect(result).toEqual([ |
| 42 | + "2024-01-01T00:00:00.000Z", |
| 43 | + "2024-01-01T00:01:00.000Z", |
| 44 | + "2024-01-01T00:02:00.000Z", |
| 45 | + "2024-01-01T00:03:00.000Z", |
| 46 | + "2024-01-01T00:04:00.000Z", |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle a series for seconds", () => { |
| 51 | + const startDate = new Date("2024-01-01T00:00:00.000Z"); |
| 52 | + const endDate = new Date("2024-01-01T00:00:05.000Z"); |
| 53 | + const unit = "seconds"; |
| 54 | + |
| 55 | + const result = getDateRangeSeries({ startDate, endDate }, unit); |
| 56 | + |
| 57 | + expect(result).toEqual([ |
| 58 | + "2024-01-01T00:00:00.000Z", |
| 59 | + "2024-01-01T00:00:01.000Z", |
| 60 | + "2024-01-01T00:00:02.000Z", |
| 61 | + "2024-01-01T00:00:03.000Z", |
| 62 | + "2024-01-01T00:00:04.000Z", |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should throw an error if startDate is after endDate", () => { |
| 67 | + const startDate = new Date("2024-01-05"); |
| 68 | + const endDate = new Date("2024-01-01"); |
| 69 | + const unit = "days"; |
| 70 | + |
| 71 | + expect(() => getDateRangeSeries({ startDate, endDate }, unit)).toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should handle a single date range", () => { |
| 75 | + const startDate = new Date("2024-01-01T00:00:00.000Z"); |
| 76 | + const endDate = new Date("2024-01-01T00:00:00.000Z"); |
| 77 | + const unit = "days"; |
| 78 | + |
| 79 | + const result = getDateRangeSeries({ startDate, endDate }, unit); |
| 80 | + |
| 81 | + expect(result).toEqual([]); |
| 82 | + }); |
| 83 | +}); |
0 commit comments