blob: bb663fa2b69dba4f249435697e5b1b7536a5ea5b [file] [log] [blame]
Matteo Scandoloebf4fad2016-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 Scandolo5dfcb082016-05-31 15:15:00 -070010 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 Scandoloebf4fad2016-04-22 12:05:37 -070026 describe('The xos.helper module', function(){
27 describe('The xos-validation component', () => {
28
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070029 beforeEach(module('xos.helpers'));
30
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070031 describe('when the form has no errors', () => {
32 beforeEach(inject(($compile, $rootScope) => {
33 compile = $compile;
34 scope = $rootScope.$new();
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070035
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070036 scope.field = {
37 $error: {}
38 };
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070039
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070040 scope.form = {
41 $submitted: true
42 }
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070043
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070044 compileElement();
45 }));
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070046
Matteo Scandolo5dfcb082016-05-31 15:15:00 -070047 it('should not show an alert by default', () => {
48 expect($(element).find('xos-alert > .alert')[0]).toHaveClass('ng-hide');
49 });
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070050 });
51
52 let availableErrors = [
53 {
Matteo Scandolob3d686f2016-04-22 14:14:03 -070054 type: 'required',
55 message: 'Field required'
56 },
57 {
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070058 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 Scandolo5dfcb082016-05-31 15:15:00 -070078 scope.field.$error[e.type] = true;
79 compileElement();
Matteo Scandoloebf4fad2016-04-22 12:05:37 -070080 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})();