Matteo Scandolo | b6bfee3 | 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 | be13607 | 2016-06-03 16:58:43 -0700 | [diff] [blame] | 10 | let compile, element, scope; |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 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(); |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 25 | describe('The xos.helper module', function(){ |
| 26 | describe('The xos-validation component', () => { |
| 27 | |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 28 | beforeEach(module('xos.helpers')); |
| 29 | |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 30 | describe('when the form has no errors', () => { |
| 31 | beforeEach(inject(($compile, $rootScope) => { |
| 32 | compile = $compile; |
| 33 | scope = $rootScope.$new(); |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 34 | |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 35 | scope.field = { |
| 36 | $error: {} |
| 37 | }; |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 38 | |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 39 | scope.form = { |
| 40 | $submitted: true |
| 41 | } |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 42 | |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 43 | compileElement(); |
| 44 | })); |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 45 | |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 46 | it('should not show an alert by default', () => { |
| 47 | expect($(element).find('xos-alert > .alert')[0]).toHaveClass('ng-hide'); |
| 48 | }); |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 49 | }); |
| 50 | |
| 51 | let availableErrors = [ |
| 52 | { |
Matteo Scandolo | 28e49b7 | 2016-04-22 14:14:03 -0700 | [diff] [blame] | 53 | type: 'required', |
| 54 | message: 'Field required' |
| 55 | }, |
| 56 | { |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 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`, () => { |
Matteo Scandolo | 62e0c61 | 2016-05-31 15:15:00 -0700 | [diff] [blame] | 77 | scope.field.$error[e.type] = true; |
| 78 | compileElement(); |
Matteo Scandolo | b6bfee3 | 2016-04-22 12:05:37 -0700 | [diff] [blame] | 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 | })(); |