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)
      })
    })
  })
})
Example #8
0
      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)
      })
Example #9
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'))
      })
Example #11
0
      it('when declared = false', () => {
        const errors = validator.validateSync(new Debts(false, undefined))

        expect(errors.length).to.equal(0)
      })
      it('should return an error when `amount` is negative', () => {
        const errors = validator.validateSync(new ExpenseSource('Source name', -100, IncomeExpenseSchedule.MONTH))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.AMOUNT_NON_NEGATIVE_NUMBER_REQUIRED('Source name'))
      })
      it('should return an error when `amount` is undefined', () => {
        const errors = validator.validateSync(new ExpenseSource('Source name', undefined, IncomeExpenseSchedule.MONTH))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, ValidationErrors.AMOUNT_REQUIRED('Source name'))
      })
 it('should return errors when all expect `name` are undefined', () => {
   const errors = validator.validateSync(new ExpenseSource('Source name', undefined))
   expect(errors.length).to.equal(2)
   expectValidationError(errors, ValidationErrors.AMOUNT_REQUIRED('Source name'))
   expectValidationError(errors, ValidationErrors.SCHEDULE_SELECT_AN_OPTION('Source name'))
 })
      it('should return an error when `amount` is negative', () => {
        const errors = validator.validateSync(new IncomeExpenseSource(-100, IncomeExpenseSchedule.MONTH))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.POSITIVE_NUMBER_REQUIRED)
      })
Example #16
0
      it('when declared = true and invalid row given', () => {
        const errors = validator.validateSync(new Debts(true, [new DebtRow('', 100, 10)]))

        expect(errors.length).to.equal(1)
      })
      it('should return an error when `schedule` is invalid', () => {
        const errors = validator.validateSync(new IncomeExpenseSource(100, new IncomeExpenseSchedule('UNKNOWN', 1)))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.SELECT_AN_OPTION)
      })
Example #18
0
      it('when declared = true and many invalid row given', () => {
        const o: DebtRow = new DebtRow('my card', -100, 10) // invalid row
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, o, o, o, o, o, o, o, o]))

        expect(errors.length).to.equal(1)
      })
 private static isHowMuchDoYouOweValid (model: HowMuchDoYouOwe): boolean {
   return !!model && validator.validateSync(model).length === 0
 }
Example #20
0
      it('when declared = true and many empty rows and one invalid given', () => {
        const o: DebtRow = DebtRow.empty()
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, o, o, new DebtRow('my card', -100, 10), o]))

        expect(errors.length).to.equal(1)
      })
 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)
 })
Example #22
0
  describe('validation', () => {

    const validator: Validator = new Validator()

    context('should accept', () => {

      it('when declared = false', () => {
        const errors = validator.validateSync(new Debts(false, undefined))

        expect(errors.length).to.equal(0)
      })

      it('when declared = true and one valid row given', () => {
        const errors = validator.validateSync(new Debts(true, [new DebtRow('my card', 100, 10)]))

        expect(errors.length).to.equal(0)
      })

      it('when declared = true and many valid row given', () => {
        const o: DebtRow = new DebtRow('my card', 100, 10) // valid row
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, o, o, o, o, o, o, o, o]))

        expect(errors.length).to.equal(0)
      })

      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)
      })
    })

    context('should reject', () => {

      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('when declared = true and invalid row given', () => {
        const errors = validator.validateSync(new Debts(true, [new DebtRow('', 100, 10)]))

        expect(errors.length).to.equal(1)
      })

      it('when declared = true and many invalid row given', () => {
        const o: DebtRow = new DebtRow('my card', -100, 10) // invalid row
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, o, o, o, o, o, o, o, o]))

        expect(errors.length).to.equal(1)
      })

      it('when declared = true and many empty rows and one invalid given', () => {
        const o: DebtRow = DebtRow.empty()
        const errors = validator.validateSync(new Debts(true, [o, o, o, o, o, o, new DebtRow('my card', -100, 10), o]))

        expect(errors.length).to.equal(1)
      })
    })
  })
 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)
 })
 private static isValid (model): boolean {
   return model !== undefined && validator.validateSync(model).length === 0
 }
 it('undefined', () => {
   const errors = validator.validateSync(new HowMuchHaveYouPaid(validAmount, validLocalDate, undefined))
   expect(errors.length).to.equal(1)
   expectValidationError(errors, ValidationErrors.EXPLANATION_REQUIRED)
 })
      it('should return an error when `amount` is undefined', () => {
        const errors = validator.validateSync(new IncomeExpenseSource(undefined, IncomeExpenseSchedule.MONTH))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.NUMBER_REQUIRED)
      })
Example #27
0
      it('when both undefined', () => {
        const errors = validator.validateSync(new TimelineRow(undefined, undefined))

        expect(errors.length).to.equal(0)
      })
      it('should return an error when `amount` has invalid decimal amount', () => {
        const errors = validator.validateSync(new IncomeExpenseSource(0.123, IncomeExpenseSchedule.MONTH))

        expect(errors.length).to.equal(1)
        expectValidationError(errors, GlobalValidationErrors.AMOUNT_INVALID_DECIMALS)
      })
Example #29
0
      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('should reject without declaration', () => {
      const errors = validator.validateSync(new ExpertReports())

      expect(errors).to.not.be.empty
      expectValidationError(errors, GlobalValidationErrors.YES_NO_REQUIRED)
    })