AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
| 15 | */ |
| 16 | import Validator from 'src/nfvo-utils/Validator.js'; |
| 17 | |
| 18 | |
| 19 | describe('Validator ', () => { |
| 20 | |
| 21 | it('class does exist', () => { |
| 22 | expect(Validator).toBeTruthy(); |
| 23 | }); |
| 24 | |
| 25 | it('returns global validation functions', () => { |
| 26 | expect(Validator.globalValidationFunctions).toBeTruthy(); |
| 27 | }); |
| 28 | |
| 29 | it('returns global validation messages', () => { |
| 30 | expect(Validator.globalValidationMessagingFunctions).toBeTruthy(); |
| 31 | }); |
| 32 | |
| 33 | it('validates data per specific types list', () => { |
| 34 | const types = ['required', 'maxLength', 'minLength', 'pattern', 'numeric', 'maximum', 'minimum', |
| 35 | 'alphanumeric', 'alphanumericWithSpaces', 'validateName', 'validateVendorName', 'freeEnglishText', 'email', 'ip', 'url', 'maximumExclusive', 'minimumExclusive']; |
| 36 | |
| 37 | for (let i = 0; i < types.length; i++) { |
| 38 | expect(Validator.globalValidationFunctions[types[i]]).toBeTruthy(); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | it('gives validation messages per specific types list', () => { |
| 43 | const types = ['required', 'maxLength', 'minLength', 'pattern', 'numeric', 'maximum', 'minimum', |
| 44 | 'alphanumeric', 'alphanumericWithSpaces', 'validateName', 'validateVendorName', 'freeEnglishText', 'email', 'ip', 'url', 'maximumExclusive', 'minimumExclusive']; |
| 45 | |
| 46 | for (let i = 0; i < types.length; i++) { |
| 47 | expect(Validator.globalValidationFunctions[types[i]]).toBeTruthy(); |
| 48 | } |
| 49 | }); |
| 50 | |
| 51 | it('returns a validation response of {isValid, errorText} when validating only by validator.js', () => { |
| 52 | const result = Validator.validateItem('a', null, 'required'); |
| 53 | const keys = Object.keys(result); |
| 54 | expect(keys.length).toBe(2); |
| 55 | expect(keys).toContain('isValid'); |
| 56 | expect(keys).toContain('errorText'); |
| 57 | }); |
| 58 | |
| 59 | it('returns a validation response of {isValid, errorText} when validating with custom functions', () => { |
| 60 | const errorText = 'ran custom validation'; |
| 61 | const result = Validator.validate('myfield','a', [{type: 'required', data: null}], {}, { 'myfield' : () => { return { isValid: false, errorText};} }); |
| 62 | const keys = Object.keys(result); |
| 63 | expect(keys.length).toBe(2); |
| 64 | expect(keys).toContain('isValid'); |
| 65 | expect(keys).toContain('errorText'); |
| 66 | expect(result.errorText).toBe(errorText); |
| 67 | }); |
| 68 | |
| 69 | }); |