From 264d720d4066200d5bd4f75386631d2cb402e511 Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Sun, 14 Jan 2024 11:58:40 +0100 Subject: [PATCH 1/6] refactor: string schema tests --- library/src/schemas/string/string.test.ts | 114 +++++++++++++++------- 1 file changed, 77 insertions(+), 37 deletions(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index ed63198fa..a815c54fd 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -4,14 +4,30 @@ 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('shoud reject', () => { + const schema = string(); + + test('schema, which imcludes 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', () => { @@ -20,38 +36,62 @@ describe('string', () => { }); 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('minLength & maxLength', () => { + const lengthError = 'Invalid length'; + const schema1 = string([minLength(1), maxLength(3)]); + + test('should pass correct length', () => { + const input1 = '12'; + 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); + }); + }); }); - test('should expose the pipeline', () => { + describe('schema pipeline', () => { const schema1 = string([minLength(2), maxLength(3)]); - expect(schema1.pipe).toStrictEqual([ - expect.objectContaining({ - type: 'min_length', - requirement: 2, - message: 'Invalid length', - }), - expect.objectContaining({ - type: 'max_length', - requirement: 3, - message: 'Invalid length', - }), - ]); - - const schema2 = string(); - expect(schema2.pipe).toBeUndefined(); + + test('should contain invalid length message, type and requirements', () => { + expect(schema1.pipe).toStrictEqual([ + expect.objectContaining({ + type: 'min_length', + requirement: 2, + message: 'Invalid length', + }), + expect.objectContaining({ + type: 'max_length', + requirement: 3, + message: 'Invalid length', + }), + ]); + }); + + test('should be undefined, if empty schema', () => { + const schema2 = string(); + expect(schema2.pipe).toBeUndefined(); + }); }); }); From c583ed6dd61ba12f5d96205e4248624def99f2de Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Sun, 14 Jan 2024 12:12:16 +0100 Subject: [PATCH 2/6] fix typo in test message --- library/src/schemas/string/string.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index a815c54fd..f2b505eed 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -17,7 +17,7 @@ describe('string', () => { describe('shoud reject', () => { const schema = string(); - test('schema, which imcludes numbers', () => { + test('schema, which includes numbers', () => { expect(() => parse(schema, 123)).toThrowError(); }); From 177754b62680629f8f9cf2ebbed8c635e42de496 Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Sun, 14 Jan 2024 19:01:12 +0100 Subject: [PATCH 3/6] refactor: stringAsync and string schema tests --- library/src/schemas/string/string.test.ts | 6 +- .../src/schemas/string/stringAsync.test.ts | 125 ++++++++++++------ 2 files changed, 88 insertions(+), 43 deletions(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index f2b505eed..f9e32b1f4 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -14,7 +14,7 @@ describe('string', () => { }); }); - describe('shoud reject', () => { + describe('should reject', () => { const schema = string(); test('schema, which includes numbers', () => { @@ -35,13 +35,13 @@ describe('string', () => { expect(() => parse(string(error), 123)).toThrowError(error); }); - test('should execute pipe', () => { + 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 = '12'; + const input1 = '123'; const output1 = parse(schema1, input1); expect(output1).toBe(input1); }); diff --git a/library/src/schemas/string/stringAsync.test.ts b/library/src/schemas/string/stringAsync.test.ts index bca66a295..34ebdae44 100644 --- a/library/src/schemas/string/stringAsync.test.ts +++ b/library/src/schemas/string/stringAsync.test.ts @@ -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 () => { @@ -21,41 +41,66 @@ 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 + ); + }); + }); }); - test('should expose the pipeline', () => { + describe('schema pipeline', () => { const schema1 = stringAsync([minLength(2), maxLength(3)]); - expect(schema1.pipe).toStrictEqual([ - expect.objectContaining({ - type: 'min_length', - requirement: 2, - message: 'Invalid length', - }), - expect.objectContaining({ - type: 'max_length', - requirement: 3, - message: 'Invalid length', - }), - ]); - - const schema2 = stringAsync(); - expect(schema2.pipe).toBeUndefined(); + + test('should contain invalid length message, type and requirements', () => { + expect(schema1.pipe).toStrictEqual([ + expect.objectContaining({ + type: 'min_length', + requirement: 2, + message: 'Invalid length', + }), + expect.objectContaining({ + type: 'max_length', + requirement: 3, + message: 'Invalid length', + }), + ]); + }); + + test('should be undefined, if empty schema', () => { + const schema2 = stringAsync(); + expect(schema2.pipe).toBeUndefined(); + }); }); }); From 1e2e2c6e44d1551bc390019420d56735b77c38c6 Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Mon, 12 Feb 2024 17:47:19 +0100 Subject: [PATCH 4/6] fix test --- library/src/schemas/string/string.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index f9e32b1f4..69027512d 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -74,17 +74,18 @@ describe('string', () => { describe('schema pipeline', () => { const schema1 = string([minLength(2), maxLength(3)]); - test('should contain invalid length message, type and requirements', () => { + test('should expose the pipeline', () => { + console.log(schema1.pipe); expect(schema1.pipe).toStrictEqual([ expect.objectContaining({ type: 'min_length', + expects: '>=2', requirement: 2, - message: 'Invalid length', }), expect.objectContaining({ type: 'max_length', + expects: '<=3', requirement: 3, - message: 'Invalid length', }), ]); }); From c9e6fd8ab0569eade84e5a6ba5170372c26ceef0 Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Mon, 12 Feb 2024 18:00:16 +0100 Subject: [PATCH 5/6] fix tests --- library/src/schemas/string/string.test.ts | 1 - library/src/schemas/string/stringAsync.test.ts | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index 69027512d..8c6e19189 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -75,7 +75,6 @@ describe('string', () => { const schema1 = string([minLength(2), maxLength(3)]); test('should expose the pipeline', () => { - console.log(schema1.pipe); expect(schema1.pipe).toStrictEqual([ expect.objectContaining({ type: 'min_length', diff --git a/library/src/schemas/string/stringAsync.test.ts b/library/src/schemas/string/stringAsync.test.ts index 34ebdae44..68f1402ea 100644 --- a/library/src/schemas/string/stringAsync.test.ts +++ b/library/src/schemas/string/stringAsync.test.ts @@ -83,17 +83,17 @@ describe('stringAsync', () => { describe('schema pipeline', () => { const schema1 = stringAsync([minLength(2), maxLength(3)]); - test('should contain invalid length message, type and requirements', () => { + test('should expose the pipeline', () => { expect(schema1.pipe).toStrictEqual([ expect.objectContaining({ type: 'min_length', + expects: '>=2', requirement: 2, - message: 'Invalid length', }), expect.objectContaining({ type: 'max_length', + expects: '<=3', requirement: 3, - message: 'Invalid length', }), ]); }); From fe9c8e3c02e93940a3ee66bb5ab07d7974d7ea8c Mon Sep 17 00:00:00 2001 From: Aris Kemper Date: Tue, 13 Feb 2024 12:13:52 +0100 Subject: [PATCH 6/6] add schema test for properties and pipe --- library/src/schemas/string/string.test.ts | 21 +++++++++++++++--- .../src/schemas/string/stringAsync.test.ts | 22 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/library/src/schemas/string/string.test.ts b/library/src/schemas/string/string.test.ts index 8c6e19189..8f53d6e2a 100644 --- a/library/src/schemas/string/string.test.ts +++ b/library/src/schemas/string/string.test.ts @@ -71,20 +71,35 @@ describe('string', () => { }); }); - describe('schema pipeline', () => { - const schema1 = string([minLength(2), maxLength(3)]); + 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 the pipeline', () => { + 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, }), ]); }); diff --git a/library/src/schemas/string/stringAsync.test.ts b/library/src/schemas/string/stringAsync.test.ts index 68f1402ea..bf7c620ff 100644 --- a/library/src/schemas/string/stringAsync.test.ts +++ b/library/src/schemas/string/stringAsync.test.ts @@ -80,20 +80,34 @@ describe('stringAsync', () => { }); }); - describe('schema pipeline', () => { - const schema1 = stringAsync([minLength(2), maxLength(3)]); - - test('should expose the pipeline', () => { + 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, }), ]); });