Files
twenty/packages/twenty-front/src/utils/__tests__/cast-as-number-or-null.test.ts
gitstart-app[bot] 97eff774bd Allow input and display of floats for Number fields (#7340)
### Description

- We added a decimal field for a Number Field type in the settings
- We updated the Number Field type create a form with decimals input
- We are not implementing the dropdown present on the Figma because it
seems not related

### Demo


<https://www.loom.com/share/18a8d4b712a14f6d8b66806764f8467f?sid=3fc79b46-ae32-46e3-8635-d0eee02e53b2>

Fixes #6987

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: Marie Stoppa <marie.stoppa@essec.edu>
2024-10-04 10:45:25 +02:00

73 lines
2.0 KiB
TypeScript

import {
canBeCastAsNumberOrNull,
castAsNumberOrNull,
} from '../cast-as-number-or-null';
describe('canBeCastAsNumberOrNull', () => {
it(`should return true if null`, () => {
expect(canBeCastAsNumberOrNull(null)).toBeTruthy();
});
it(`should return true if number`, () => {
expect(canBeCastAsNumberOrNull(9)).toBeTruthy();
});
it(`should return true if empty string`, () => {
expect(canBeCastAsNumberOrNull('')).toBeTruthy();
});
it(`should return true if integer string`, () => {
expect(canBeCastAsNumberOrNull('9')).toBeTruthy();
});
it(`should return false if undefined`, () => {
expect(canBeCastAsNumberOrNull(undefined)).toBeFalsy();
});
it(`should return false if non numeric string`, () => {
expect(canBeCastAsNumberOrNull('9a')).toBeFalsy();
});
it(`should return false if non numeric string #2`, () => {
expect(canBeCastAsNumberOrNull('a9a')).toBeFalsy();
});
it(`should return true if float`, () => {
expect(canBeCastAsNumberOrNull(0.9)).toBeTruthy();
});
it(`should return true if float string`, () => {
expect(canBeCastAsNumberOrNull('0.9')).toBeTruthy();
});
});
describe('castAsNumberOrNull', () => {
it(`should cast null to null`, () => {
expect(castAsNumberOrNull(null)).toBe(null);
});
it(`should cast empty string to null`, () => {
expect(castAsNumberOrNull('')).toBe(null);
});
it(`should cast an integer to an integer`, () => {
expect(castAsNumberOrNull(9)).toBe(9);
});
it(`should cast an integer string to an integer`, () => {
expect(castAsNumberOrNull('9')).toBe(9);
});
it(`should throw if trying to cast a float string to an integer`, () => {
expect(castAsNumberOrNull('9.9')).toBe(9.9);
});
it(`should throw if trying to cast a non numeric string to an integer`, () => {
expect(() => castAsNumberOrNull('9.9a')).toThrow(Error);
});
it(`should throw if trying to cast an undefined to an integer`, () => {
expect(() => castAsNumberOrNull(undefined)).toThrow(Error);
});
});