Skip to content

Commit

Permalink
Additional testing for new helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrich committed Jun 4, 2024
1 parent 48d5553 commit cbf2e61
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/schema/field/arrays/allowed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import {type SchemaFieldType} from '../type';

/**
* Determine if array support is enabled based on all supported types for a single
* schema field.
* Determine if field supports array values based on field types.
* @param items Field types to check.
*
* @category Schema – Field
Expand Down
2 changes: 1 addition & 1 deletion src/transform/verified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {Fate} from '@toreda/fate';
import {Log} from '@toreda/log';
import {schemaError} from '../schema/error';
import {type VerifiedMap} from '../verified/map';
import {Transformed} from '../transformed';
import {type Transformed} from '../transformed';
import {transformVerifiedField} from './verified/field';

/**
Expand Down
2 changes: 2 additions & 0 deletions src/transformed/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/

/**
* Type returned by functions that transform verified field data. Fields may contain
* scalar values, nulls, subfields, and arrays containing the former.
* @param id
* @param data
* @param base
Expand Down
67 changes: 67 additions & 0 deletions tests/empty.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {empty} from '../src/empty';
const EMPTY_ARRAY: string[] = [];

describe('empty', () => {
it(`should return false when value is undefined`, () => {
expect(empty<any>(undefined)).toBe(false);
});

it(`should return false when value is null`, () => {
expect(empty<any>(null)).toBe(false);
});

it(`should return false when value is a non-empty array`, () => {
expect(empty<any>([11, 'aa'])).toBe(false);
});

it(`should return true when value is an empty array`, () => {
expect(empty<any>(EMPTY_ARRAY)).toBe(true);
});

it(`should return true when value is an empty object`, () => {
expect(empty<any>({})).toBe(true);
});

it(`should return false when value is a non-empty string`, () => {
expect(empty<any>('aaaaaa')).toBe(false);
});

it(`should return false when value is a non-empty string`, () => {
expect(empty<any>('aaaaaa')).toBe(false);
});

it(`should return true when value is a space-padded empty string`, () => {
let value = ' ';

for (let i = 0; i < 5; i++) {
expect(empty<any>(value)).toBe(true);
value += ' ';
}
});

it(`should return true when value is an non-empty object`, () => {
expect(
empty<any>({
one: 'aaaaa'
})
).toBe(false);
});

it(`should return false when value is a boolean (true)`, () => {
expect(empty<boolean>(true)).toBe(false);
});

it(`should return false when value is a boolean (true)`, () => {
expect(empty<boolean>(false)).toBe(false);
});

it(`should return false when value is a falsy number (0)`, () => {
expect(empty<number>(0)).toBe(false);
});

it(`should return false when value is a truthy number`, () => {
expect(empty<number>(1)).toBe(false);
expect(empty<number>(10000)).toBe(false);
expect(empty<number>(-1)).toBe(false);
});
});
123 changes: 123 additions & 0 deletions tests/schema/field/arrays/allowed.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {SchemaFieldType} from '../../../../src';
import {schemaFieldArraysAllowed} from '../../../../src/schema/field/arrays/allowed';
import {schemaFieldTypes} from '../../../../src/schema/field/types';

const EMPTY_STRING = '';
const EMPTY_OBJECT = {};
const EMPTY_STR_ARRAY: string[] = [];

const NON_ARRAY_TYPES: SchemaFieldType[] = [
'bigint',
'BigInt',
'boolean',
'datetime',
'dbl',
'double',
'float',
'int',
'iterable',
'json',
'null',
'number',
'serialized',
'string',
'time',
'uint',
'undefined',
'url'
];

const ARRAY_TYPES: SchemaFieldType<string>[] = [
'bigint[]',
'BigInt[]',
'boolean[]',
'datetime[]',
'datetime[]',
'dbl[]',
'double[]',
'float[]',
'int[]',
'iterable[]',
'json[]',
'null[]',
'number[]',
'serialized[]',
'string[]',
'time[]',
'undefined[]',
'url[]'
];

describe('schemaFieldArraysAllowed', () => {
it(`should return false when items arg is an empty string`, () => {
expect(schemaFieldArraysAllowed<string>(EMPTY_STRING as any)).toBe(false);
});

it(`should return false when items arg is a string`, () => {
expect(schemaFieldArraysAllowed<string>('f087145081-998132' as any)).toBe(false);
});

it(`should return false when items arg is an empty array`, () => {
expect(schemaFieldArraysAllowed<string>(EMPTY_STR_ARRAY as any)).toBe(false);
});

it(`should return false when items arg is an empty object`, () => {
expect(schemaFieldArraysAllowed<string>(EMPTY_OBJECT as any)).toBe(false);
});

it(`should return false for all built-in non-array types'`, () => {
expect(schemaFieldArraysAllowed<string>(NON_ARRAY_TYPES)).toBe(false);
});

it(`should return true when one item is an array type among non-array types`, () => {
const input: SchemaFieldType<string>[] = [...NON_ARRAY_TYPES, 'number[]'];
expect(schemaFieldArraysAllowed<string>(input)).toBe(true);
});

it(`should return true when items array has one array type`, () => {
expect(schemaFieldArraysAllowed<string>(['BigInt[]'])).toBe(true);
});

for (const fieldType of NON_ARRAY_TYPES) {
it(`should return false when items arg is a single non-array type '${fieldType}'`, () => {
expect(schemaFieldArraysAllowed<string>([fieldType])).toBe(false);
});
}

for (const fieldType of ARRAY_TYPES) {
it(`should return true when items array has one array type '${fieldType}'`, () => {
expect(schemaFieldArraysAllowed<string>([fieldType])).toBe(true);
});
}

it(`should return true when the last item is an array type and all other elements are skipped due to being non-strings`, () => {
expect(
schemaFieldArraysAllowed<string>([
undefined,
null,
1000,
{},
[],
'___',
EMPTY_STRING,
'BigInt[]'
] as any)
).toBe(true);
});

it(`should return true when items arg contains a non-builtin type ending in '[]'`, () => {
expect(
schemaFieldArraysAllowed<string>([
null,
undefined,
10,
100,
91910,
'---',
'string',
'undefined',
'customtype[]'
] as any)
).toBe(true);
});
});
46 changes: 46 additions & 0 deletions tests/transform/verified.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Primitive} from '@toreda/types';
import {transformVerified} from '../../src/transform/verified';
import {schemaError} from '../../src';
import {Levels, Log} from '@toreda/log';

describe('transformVerified', () => {
let verifiedMap: Map<string, string>;
let base: Log;

beforeAll(() => {
verifiedMap = new Map<string, string>();
base = new Log({
consoleEnabled: true,
globalLevel: Levels.ALL,
groupsStartEnabled: true
});
});

beforeEach(() => {
verifiedMap.clear();
});

describe('Base parameters', () => {
it(`should fail with code when base arg is undefined`, async () => {
const result = await transformVerified<Primitive, any>(verifiedMap, undefined as any);
expect(result.ok()).toBe(false);

expect(result.errorCode()).toBe(schemaError('missing_argument', 'transformVerified', 'base'));
});

it(`should fail with code when base arg is null`, async () => {
const result = await transformVerified<Primitive, any>(verifiedMap, null as any);
expect(result.ok()).toBe(false);

expect(result.errorCode()).toBe(schemaError('missing_argument', 'transformVerified', 'base'));
});

it(`should succeed when input is an empty map`, async () => {
expect(verifiedMap.size).toBe(0);

const result = await transformVerified<Primitive, any>(verifiedMap, base);
expect(result.ok()).toBe(true);
expect(result.data).toStrictEqual({});
});
});
});

0 comments on commit cbf2e61

Please sign in to comment.