|
| 1 | +import { ValidationError } from './validation-error'; |
| 2 | + |
| 3 | +describe('ValidationError', () => { |
| 4 | + it(`fromFirstError takes the first error object from a FormControl`, () => { |
| 5 | + const requiredErrorObject = {}; |
| 6 | + const control: any = { |
| 7 | + errors: { |
| 8 | + required: requiredErrorObject, |
| 9 | + test: true |
| 10 | + } |
| 11 | + }; |
| 12 | + const error = ValidationError.fromFirstError(control); |
| 13 | + |
| 14 | + expect(error.control).toEqual(control); |
| 15 | + expect(error.key).toBe('required'); |
| 16 | + expect(error.errorObject).toEqual(requiredErrorObject); |
| 17 | + }); |
| 18 | + |
| 19 | + it(`fromFirstError returns undefined when the FormControl has no errors`, () => { |
| 20 | + const control: any = { |
| 21 | + errors: null |
| 22 | + }; |
| 23 | + const error = ValidationError.fromFirstError(control); |
| 24 | + |
| 25 | + expect(error).toBe(undefined); |
| 26 | + }); |
| 27 | + |
| 28 | + it(`hasMessage returns true when the errorObject contains a message`, () => { |
| 29 | + const control: any = { |
| 30 | + errors: { |
| 31 | + required: { |
| 32 | + message: 'This is the expected message' |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | + const error = ValidationError.fromFirstError(control); |
| 37 | + expect(error.hasMessage()).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it(`hasMessage returns false when the errorObject doesn't contain a message`, () => { |
| 41 | + const control: any = { |
| 42 | + errors: { |
| 43 | + required: {} |
| 44 | + } |
| 45 | + }; |
| 46 | + const error = ValidationError.fromFirstError(control); |
| 47 | + expect(error.hasMessage()).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it(`getMessage returns the message from the errorObject`, () => { |
| 51 | + const expected = 'This is the expected message'; |
| 52 | + const control: any = { |
| 53 | + errors: { |
| 54 | + required: { |
| 55 | + message: expected |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + const error = ValidationError.fromFirstError(control); |
| 60 | + expect(error.getMessage()).toBe(expected); |
| 61 | + }); |
| 62 | +}); |
0 commit comments