Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added unit test for segment utils #118

Merged
merged 1 commit into from
Oct 27, 2020
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);
});
});
});