test: improve utils coverage (#4230)

* test: improve utils coverage

* refactor: review - rename isDefined to isNonNullable, update tests and return statement
This commit is contained in:
Thaïs
2024-02-29 13:03:52 -03:00
committed by GitHub
parent 6ec0e5e995
commit 30df6c10ea
85 changed files with 396 additions and 240 deletions

View File

@ -1,19 +0,0 @@
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();
});
});

View File

@ -1,19 +0,0 @@
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);
});
});

View File

@ -0,0 +1,15 @@
import { isNonNullable } from '~/utils/isNonNullable';
describe('isNonNullable', () => {
it('returns true if value is not undefined nor null', () => {
expect(isNonNullable('')).toBe(true);
});
it('returns false if value is null', () => {
expect(isNonNullable(null)).toBe(false);
});
it('returns false if value is undefined', () => {
expect(isNonNullable(undefined)).toBe(false);
});
});

View File

@ -0,0 +1,30 @@
import { parseApolloStoreFieldName } from '../parseApolloStoreFieldName';
describe('parseApolloStoreFieldName', () => {
it('returns an empty object if string is not a valid store field name', () => {
const result = parseApolloStoreFieldName('////');
expect(result).toEqual({});
});
it('returns the field name and parsed variables if they exist', () => {
const result = parseApolloStoreFieldName('fieldName({"key":"value"})');
expect(result).toEqual({
fieldName: 'fieldName',
fieldVariables: { key: 'value' },
});
});
it('returns only the field name if the variables cannot be parsed', () => {
const result = parseApolloStoreFieldName('fieldName(notJson)');
expect(result).toEqual({
fieldName: 'fieldName',
});
});
it('returns only the field name if there are no variables', () => {
const result = parseApolloStoreFieldName('fieldName');
expect(result).toEqual({
fieldName: 'fieldName',
});
});
});

View File

@ -0,0 +1,35 @@
import { sortAsc, sortDesc, sortNullsFirst, sortNullsLast } from '../sort';
describe('sort', () => {
describe('sortNullsFirst', () => {
it('should sort nulls first', () => {
expect(sortNullsFirst(null, 'a')).toBe(-1);
expect(sortNullsFirst('a', null)).toBe(1);
expect(sortNullsFirst('a', 'a')).toBe(0);
});
});
describe('sortNullsLast', () => {
it('should sort nulls last', () => {
expect(sortNullsLast(null, 'a')).toBe(1);
expect(sortNullsLast('a', null)).toBe(-1);
expect(sortNullsLast('a', 'a')).toBe(0);
});
});
describe('sortAsc', () => {
it('should sort in ascending order', () => {
expect(sortAsc('a', 'b')).toBe(-1);
expect(sortAsc('b', 'a')).toBe(1);
expect(sortAsc('a', 'a')).toBe(0);
});
});
describe('sortDesc', () => {
it('should sort in descending order', () => {
expect(sortDesc('a', 'b')).toBe(1);
expect(sortDesc('b', 'a')).toBe(-1);
expect(sortDesc('a', 'a')).toBe(0);
});
});
});