-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added unit test for segment utils (#118)
- Loading branch information
1 parent
fa6de56
commit 722da0b
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |