Matteo Scandolo | 686547a | 2017-08-08 13:05:25 -0700 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 19 | /** |
| 20 | * © OpenCORD |
| 21 | * |
| 22 | * Created by teone on 4/15/16. |
| 23 | */ |
| 24 | |
| 25 | (function () { |
| 26 | 'use strict'; |
| 27 | |
| 28 | let compile, element, scope, rootScope; |
| 29 | |
| 30 | const compileElement = (el) => { |
| 31 | element = el; |
| 32 | |
| 33 | if(!scope){ |
| 34 | scope = rootScope.$new(); |
| 35 | } |
| 36 | if(angular.isUndefined(element)){ |
| 37 | element = angular.element('<xos-validation field="field" form="form"></xos-validation>'); |
| 38 | } |
| 39 | compile(element)(scope); |
| 40 | scope.$digest(); |
| 41 | } |
| 42 | |
| 43 | describe('The xos.helper module', function(){ |
| 44 | describe('The xos-validation component', () => { |
| 45 | |
| 46 | beforeEach(module('xos.helpers')); |
| 47 | |
| 48 | describe('when the form has no errors', () => { |
| 49 | beforeEach(inject(($compile, $rootScope) => { |
| 50 | compile = $compile; |
| 51 | scope = $rootScope.$new(); |
| 52 | |
| 53 | scope.field = { |
| 54 | $error: {} |
| 55 | }; |
| 56 | |
| 57 | scope.form = { |
| 58 | $submitted: true |
| 59 | } |
| 60 | |
| 61 | compileElement(); |
| 62 | })); |
| 63 | |
| 64 | it('should not show an alert by default', () => { |
| 65 | expect($(element).find('xos-alert > .alert')[0]).toHaveClass('ng-hide'); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | let availableErrors = [ |
| 70 | { |
| 71 | type: 'required', |
| 72 | message: 'Field required' |
| 73 | }, |
| 74 | { |
| 75 | type: 'email', |
| 76 | message: 'This is not a valid email' |
| 77 | }, |
| 78 | { |
| 79 | type: 'minlength', |
| 80 | message: 'Too short' |
| 81 | }, |
| 82 | { |
| 83 | type: 'maxlength', |
| 84 | message: 'Too long' |
| 85 | }, |
| 86 | { |
| 87 | type: 'custom', |
| 88 | message: 'Field invalid' |
| 89 | }, |
| 90 | ]; |
| 91 | |
| 92 | // use a loop to generate similar test |
| 93 | availableErrors.forEach((e, i) => { |
| 94 | it(`should show an alert for ${e.type} errors`, () => { |
| 95 | scope.field.$error[e.type] = true; |
| 96 | compileElement(); |
| 97 | let alert = $(element).find('xos-alert > .alert')[i]; |
| 98 | expect(alert).not.toHaveClass('ng-hide'); |
| 99 | expect(alert).toHaveText(e.message); |
| 100 | }); |
| 101 | }); |
| 102 | }); |
| 103 | }); |
| 104 | })(); |