From 416c5064eb9ce434f65c472bcee3cbb210ea3c15 Mon Sep 17 00:00:00 2001 From: Kirill Zaytsev Date: Mon, 5 Feb 2024 13:55:17 +0400 Subject: [PATCH] test: Added cases for 'Exists' constraint, non-object validation with 'Collection' constraint --- Makefile | 9 +++ tests/index.test.ts | 165 +++++++++++++++++++++++++++++++------------- 2 files changed, 126 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index 4b12715..0ea5a45 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,15 @@ test: node_modules ## Runs autotests $(TARGET_HEADER) $(YARN) test +.PHONY: test-coverage +test-coverage: node_modules ## Runs autotests with --coverage + $(TARGET_HEADER) +ifdef reporter + $(YARN) test --coverage --coverageReporters=$(reporter) +else + $(YARN) test --coverage --coverageReporters=text +endif + .PHONY: help help: ## Calls recipes list @cat $(MAKEFILE_LIST) | grep -e "^[a-zA-Z_\-]*: *.*## *" | awk '\ diff --git a/tests/index.test.ts b/tests/index.test.ts index d97f497..d4e8e19 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -26,33 +26,67 @@ import { describe('validates synchronously', () => { const validator = createValidator() - test('Collection', () => { - expect(validator.validate({ - form: { - nickname: '', - password: '', - }, - }, new Collection({ - form: [ - new Exists(), - new Collection({ + describe('Collection', () => { + test('checks object\'s structure', () => { + expect(validator.validate({ + form: { + nickname: '', + password: '', + }, + }, new Collection({ + form: [ + new Collection({ + nickname: new Length({ min: 4 }), + password: new Length({ min: 6 }), + }), + ], + }), false)).toEqual([{ + by: '@modulify/validator/Length', + value: '', + path: ['form', 'nickname'], + reason: 'min', + meta: 4, + }, { + by: '@modulify/validator/Length', + value: '', + path: ['form', 'password'], + reason: 'min', + meta: 6, + }]) + }) + + test('doesn\'t check non-object values', () => { + expect(validator.validate('', new Collection({ + form: new Collection({ nickname: new Length({ min: 4 }), password: new Length({ min: 6 }), }), - ], - }), false)).toEqual([{ - by: '@modulify/validator/Length', - value: '', - path: ['form', 'nickname'], - reason: 'min', - meta: 4, - }, { - by: '@modulify/validator/Length', - value: '', - path: ['form', 'password'], - reason: 'min', - meta: 6, - }]) + }), false)).toEqual([{ + by: '@modulify/validator/Collection', + value: '', + path: [], + reason: 'unsupported', + }]) + }) + }) + + describe('Collection & Exists', () => { + test('checks object\'s structure', () => { + expect(validator.validate({}, new Collection({ + form: [ + new Exists(), + new Collection({ + nickname: new Length({ min: 4 }), + password: new Length({ min: 6 }), + }), + ], + }), false)).toEqual([{ + by: '@modulify/validator/Exists', + value: undefined, + path: ['form'], + reason: 'undefined', + }]) + }) }) describe('Each', () => { @@ -107,33 +141,68 @@ describe('validates synchronously', () => { describe('validates asynchronously', () => { const validator = createValidator() - test('Collection', async () => { - expect(await validator.validate({ - form: { - nickname: '', - password: '', - }, - }, new Collection({ - form: [ - new Exists(), - new Collection({ + describe('Collection', () => { + test('checks object\'s structure', async () => { + expect(await validator.validate({ + form: { + nickname: '', + password: '', + }, + }, new Collection({ + form: [ + new Exists(), + new Collection({ + nickname: new Length({ min: 4 }), + password: new Length({ min: 6 }), + }), + ], + }))).toEqual([{ + by: '@modulify/validator/Length', + value: '', + path: ['form', 'nickname'], + reason: 'min', + meta: 4, + }, { + by: '@modulify/validator/Length', + value: '', + path: ['form', 'password'], + reason: 'min', + meta: 6, + }]) + }) + + test('doesn\'t check non-object values', async () => { + expect(await validator.validate('', new Collection({ + form: new Collection({ nickname: new Length({ min: 4 }), password: new Length({ min: 6 }), }), - ], - }))).toEqual([{ - by: '@modulify/validator/Length', - value: '', - path: ['form', 'nickname'], - reason: 'min', - meta: 4, - }, { - by: '@modulify/validator/Length', - value: '', - path: ['form', 'password'], - reason: 'min', - meta: 6, - }]) + }))).toEqual([{ + by: '@modulify/validator/Collection', + value: '', + path: [], + reason: 'unsupported', + }]) + }) + }) + + describe('Collection & Exists', () => { + test('checks object\'s structure', async () => { + expect(await validator.validate({}, new Collection({ + form: [ + new Exists(), + new Collection({ + nickname: new Length({ min: 4 }), + password: new Length({ min: 6 }), + }), + ], + }))).toEqual([{ + by: '@modulify/validator/Exists', + value: undefined, + path: ['form'], + reason: 'undefined', + }]) + }) }) describe('Each', () => {