blob: f8350ed896279e016cb1e7a201ca28e07f123753 [file] [log] [blame]
Matteo Scandolob6bfee32016-04-22 12:05:37 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 4/15/16.
5 */
6
7(function () {
8 'use strict';
9
Matteo Scandolobe136072016-06-03 16:58:43 -070010 let compile, element, scope;
Matteo Scandolo62e0c612016-05-31 15:15:00 -070011
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 Scandolo62e0c612016-05-31 15:15:00 -070023 }
24
Matteo Scandolob6bfee32016-04-22 12:05:37 -070025 describe('The xos.helper module', function(){
26 describe('The xos-validation component', () => {
27
Matteo Scandolob6bfee32016-04-22 12:05:37 -070028 beforeEach(module('xos.helpers'));
29
Matteo Scandolo62e0c612016-05-31 15:15:00 -070030 describe('when the form has no errors', () => {
31 beforeEach(inject(($compile, $rootScope) => {
32 compile = $compile;
33 scope = $rootScope.$new();
Matteo Scandolob6bfee32016-04-22 12:05:37 -070034
Matteo Scandolo62e0c612016-05-31 15:15:00 -070035 scope.field = {
36 $error: {}
37 };
Matteo Scandolob6bfee32016-04-22 12:05:37 -070038
Matteo Scandolo62e0c612016-05-31 15:15:00 -070039 scope.form = {
40 $submitted: true
41 }
Matteo Scandolob6bfee32016-04-22 12:05:37 -070042
Matteo Scandolo62e0c612016-05-31 15:15:00 -070043 compileElement();
44 }));
Matteo Scandolob6bfee32016-04-22 12:05:37 -070045
Matteo Scandolo62e0c612016-05-31 15:15:00 -070046 it('should not show an alert by default', () => {
47 expect($(element).find('xos-alert > .alert')[0]).toHaveClass('ng-hide');
48 });
Matteo Scandolob6bfee32016-04-22 12:05:37 -070049 });
50
51 let availableErrors = [
52 {
Matteo Scandolo28e49b72016-04-22 14:14:03 -070053 type: 'required',
54 message: 'Field required'
55 },
56 {
Matteo Scandolob6bfee32016-04-22 12:05:37 -070057 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 Scandolo62e0c612016-05-31 15:15:00 -070077 scope.field.$error[e.type] = true;
78 compileElement();
Matteo Scandolob6bfee32016-04-22 12:05:37 -070079 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})();