Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Created by teone on 4/15/16. |
| 5 | */ |
| 6 | |
| 7 | (function () { |
| 8 | 'use strict'; |
| 9 | |
| 10 | let compile, element, scope, rootScope; |
| 11 | |
| 12 | const compileElement = (el) => { |
| 13 | element = el; |
| 14 | |
| 15 | if(!scope){ |
| 16 | scope = rootScope.$new(); |
| 17 | } |
| 18 | if(angular.isUndefined(element)){ |
| 19 | element = angular.element('<xos-validation field="field" form="form"></xos-validation>'); |
| 20 | } |
| 21 | compile(element)(scope); |
| 22 | scope.$digest(); |
| 23 | } |
| 24 | |
| 25 | describe('The xos.helper module', function(){ |
| 26 | describe('The xos-validation component', () => { |
| 27 | |
| 28 | beforeEach(module('xos.helpers')); |
| 29 | |
| 30 | describe('when the form has no errors', () => { |
| 31 | beforeEach(inject(($compile, $rootScope) => { |
| 32 | compile = $compile; |
| 33 | scope = $rootScope.$new(); |
| 34 | |
| 35 | scope.field = { |
| 36 | $error: {} |
| 37 | }; |
| 38 | |
| 39 | scope.form = { |
| 40 | $submitted: true |
| 41 | } |
| 42 | |
| 43 | compileElement(); |
| 44 | })); |
| 45 | |
| 46 | it('should not show an alert by default', () => { |
| 47 | expect($(element).find('xos-alert > .alert')[0]).toHaveClass('ng-hide'); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | let availableErrors = [ |
| 52 | { |
| 53 | type: 'required', |
| 54 | message: 'Field required' |
| 55 | }, |
| 56 | { |
| 57 | type: 'email', |
| 58 | message: 'This is not a valid email' |
| 59 | }, |
| 60 | { |
| 61 | type: 'minlength', |
| 62 | message: 'Too short' |
| 63 | }, |
| 64 | { |
| 65 | type: 'maxlength', |
| 66 | message: 'Too long' |
| 67 | }, |
| 68 | { |
| 69 | type: 'custom', |
| 70 | message: 'Field invalid' |
| 71 | }, |
| 72 | ]; |
| 73 | |
| 74 | // use a loop to generate similar test |
| 75 | availableErrors.forEach((e, i) => { |
| 76 | it(`should show an alert for ${e.type} errors`, () => { |
| 77 | scope.field.$error[e.type] = true; |
| 78 | compileElement(); |
| 79 | let alert = $(element).find('xos-alert > .alert')[i]; |
| 80 | expect(alert).not.toHaveClass('ng-hide'); |
| 81 | expect(alert).toHaveText(e.message); |
| 82 | }); |
| 83 | }); |
| 84 | }); |
| 85 | }); |
| 86 | })(); |