Files
twenty/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts
Srikar Samudrala 4717f4cb90 fix(882): fixes negative number submission for employees input (#1130)
* fix(882): fixes negative number submission for employees input

* formatting

* fix linting
2023-08-09 22:25:57 -07:00

117 lines
3.7 KiB
TypeScript

import {
canBeCastAsPositiveIntegerOrNull,
castAsPositiveIntegerOrNull,
} from '~/utils/cast-as-positive-integer-or-null';
describe('canBeCastAsPositiveIntegerOrNull', () => {
it(`should return true if null`, () => {
expect(canBeCastAsPositiveIntegerOrNull(null)).toBeTruthy();
});
it(`should return true if positive number`, () => {
expect(canBeCastAsPositiveIntegerOrNull(9)).toBeTruthy();
});
it(`should return false if negative number`, () => {
expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy();
});
it(`should return false if zero`, () => {
expect(canBeCastAsPositiveIntegerOrNull(0)).toBeFalsy();
});
it(`should return false if string 0`, () => {
expect(canBeCastAsPositiveIntegerOrNull('0')).toBeFalsy();
});
it(`should return false if negative float`, () => {
expect(canBeCastAsPositiveIntegerOrNull(-1.22)).toBeFalsy();
});
it(`should return false if positive float`, () => {
expect(canBeCastAsPositiveIntegerOrNull(1.22)).toBeFalsy();
});
it(`should return false if positive float string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('0.9')).toBeFalsy();
});
it(`should return false if negative float string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('-0.9')).toBeFalsy();
});
it(`should return false if less than 1`, () => {
expect(canBeCastAsPositiveIntegerOrNull(0.22)).toBeFalsy();
});
it(`should return true if empty string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('')).toBeTruthy();
});
it(`should return true if integer string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('9')).toBeTruthy();
});
it(`should return false if undefined`, () => {
expect(canBeCastAsPositiveIntegerOrNull(undefined)).toBeFalsy();
});
it(`should return false if non numeric string`, () => {
expect(canBeCastAsPositiveIntegerOrNull('9a')).toBeFalsy();
});
it(`should return false if non numeric string #2`, () => {
expect(canBeCastAsPositiveIntegerOrNull('a9a')).toBeFalsy();
});
});
describe('castAsPositiveIntegerOrNull', () => {
it(`should cast null to null`, () => {
expect(castAsPositiveIntegerOrNull(null)).toBe(null);
});
it(`should cast empty string to null`, () => {
expect(castAsPositiveIntegerOrNull('')).toBe(null);
});
it(`should cast an integer to positive integer`, () => {
expect(castAsPositiveIntegerOrNull(9)).toBe(9);
});
it(`should cast an integer string to positive integer`, () => {
expect(castAsPositiveIntegerOrNull('9')).toBe(9);
});
it(`should throw if trying to cast a 0 to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(0)).toThrow(Error);
});
it(`should throw if trying to cast a string 0 to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('0')).toThrow(Error);
});
it(`should throw if trying to cast a positive float string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('9.9')).toThrow(Error);
});
it(`should throw if trying to cast a negative float string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('-9.9')).toThrow(Error);
});
it(`should throw if trying to cast a positive float to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(9.9)).toThrow(Error);
});
it(`should throw if trying to cast a negative float to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(-9.9)).toThrow(Error);
});
it(`should throw if trying to cast a non numeric string to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull('9.9a')).toThrow(Error);
});
it(`should throw if trying to cast an undefined to positive integer`, () => {
expect(() => castAsPositiveIntegerOrNull(undefined)).toThrow(Error);
});
});