Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/segment/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});