Files
payload/src/fields/validations.spec.ts
Dan Ribbens bab34d82f5 feat: add i18n to admin panel (#1326)
Co-authored-by: shikhantmaungs <shinkhantmaungs@gmail.com>
Co-authored-by: Thomas Ghysels <info@thomasg.be>
Co-authored-by: Kokutse Djoguenou <kokutse@Kokutses-MacBook-Pro.local>
Co-authored-by: Christian Gil <47041342+ChrisGV04@users.noreply.github.com>
Co-authored-by: Łukasz Rabiec <lukaszrabiec@gmail.com>
Co-authored-by: Jenny <jennifer.eberlei@gmail.com>
Co-authored-by: Hung Vu <hunghvu2017@gmail.com>
Co-authored-by: Shin Khant Maung <101539335+shinkhantmaungs@users.noreply.github.com>
Co-authored-by: Carlo Brualdi <carlo.brualdi@gmail.com>
Co-authored-by: Ariel Tonglet <ariel.tonglet@gmail.com>
Co-authored-by: Roman Ryzhikov <general+github@ya.ru>
Co-authored-by: maekoya <maekoya@stromatolite.jp>
Co-authored-by: Emilia Trollros <3m1l1a@emiliatrollros.se>
Co-authored-by: Kokutse J Djoguenou <90865585+Julesdj@users.noreply.github.com>
Co-authored-by: Mitch Dries <mitch.dries@gmail.com>

BREAKING CHANGE: If you assigned labels to collections, globals or block names, you need to update your config! Your GraphQL schema and generated Typescript interfaces may have changed. Payload no longer uses labels for code based naming. To prevent breaking changes to your GraphQL API and typescript types in your project, you can assign the below properties to match what Payload previously generated for you from labels.

On Collections
Use `graphQL.singularName`, `graphQL.pluralName` for GraphQL schema names.
Use `typescript.interface` for typescript generation name.

On Globals
Use `graphQL.name` for GraphQL Schema name.
Use `typescript.interface` for typescript generation name.

On Blocks (within Block fields)
Use `graphQL.singularName` for graphQL schema names.
2022-11-18 07:36:30 -05:00

357 lines
12 KiB
TypeScript

import { text, textarea, password, select, point, number } from './validations';
import { ValidateOptions } from './config/types';
const t = jest.fn((string) => string);
let options: ValidateOptions<any, any, any> = {
operation: 'create',
data: undefined,
siblingData: undefined,
t,
};
describe('Field Validations', () => {
describe('text', () => {
it('should validate', () => {
const val = 'test';
const result = text(val, options);
expect(result).toBe(true);
});
it('should show required message', () => {
const val = undefined;
const result = text(val, { ...options, required: true });
expect(result).toBe('validation:required');
});
it('should handle undefined', () => {
const val = undefined;
const result = text(val, options);
expect(result).toBe(true);
});
it('should validate maxLength', () => {
const val = 'toolong';
const result = text(val, { ...options, maxLength: 5 });
expect(result).toBe('validation:shorterThanMax');
});
it('should validate minLength', () => {
const val = 'short';
const result = text(val, { ...options, minLength: 10 });
expect(result).toBe('validation:longerThanMin');
});
it('should validate maxLength with no value', () => {
const val = undefined;
const result = text(val, { ...options, maxLength: 5 });
expect(result).toBe(true);
});
it('should validate minLength with no value', () => {
const val = undefined;
const result = text(val, { ...options, minLength: 10 });
expect(result).toBe(true);
});
});
describe('textarea', () => {
options = { ...options, field: { type: 'textarea', name: 'test' } };
it('should validate', () => {
const val = 'test';
const result = textarea(val, options);
expect(result).toBe(true);
});
it('should show required message', () => {
const val = undefined;
const result = textarea(val, { ...options, required: true });
expect(result).toBe('validation:required');
});
it('should handle undefined', () => {
const val = undefined;
const result = textarea(val, options);
expect(result).toBe(true);
});
it('should validate maxLength', () => {
const val = 'toolong';
const result = textarea(val, { ...options, maxLength: 5 });
expect(result).toBe('validation:shorterThanMax');
});
it('should validate minLength', () => {
const val = 'short';
const result = textarea(val, { ...options, minLength: 10 });
expect(result).toBe('validation:longerThanMin');
});
it('should validate maxLength with no value', () => {
const val = undefined;
const result = textarea(val, { ...options, maxLength: 5 });
expect(result).toBe(true);
});
it('should validate minLength with no value', () => {
const val = undefined;
const result = textarea(val, { ...options, minLength: 10 });
expect(result).toBe(true);
});
});
describe('password', () => {
options.type = 'password';
options.name = 'test';
it('should validate', () => {
const val = 'test';
const result = password(val, options);
expect(result).toBe(true);
});
it('should show required message', () => {
const val = undefined;
const result = password(val, { ...options, required: true });
expect(result).toBe('validation:required');
});
it('should handle undefined', () => {
const val = undefined;
const result = password(val, options);
expect(result).toBe(true);
});
it('should validate maxLength', () => {
const val = 'toolong';
const result = password(val, { ...options, maxLength: 5 });
expect(result).toBe('validation:shorterThanMax');
});
it('should validate minLength', () => {
const val = 'short';
const result = password(val, { ...options, minLength: 10 });
expect(result).toBe('validation:longerThanMin');
});
it('should validate maxLength with no value', () => {
const val = undefined;
const result = password(val, { ...options, maxLength: 5 });
expect(result).toBe(true);
});
it('should validate minLength with no value', () => {
const val = undefined;
const result = password(val, { ...options, minLength: 10 });
expect(result).toBe(true);
});
});
describe('point', () => {
options.type = 'point';
options.name = 'point';
it('should validate numbers', () => {
const val = ['0.1', '0.2'];
const result = point(val, options);
expect(result).toBe(true);
});
it('should validate strings that could be numbers', () => {
const val = ['0.1', '0.2'];
const result = point(val, options);
expect(result).toBe(true);
});
it('should show required message when undefined', () => {
const val = undefined;
const result = point(val, { ...options, required: true });
expect(result).not.toBe(true);
});
it('should show required message when array', () => {
const val = [];
const result = point(val, { ...options, required: true });
expect(result).not.toBe(true);
});
it('should show required message when array of undefined', () => {
const val = [undefined, undefined];
const result = point(val, { ...options, required: true });
expect(result).not.toBe(true);
});
it('should handle undefined not required', () => {
const val = undefined;
const result = password(val, options);
expect(result).toBe(true);
});
it('should handle empty array not required', () => {
const val = [];
const result = point(val, options);
expect(result).toBe(true);
});
it('should handle array of undefined not required', () => {
const val = [undefined, undefined];
const result = point(val, options);
expect(result).toBe(true);
});
it('should prevent text input', () => {
const val = ['bad', 'input'];
const result = point(val, options);
expect(result).not.toBe(true);
});
it('should prevent missing value', () => {
const val = [0.1];
const result = point(val, options);
expect(result).not.toBe(true);
});
});
describe('select', () => {
options.type = 'select';
options.options = ['one', 'two', 'three'];
const optionsRequired = {
...options,
required: true,
options: [{
value: 'one',
label: 'One',
}, {
value: 'two',
label: 'two',
}, {
value: 'three',
label: 'three',
}],
};
const optionsWithEmptyString = {
...options,
options: [{
value: '',
label: 'None',
}, {
value: 'option',
label: 'Option',
}],
};
it('should allow valid input', () => {
const val = 'one';
const result = select(val, options);
expect(result).toStrictEqual(true);
});
it('should prevent invalid input', () => {
const val = 'bad';
const result = select(val, options);
expect(result).not.toStrictEqual(true);
});
it('should allow null input', () => {
const val = null;
const result = select(val, options);
expect(result).toStrictEqual(true);
});
it('should allow undefined input', () => {
let val;
const result = select(val, options);
expect(result).toStrictEqual(true);
});
it('should prevent empty string input', () => {
const val = '';
const result = select(val, options);
expect(result).not.toStrictEqual(true);
});
it('should prevent undefined input with required', () => {
let val;
const result = select(val, optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should prevent empty string input with required', () => {
const result = select('', optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should prevent undefined input with required and hasMany', () => {
let val;
options.hasMany = true;
const result = select(val, optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should prevent empty array input with required and hasMany', () => {
optionsRequired.hasMany = true;
const result = select([], optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should prevent empty string array input with required and hasMany', () => {
options.hasMany = true;
const result = select([''], optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should prevent null input with required and hasMany', () => {
const val = null;
options.hasMany = true;
const result = select(val, optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should allow valid input with option objects', () => {
const val = 'one';
options.hasMany = false;
const result = select(val, optionsRequired);
expect(result).toStrictEqual(true);
});
it('should prevent invalid input with option objects', () => {
const val = 'bad';
options.hasMany = false;
const result = select(val, optionsRequired);
expect(result).not.toStrictEqual(true);
});
it('should allow empty string input with option object', () => {
const val = '';
const result = select(val, optionsWithEmptyString);
expect(result).toStrictEqual(true);
});
it('should allow empty string input with option object and required', () => {
const val = '';
optionsWithEmptyString.required = true;
const result = select(val, optionsWithEmptyString);
expect(result).toStrictEqual(true);
});
it('should allow valid input with hasMany', () => {
const val = ['one', 'two'];
const result = select(val, options);
expect(result).toStrictEqual(true);
});
it('should prevent invalid input with hasMany', () => {
const val = ['one', 'bad'];
const result = select(val, options);
expect(result).not.toStrictEqual(true);
});
it('should allow valid input with hasMany option objects', () => {
const val = ['one', 'three'];
optionsRequired.hasMany = true;
const result = select(val, optionsRequired);
expect(result).toStrictEqual(true);
});
it('should prevent invalid input with hasMany option objects', () => {
const val = ['three', 'bad'];
optionsRequired.hasMany = true;
const result = select(val, optionsRequired);
expect(result).not.toStrictEqual(true);
});
});
describe('number', () => {
options.type = 'number';
options.name = 'test';
it('should validate', () => {
const val = 1;
const result = number(val, options);
expect(result).toBe(true);
});
it('should validate 2', () => {
const val = 1.5;
const result = number(val, options);
expect(result).toBe(true);
});
it('should show invalid number message', () => {
const val = 'test';
const result = number(val, { ...options });
expect(result).toBe('validation:enterNumber');
});
it('should handle empty value', () => {
const val = '';
const result = number(val, { ...options });
expect(result).toBe(true);
});
it('should handle required value', () => {
const val = '';
const result = number(val, { ...options, required: true });
expect(result).toBe('validation:enterNumber');
});
it('should validate minValue', () => {
const val = 2.4;
const result = number(val, { ...options, min: 2.5 });
expect(result).toBe('validation:lessThanMin');
});
it('should validate maxValue', () => {
const val = 1.25;
const result = number(val, { ...options, max: 1 });
expect(result).toBe('validation:greaterThanMax');
});
});
});