From f496b15e4d1fd5812a014cb17bb872e8fcd6f824 Mon Sep 17 00:00:00 2001 From: Ribeiro Date: Wed, 31 Jan 2024 04:38:31 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20de=20Teste=20Unit?= =?UTF-8?q?=C3=A1rio=20CustomValidators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validators/custom.validators.spec.ts | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 HomeBrokerSPA/HomeBrokerChart/src/app/shared/validators/custom.validators.spec.ts diff --git a/HomeBrokerSPA/HomeBrokerChart/src/app/shared/validators/custom.validators.spec.ts b/HomeBrokerSPA/HomeBrokerChart/src/app/shared/validators/custom.validators.spec.ts new file mode 100644 index 0000000..1a90389 --- /dev/null +++ b/HomeBrokerSPA/HomeBrokerChart/src/app/shared/validators/custom.validators.spec.ts @@ -0,0 +1,72 @@ +import { FormControl, FormGroup } from '@angular/forms'; +import { CustomValidators } from './custom.validators'; + +describe('CustomValidators', () => { + it('should return null for a valid date range', () => { + const formGroup = new FormGroup({ + filterStart: new FormControl('2024-01-01'), + filterEnd: new FormControl('2024-01-10'), + }); + + const result = CustomValidators.dateRange(formGroup); + + expect(result).toBeNull(); + }); + + it('should return error for an invalid date range (end date before start date)', () => { + const formGroup = new FormGroup({ + filterStart: new FormControl('2024-01-10'), + filterEnd: new FormControl('2024-01-01'), + }); + + const result = CustomValidators.dateRange(formGroup); + + expect(result).toEqual({ dateRange: 'A data final deve ser maior ou igual à data inicial' }); + }); + + it('should return error for a date range with less than 5 days interval', () => { + const formGroup = new FormGroup({ + filterStart: new FormControl('2024-01-01'), + filterEnd: new FormControl('2024-01-04'), + }); + + const result = CustomValidators.dateRange(formGroup); + + expect(result).toEqual({ dateDifference: 'O intervalo entre as datas deve ser de pelo menos 5 dias' }); + }); + + it('should return error for a start date earlier than "2011-05-02"', () => { + const formGroup = new FormGroup({ + filterStart: new FormControl('2011-04-30'), + filterEnd: new FormControl('2011-05-10'), + }); + + const result = CustomValidators.dateRange(formGroup); + + expect(result).toEqual({ minDate: 'A data não deve ser anterior a 02/05/2011' }); + }); + + it('should return true for a valid period using IsValidPeriod', () => { + const isValid = CustomValidators.IsValidPeriod('2024-01-01', '2024-01-10'); + + expect(isValid).toBe(true); + }); + + it('should return false for an invalid period using IsValidPeriod (end date before start date)', () => { + const isValid = CustomValidators.IsValidPeriod('2024-01-10', '2024-01-01'); + + expect(isValid).toBe(false); + }); + + it('should return false for an invalid period using IsValidPeriod (less than 5 days interval)', () => { + const isValid = CustomValidators.IsValidPeriod('2024-01-01', '2024-01-04'); + + expect(isValid).toBe(false); + }); + + it('should return false for an invalid period using IsValidPeriod (start date earlier than "2011-05-02")', () => { + const isValid = CustomValidators.IsValidPeriod('2011-04-30', '2011-05-10'); + + expect(isValid).toBe(false); + }); +});