blob: c85c934a4bfc480234255bbcb0d7c8904f893013 [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001/**
2 * © OpenCORD
3 *
4 * Created by teone on 5/25/16.
5 */
6
7(function () {
8 'use strict';
9
10 describe('The xos.helper module', function () {
11 describe('The xosCustomValidator directive', () => {
12 let element, scope, isolatedScope, rootScope, compile, form, input;
13 const compileElement = (el) => {
14 element = el;
15
16 if(!scope){
17 scope = rootScope.$new();
18 }
19 if(angular.isUndefined(element)){
20 element = angular.element(`
21 <form name="form">
22 <input name="testInput" type="text" ng-model="value" xos-custom-validator custom-validator="validator"/>
23 </form>
24 `);
25 }
26 compile(element)(scope);
27 scope.$digest();
28 input = $(element).find('input');
29 isolatedScope = angular.element(input).isolateScope();
30 form = scope.form;
31 };
32
33 beforeEach(module('xos.helpers'));
34
35 beforeEach(inject(function ($compile, $rootScope) {
36 compile = $compile;
37 rootScope = $rootScope;
38 }));
39
40 beforeEach(() => {
41 scope = rootScope.$new();
42 scope.validator = 'validator';
43 scope.value = '';
44 compileElement();
45 });
46
47 it('should bind the validator', () => {
48 expect(isolatedScope.fn).toEqual('validator');
49 });
50
51 describe('given a validator function', () => {
52
53 beforeEach(() => {
54 scope = rootScope.$new();
55 scope.value = '';
56 scope.validator = (model) => angular.equals(model, 'test');
57 spyOn(scope, 'validator').and.callThrough();
58 compileElement();
59 });
60
61 it('should call the validator function on value change', () => {
62 form.testInput.$setViewValue('something');
63 scope.$digest();
64 expect(scope.validator).toHaveBeenCalledWith('something');
65 expect(scope.value).toEqual('something');
66 });
67
68 it('should set the field invalid', () => {
69 form.testInput.$setViewValue('something');
70 scope.$digest();
71 expect(scope.validator).toHaveBeenCalledWith('something');
72 expect(input).toHaveClass('ng-invalid');
73 expect(input).toHaveClass('ng-invalid-custom');
74 });
75
76 it('should set the field valid', () => {
77 form.testInput.$setViewValue('test');
78 scope.$digest();
79 expect(scope.validator).toHaveBeenCalledWith('test');
80 expect(input).not.toHaveClass('ng-invalid');
81 expect(input).not.toHaveClass('ng-invalid-custom');
82 });
83
84 describe('if the validation function return an array', () => {
85
86 beforeEach(() => {
87 scope = rootScope.$new();
88 scope.value = '';
89 scope.validator = (model) => {
90 return ['randomTest', angular.equals(model, 'test')];
91 };
92 spyOn(scope, 'validator').and.callThrough();
93 compileElement();
94 });
95
96 it('should set the field invalid', () => {
97 form.testInput.$setViewValue('something');
98 scope.$digest();
99 expect(scope.validator).toHaveBeenCalledWith('something');
100 expect(input).toHaveClass('ng-invalid');
101 expect(input).toHaveClass('ng-invalid-random-test');
102 });
103 });
104 });
105 });
106 });
107})();