diff --git a/src/segment/__tests__/utils.test.ts b/src/segment/__tests__/utils.test.ts new file mode 100644 index 000000000..2293cc688 --- /dev/null +++ b/src/segment/__tests__/utils.test.ts @@ -0,0 +1,95 @@ +import { getSegmentLimits, setSegment, cycleValue } from "../__utils"; +import MockDate from "mockdate"; + +describe("Segment Utils", () => { + test("getSegmentLimits", () => { + MockDate.set(new Date("2020-02-01T11:30:00.000Z")); + + expect(getSegmentLimits(new Date(), "month", {})).toEqual({ + value: 2, + minValue: 1, + maxValue: 12, + }); + + expect(getSegmentLimits(new Date(), "year", {})).toEqual({ + value: 2020, + minValue: 1, + maxValue: 9999, + }); + + expect(getSegmentLimits(new Date(), "day", {})).toEqual({ + value: 1, + minValue: 1, + maxValue: 29, + }); + + expect(getSegmentLimits(new Date(), "dayPeriod", {})).toEqual({ + value: 12, + minValue: 0, + maxValue: 12, + }); + + expect(getSegmentLimits(new Date(), "minute", {})).toEqual({ + value: 0, + minValue: 0, + maxValue: 59, + }); + + expect(getSegmentLimits(new Date(), "second", {})).toEqual({ + value: 0, + minValue: 0, + maxValue: 59, + }); + }); + + test("setSegment", () => { + MockDate.set(new Date("2020-02-01T11:30:00.000Z")); + + const options = new Intl.DateTimeFormat().resolvedOptions(); + expect(setSegment(new Date(), "month", 5, options)).toEqual( + new Date("2020-05-01T11:30:00.000Z"), + ); + + expect(setSegment(new Date(), "day", 5, options)).toEqual( + new Date("2020-02-05T11:30:00.000Z"), + ); + + expect(setSegment(new Date(), "hour", 10, options)).toEqual( + new Date("2020-02-01T04:30:00.000Z"), + ); + + expect(setSegment(new Date(), "year", 2050, options)).toEqual( + new Date("2050-02-01T11:30:00.000Z"), + ); + }); + + describe("cycleValue", () => { + it("it should cycle one step", () => { + let value = 100; + value = cycleValue(value, 1, 0, 500, false); + + expect(value).toBe(101); + }); + + it("it should cycle back at when reached max value", () => { + let value = 100; + value = cycleValue(value, 1, 0, 100, false); + + expect(value).toBe(0); + }); + + it("it should cycle from minimum value if it's our of range", () => { + let value = 0; + value = cycleValue(value, 1, 50, 100, false); + + expect(value).toBe(52); + }); + + it("it should cycle with round", () => { + let value = 0; + value = cycleValue(value, 10, 0, 100, true); + + expect(value).toBe(10); + }); + }); +});