react-native-timer-picker
Version:
A simple, flexible, performant duration picker for React Native apps 🔥 Great for timers, alarms and duration inputs ⏰🕰️⏳ Includes iOS-style haptic and audio feedback 🍏
497 lines (496 loc) • 12.1 kB
JavaScript
"use strict";
var _getSafeInitialValue = require("../utils/getSafeInitialValue");
describe("getSafeInitialValue", () => {
describe("valid values", () => {
it("returns all values when all are valid", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 30,
seconds: 45
});
});
it("handles zero values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
it("handles large values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 365,
hours: 23,
minutes: 59,
seconds: 59
});
expect(result).toEqual({
days: 365,
hours: 23,
minutes: 59,
seconds: 59
});
});
it("handles negative values as valid numbers", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: -5,
hours: -2,
minutes: -30,
seconds: -15
});
expect(result).toEqual({
days: -5,
hours: -2,
minutes: -30,
seconds: -15
});
});
it("handles decimal values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 1.5,
hours: 2.7,
minutes: 30.3,
seconds: 45.9
});
expect(result).toEqual({
days: 1.5,
hours: 2.7,
minutes: 30.3,
seconds: 45.9
});
});
});
describe("undefined input", () => {
it("returns all zeros when input is undefined", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)(undefined);
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
});
describe("partial values", () => {
it("defaults missing days to 0", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
hours: 10,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 0,
hours: 10,
minutes: 30,
seconds: 45
});
});
it("defaults missing hours to 0", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 45
});
});
it("defaults missing minutes to 0", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 0,
seconds: 45
});
});
it("defaults missing seconds to 0", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: 30
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 30,
seconds: 0
});
});
it("handles only days provided", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 7
});
expect(result).toEqual({
days: 7,
hours: 0,
minutes: 0,
seconds: 0
});
});
it("handles only hours provided", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
hours: 12
});
expect(result).toEqual({
days: 0,
hours: 12,
minutes: 0,
seconds: 0
});
});
it("handles only minutes provided", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
minutes: 45
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 45,
seconds: 0
});
});
it("handles only seconds provided", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
seconds: 30
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 30
});
});
it("handles empty object", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
});
describe("invalid values - NaN", () => {
it("defaults days to 0 when NaN", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: NaN,
hours: 10,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 0,
hours: 10,
minutes: 30,
seconds: 45
});
});
it("defaults hours to 0 when NaN", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: NaN,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 45
});
});
it("defaults minutes to 0 when NaN", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: NaN,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 0,
seconds: 45
});
});
it("defaults seconds to 0 when NaN", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: 30,
seconds: NaN
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 30,
seconds: 0
});
});
it("defaults all to 0 when all are NaN", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: NaN,
hours: NaN,
minutes: NaN,
seconds: NaN
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
});
describe("invalid values - wrong types", () => {
it("defaults days to 0 when string", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: "5",
hours: 10,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 0,
hours: 10,
minutes: 30,
seconds: 45
});
});
it("defaults hours to 0 when string", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: "10",
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 45
});
});
it("defaults minutes to 0 when boolean", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: true,
seconds: 45
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 0,
seconds: 45
});
});
it("defaults seconds to 0 when null", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: 10,
minutes: 30,
seconds: null
});
expect(result).toEqual({
days: 5,
hours: 10,
minutes: 30,
seconds: 0
});
});
it("defaults all to 0 when all are invalid types", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: "invalid",
hours: true,
minutes: null,
seconds: undefined
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
it("defaults values to 0 when objects", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: {},
hours: [],
minutes: {
value: 30
},
seconds: [45]
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 0
});
});
});
describe("mixed valid and invalid values", () => {
it("handles mix of valid and NaN values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: NaN,
minutes: 30,
seconds: NaN
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 0
});
});
it("handles mix of valid and undefined values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
minutes: 30
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 0
});
});
it("handles mix of valid and invalid type values", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 5,
hours: "invalid",
minutes: 30,
seconds: null
});
expect(result).toEqual({
days: 5,
hours: 0,
minutes: 30,
seconds: 0
});
});
});
describe("special number values", () => {
it("handles Infinity", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: Infinity,
hours: -Infinity,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: Infinity,
hours: -Infinity,
minutes: 30,
seconds: 45
});
});
it("handles very small numbers", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 0.00001,
hours: 0.00002,
minutes: 0.00003,
seconds: 0.00004
});
expect(result).toEqual({
days: 0.00001,
hours: 0.00002,
minutes: 0.00003,
seconds: 0.00004
});
});
it("handles very large numbers", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 999999,
hours: 999999,
minutes: 999999,
seconds: 999999
});
expect(result).toEqual({
days: 999999,
hours: 999999,
minutes: 999999,
seconds: 999999
});
});
});
describe("real-world scenarios", () => {
it("handles typical timer value", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
hours: 2,
minutes: 30,
seconds: 0
});
expect(result).toEqual({
days: 0,
hours: 2,
minutes: 30,
seconds: 0
});
});
it("handles countdown timer", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
minutes: 5,
seconds: 0
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 5,
seconds: 0
});
});
it("handles duration with days", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: 7,
hours: 12,
minutes: 30,
seconds: 45
});
expect(result).toEqual({
days: 7,
hours: 12,
minutes: 30,
seconds: 45
});
});
it("handles invalid user input gracefully", () => {
const result = (0, _getSafeInitialValue.getSafeInitialValue)({
days: parseInt("abc"),
hours: parseInt(""),
minutes: parseFloat("not a number"),
seconds: 30
});
expect(result).toEqual({
days: 0,
hours: 0,
minutes: 0,
seconds: 30
});
});
});
});
//# sourceMappingURL=getSafeInitialValue.test.js.map