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