Add jest tests for twenty-front (#2983)
* Add jest tests for twenty-front Co-authored-by: v1b3m <vibenjamin6@gmail.com> * Fix tests --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
19
packages/twenty-front/src/utils/__tests__/assert.test.ts
Normal file
19
packages/twenty-front/src/utils/__tests__/assert.test.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { assertNotNull } from '~/utils/assert';
|
||||
|
||||
describe('assert', () => {
|
||||
it('should return true for a NonNullable value', () => {
|
||||
expect(assertNotNull(1)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return true for a NonNullable value', () => {
|
||||
expect(assertNotNull('')).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for a null value', () => {
|
||||
expect(assertNotNull(null)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false for an undefined value', () => {
|
||||
expect(assertNotNull(undefined)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,35 @@
|
||||
import {
|
||||
convertCurrencyMicrosToCurrency,
|
||||
convertCurrencyToCurrencyMicros,
|
||||
} from '~/utils/convert-currency-amount';
|
||||
|
||||
describe('convertCurrencyToCurrencyMicros', () => {
|
||||
it('should return null if currencyAmount is null', () => {
|
||||
expect(convertCurrencyToCurrencyMicros(null)).toBeNull();
|
||||
});
|
||||
|
||||
it('should throw an error if currencyAmount converted to micros is not a whole number', () => {
|
||||
expect(() => convertCurrencyToCurrencyMicros(1.023)).toThrow(
|
||||
'Cannot convert 1.023 to micros',
|
||||
);
|
||||
});
|
||||
|
||||
it('should convert currencyAmount to micros', () => {
|
||||
expect(convertCurrencyToCurrencyMicros(1)).toBe(1000000);
|
||||
expect(convertCurrencyToCurrencyMicros(1.5)).toBe(1500000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertCurrencyMicrosToCurrency', () => {
|
||||
it('should return null if currencyAmountMicros is null', () => {
|
||||
expect(convertCurrencyMicrosToCurrency(null)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if currencyAmountMicros is undefined', () => {
|
||||
expect(convertCurrencyMicrosToCurrency(undefined)).toBeNull();
|
||||
});
|
||||
|
||||
it('should convert currency micros to currency', () => {
|
||||
expect(convertCurrencyMicrosToCurrency(24000000)).toBe(24);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,26 @@
|
||||
import { cookieStorage } from '~/utils/cookie-storage';
|
||||
|
||||
describe('cookieStorage', () => {
|
||||
it('should be able to set and get a cookie', () => {
|
||||
cookieStorage.setItem('foo', 'bar');
|
||||
expect(cookieStorage.getItem('foo')).toBe('bar');
|
||||
});
|
||||
|
||||
it('should return undefined for a non-existent cookie', () => {
|
||||
expect(cookieStorage.getItem('non-existent')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should be able to remove a cookie', () => {
|
||||
cookieStorage.setItem('foo', 'bar');
|
||||
cookieStorage.removeItem('foo');
|
||||
expect(cookieStorage.getItem('foo')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should be able to clear all cookies', () => {
|
||||
cookieStorage.setItem('foo', 'bar');
|
||||
cookieStorage.setItem('baz', 'qux');
|
||||
cookieStorage.clear();
|
||||
expect(cookieStorage.getItem('foo')).toBeUndefined();
|
||||
expect(cookieStorage.getItem('baz')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
20
packages/twenty-front/src/utils/__tests__/debounce.test.ts
Normal file
20
packages/twenty-front/src/utils/__tests__/debounce.test.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { debounce } from '../debounce';
|
||||
|
||||
describe('debounce', () => {
|
||||
it('should debounce a function', () => {
|
||||
jest.useFakeTimers();
|
||||
|
||||
const func = jest.fn();
|
||||
const debouncedFunc = debounce(func, 1000);
|
||||
|
||||
debouncedFunc();
|
||||
debouncedFunc();
|
||||
debouncedFunc();
|
||||
|
||||
expect(func).not.toHaveBeenCalled();
|
||||
|
||||
jest.runAllTimers();
|
||||
|
||||
expect(func).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
19
packages/twenty-front/src/utils/__tests__/isDefined.test.ts
Normal file
19
packages/twenty-front/src/utils/__tests__/isDefined.test.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
describe('isDefined', () => {
|
||||
it('should return true for a NonNullable value', () => {
|
||||
expect(isDefined(1)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for a NonNullable value', () => {
|
||||
expect(isDefined('')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for a null value', () => {
|
||||
expect(isDefined(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for an undefined value', () => {
|
||||
expect(isDefined(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,19 @@
|
||||
import { isNonEmptyArray } from '../isNonEmptyArray';
|
||||
|
||||
describe('isNonEmptyArray', () => {
|
||||
it('should return true for a non empty array', () => {
|
||||
expect(isNonEmptyArray([1])).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for an empty array', () => {
|
||||
expect(isNonEmptyArray([])).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for a null value', () => {
|
||||
expect(isNonEmptyArray(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for an undefined value', () => {
|
||||
expect(isNonEmptyArray(undefined)).toBe(false);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
import { stringToHslColor } from '../string-to-hsl';
|
||||
|
||||
describe('stringToHslColor', () => {
|
||||
it('should return a color based on a string', () => {
|
||||
expect(stringToHslColor('red', 70, 90)).toBe('hsl(105, 70%, 90%)');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,26 @@
|
||||
import { getPageTitleFromPath } from '../title-utils';
|
||||
|
||||
describe('title-utils', () => {
|
||||
it('should return the correct title for a given path', () => {
|
||||
expect(getPageTitleFromPath('/verify')).toBe('Verify');
|
||||
expect(getPageTitleFromPath('/sign-in')).toBe('Sign In');
|
||||
expect(getPageTitleFromPath('/sign-up')).toBe('Sign Up');
|
||||
expect(getPageTitleFromPath('/invite/:workspaceInviteHash')).toBe('Invite');
|
||||
expect(getPageTitleFromPath('/create/workspace')).toBe('Create Workspace');
|
||||
expect(getPageTitleFromPath('/create/profile')).toBe('Create Profile');
|
||||
expect(getPageTitleFromPath('/tasks')).toBe('Tasks');
|
||||
expect(getPageTitleFromPath('/objects/opportunities')).toBe(
|
||||
'Opportunities',
|
||||
);
|
||||
expect(getPageTitleFromPath('/settings/profile')).toBe('Profile');
|
||||
expect(getPageTitleFromPath('/settings/profile/appearance')).toBe(
|
||||
'Appearance',
|
||||
);
|
||||
expect(getPageTitleFromPath('/settings/workspace-members')).toBe(
|
||||
'Workspace Members',
|
||||
);
|
||||
expect(getPageTitleFromPath('/settings/workspace')).toBe('Workspace');
|
||||
expect(getPageTitleFromPath('/')).toBe('Twenty');
|
||||
expect(getPageTitleFromPath('/random')).toBe('Twenty');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
import { arrayToChunks } from '~/utils/array/array-to-chunks';
|
||||
|
||||
describe('arrayToChunks', () => {
|
||||
it('should split an array into subarrays of a given size', () => {
|
||||
expect(arrayToChunks([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
|
||||
});
|
||||
});
|
||||
@ -1 +1,2 @@
|
||||
export const formatNumber = (value: number): string => value.toLocaleString();
|
||||
export const formatNumber = (value: number): string =>
|
||||
value.toLocaleString('en-US');
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
import { capitalize } from '../capitalize';
|
||||
|
||||
describe('capitalize', () => {
|
||||
it('should capitalize a string', () => {
|
||||
expect(capitalize('test')).toBe('Test');
|
||||
});
|
||||
|
||||
it('should return an empty string if input is an empty string', () => {
|
||||
expect(capitalize('')).toBe('');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user