Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: string schema tests #366

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 96 additions & 22 deletions library/src/schemas/string/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,109 @@ import { email, maxLength, minLength } from '../../validations/index.ts';
import { string } from './string.ts';

describe('string', () => {
test('should pass only strings', () => {
describe('should pass', () => {
const schema = string();
const input = '';
const output = parse(schema, input);
expect(output).toBe(input);
expect(() => parse(schema, 123n)).toThrowError();
expect(() => parse(schema, null)).toThrowError();
expect(() => parse(schema, {})).toThrowError();

test('empty string schema', () => {
const input = '';
const output = parse(schema, input);
expect(output).toBe(input);
});
});

describe('should reject', () => {
const schema = string();

test('schema, which includes numbers', () => {
expect(() => parse(schema, 123)).toThrowError();
});

test('reject null schema', () => {
expect(() => parse(schema, null)).toThrowError();
});

test('empty object schema', () => {
expect(() => parse(schema, {})).toThrowError();
});
});

test('should throw custom error', () => {
const error = 'Value is not a string!';
expect(() => parse(string(error), 123)).toThrowError(error);
});

test('should execute pipe', () => {
const lengthError = 'Invalid length';
const schema1 = string([minLength(1), maxLength(3)]);
const input1 = '12';
const output1 = parse(schema1, input1);
expect(output1).toBe(input1);
expect(() => parse(schema1, '')).toThrowError(lengthError);
expect(() => parse(schema1, '1234')).toThrowError(lengthError);

const emailError = 'Invalid email';
const schema2 = string('Error', [email()]);
const input2 = 'jane@example.com';
const output2 = parse(schema2, input2);
expect(output2).toBe(input2);
expect(() => parse(schema2, 'jane@example')).toThrowError(emailError);
describe('should execute pipe', () => {
describe('minLength & maxLength', () => {
const lengthError = 'Invalid length';
const schema1 = string([minLength(1), maxLength(3)]);

test('should pass correct length', () => {
const input1 = '123';
const output1 = parse(schema1, input1);
expect(output1).toBe(input1);
});

test('should reject empty string', () => {
expect(() => parse(schema1, '')).toThrowError(lengthError);
});

test('should reject to long strings', () => {
expect(() => parse(schema1, '1234')).toThrowError(lengthError);
});
});

describe('email', () => {
const emailError = 'Invalid email';
const schema2 = string('Error', [email()]);

test('should pass', () => {
const input2 = 'jane@example.com';
const output2 = parse(schema2, input2);
expect(output2).toBe(input2);
});

test('should reject invalid email address string', () => {
expect(() => parse(schema2, 'jane@example')).toThrowError(emailError);
});
});
});

describe('schema', () => {
test('should expose properties', async () => {
const schema1 = string([minLength(2), maxLength(3)]);
expect(schema1).toStrictEqual(
expect.objectContaining({
type: 'string',
expects: 'string',
async: false,
message: undefined,
})
);
});

test('should expose pipeline', () => {
const schema1 = string([minLength(2), maxLength(3)]);
expect(schema1.pipe).toStrictEqual([
expect.objectContaining({
type: 'min_length',
expects: '>=2',
requirement: 2,
async: false,
message: undefined,
}),
expect.objectContaining({
type: 'max_length',
expects: '<=3',
requirement: 3,
async: false,
message: undefined,
}),
]);
});

test('should be undefined, if empty schema', () => {
const schema2 = string();
expect(schema2.pipe).toBeUndefined();
});
});
});
126 changes: 102 additions & 24 deletions library/src/schemas/string/stringAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ import { email, maxLength, minLength } from '../../validations/index.ts';
import { stringAsync } from './stringAsync.ts';

describe('stringAsync', () => {
test('should pass only strings', async () => {
describe('should pass', () => {
const schema = stringAsync();
const input = '';
const output = await parseAsync(schema, input);
expect(output).toBe(input);
await expect(parseAsync(schema, 123n)).rejects.toThrowError();
await expect(parseAsync(schema, null)).rejects.toThrowError();
await expect(parseAsync(schema, {})).rejects.toThrowError();

test('empty string schema', async () => {
const input = '';
const output = await parseAsync(schema, input);
expect(output).toBe(input);
});
});

describe('should reject', () => {
const schema = stringAsync();

test('schema, if not string', async () => {
await expect(parseAsync(schema, 123n)).rejects.toThrowError(
'Invalid type'
);
});

test('null schema', async () => {
await expect(parseAsync(schema, null)).rejects.toThrowError(
'Invalid type'
);
});

test('empty object schema', async () => {
await expect(parseAsync(schema, {})).rejects.toThrowError('Invalid type');
});
});

test('should throw custom error', async () => {
Expand All @@ -21,22 +41,80 @@ describe('stringAsync', () => {
);
});

test('should execute pipe', async () => {
const lengthError = 'Invalid length';
const schema1 = stringAsync([minLength(1), maxLength(3)]);
const input1 = '12';
const output1 = await parseAsync(schema1, input1);
expect(output1).toBe(input1);
await expect(parseAsync(schema1, '')).rejects.toThrowError(lengthError);
await expect(parseAsync(schema1, '1234')).rejects.toThrowError(lengthError);

const emailError = 'Invalid email';
const schema2 = stringAsync('Error', [email()]);
const input2 = 'jane@example.com';
const output2 = await parseAsync(schema2, input2);
expect(output2).toBe(input2);
await expect(parseAsync(schema2, 'jane@example')).rejects.toThrowError(
emailError
);
describe('should execute pipe', () => {
describe('minLength and maxLength', async () => {
const lengthError = 'Invalid length';
const schema1 = stringAsync([minLength(1), maxLength(3)]);

test('should pass correct length', async () => {
const input1 = '123';
expect(await parseAsync(schema1, input1)).toBe(input1);
});

test('should reject empty string', async () => {
await expect(parseAsync(schema1, '')).rejects.toThrowError(lengthError);
});

test('should reject to long strings', async () => {
await expect(parseAsync(schema1, '1234')).rejects.toThrowError(
lengthError
);
});
});

describe('email', () => {
const emailError = 'Invalid email';
const schema2 = stringAsync('Error', [email()]);

test('should pass', async () => {
const input2 = 'jane@example.com';
const output2 = await parseAsync(schema2, input2);
expect(output2).toBe(input2);
});

test('should reject invalid email address string', async () => {
await expect(parseAsync(schema2, 'jane@example')).rejects.toThrowError(
emailError
);
});
});
});

describe('schema', () => {
test('should expose properties', async () => {
const schema1 = stringAsync([minLength(2), maxLength(3)]);
expect(schema1).toStrictEqual(
expect.objectContaining({
type: 'string',
expects: 'string',
async: true,
message: undefined,
})
);
});
test('should expose the pipeline', async () => {
const schema1 = stringAsync([minLength(2), maxLength(3)]);
expect(schema1.pipe).toStrictEqual([
expect.objectContaining({
type: 'min_length',
expects: '>=2',
requirement: 2,
async: false,
message: undefined,
}),
expect.objectContaining({
type: 'max_length',
expects: '<=3',
requirement: 3,
async: false,
message: undefined,
}),
]);
});

test('should be undefined, if empty schema', () => {
const schema2 = stringAsync();
expect(schema2.pipe).toBeUndefined();
});
});
});