it('should return an error when `schedule` is invalid', () => { const errors = validator.validateSync(new ExpenseSource('Source name', 100, new IncomeExpenseSchedule('UNKNOWN', 'Unknown'))) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.SCHEDULE_SELECT_AN_OPTION('Source name')) })
describe('validation', () => { const validator: Validator = new Validator() context('should accept', () => { it('when both undefined', () => { const errors = validator.validateSync(new TimelineRow(undefined, undefined)) expect(errors.length).to.equal(0) }) it('when both are valid strings', () => { const errors = validator.validateSync(new TimelineRow('Date', 'description')) expect(errors.length).to.equal(0) }) }) context('should reject', () => { it('when date given, but no description', () => { const errors = validator.validateSync(new TimelineRow('May', '')) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.DESCRIPTION_REQUIRED) }) it('when description given, but no date', () => { const errors = validator.validateSync(new TimelineRow('', 'Let me tell you what happened')) expect(errors.length).to.equal(1) expectValidationError(errors, GlobalValidationErrors.DATE_REQUIRED) }) it('when both args are empty strings', () => { const errors = validator.validateSync(new TimelineRow('', '')) expect(errors.length).to.equal(2) expectValidationError(errors, GlobalValidationErrors.DATE_REQUIRED) expectValidationError(errors, ValidationErrors.DESCRIPTION_REQUIRED) }) it('when date is too long', () => { const errors = validator.validateSync( new TimelineRow(generateString(ValidationConstraints.DATE_MAX_LENGTH + 1), 'description') ) expect(errors.length).to.equal(1) expectValidationError( errors, ValidationErrors.DATE_TOO_LONG.replace('$constraint1', ValidationConstraints.DATE_MAX_LENGTH.toString()) ) }) it('when description is too long', () => { const errors = validator.validateSync( new TimelineRow('date', generateString(DefaultValidationConstraints.FREE_TEXT_MAX_LENGTH + 1)) ) expect(errors.length).to.equal(1) expectValidationError(errors, GlobalValidationErrors.TEXT_TOO_LONG) }) }) })
it('when both are valid strings', () => { const errors = validator.validateSync(new TimelineRow('Date', 'description')) expect(errors.length).to.equal(0) })
it('in the future', () => { const dateInThePast = LocalDate.fromObject({ day: 10, month: 10, year: 2200 }) const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.VALID_PAST_DATE) })
it('undefined', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, undefined, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, DefaultValidationErrors.DATE_REQUIRED) })
it('should return no error', () => { const errors = validator.validateSync(new IncomeExpenseSource(100, IncomeExpenseSchedule.MONTH)) expect(errors.length).to.equal(0) })
describe('HowMuchHaveYouPaid', () => { const validator: Validator = new Validator() context('should not be valid when', () => { context('amount is', () => { it('eq 0', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(0, validLocalDate, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.AMOUNT_NOT_VALID) }) it('less than 0', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(-10, validLocalDate, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.AMOUNT_NOT_VALID) }) }) context('date is', () => { it('in the future', () => { const dateInThePast = LocalDate.fromObject({ day: 10, month: 10, year: 2200 }) const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.VALID_PAST_DATE) }) it('invalid', () => { const dateInThePast = LocalDate.fromObject({ day: 33, month: 13, year: 1990 }) const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, dateInThePast, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, LocalDateValidationErrors.DAY_NOT_VALID) }) it('undefined', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, undefined, validText)) expect(errors.length).to.equal(1) expectValidationError(errors, DefaultValidationErrors.DATE_REQUIRED) }) }) context('text is', () => { it('empty', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, validLocalDate, '')) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.EXPLANATION_REQUIRED) }) it('undefined', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, validLocalDate, undefined)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.EXPLANATION_REQUIRED) }) it('too long', () => { const errors = validator.validateSync(new HowMuchHaveYouPaid( validAmount, validLocalDate, generateString(ValidationConstraints.FREE_TEXT_MAX_LENGTH + 1) )) expect(errors.length).to.equal(1) expectValidationError(errors, DefaultValidationErrors.TEXT_TOO_LONG) }) }) }) })
it('when declared = true and one valid row and many many empty ones given', () => { const o: DebtRow = DebtRow.empty() const errors = validator.validateSync(new Debts(true, [o, o, o, o, new DebtRow('card', 1, 1), o, o, o, o])) expect(errors.length).to.equal(0) })
it('when declared = true and empty list of rows', () => { const errors = validator.validateSync(new Debts(true, [])) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.ENTER_AT_LEAST_ONE_ROW) })
it('should return an error when `schedule` is undefined', () => { const errors = validator.validateSync(new ExpenseSource('Source name', 100, undefined)) expect(errors.length).to.equal(1) expectValidationError(errors, ValidationErrors.SCHEDULE_SELECT_AN_OPTION('Source name')) })