blob: 55a1afcfd6ebb17509bc4a3b86dba7b5115c58ff [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/**
20 * © OpenCORD
21 *
22 * Created by teone on 5/25/16.
23 */
24
25(function () {
26 'use strict';
27
28 describe('The xos.helper module', function () {
29 describe('The xosCustomValidator directive', () => {
30 let element, scope, isolatedScope, rootScope, compile, form, input;
31 const compileElement = (el) => {
32 element = el;
33
34 if(!scope){
35 scope = rootScope.$new();
36 }
37 if(angular.isUndefined(element)){
38 element = angular.element(`
39 <form name="form">
40 <input name="testInput" type="text" ng-model="value" xos-custom-validator custom-validator="validator"/>
41 </form>
42 `);
43 }
44 compile(element)(scope);
45 scope.$digest();
46 input = $(element).find('input');
47 isolatedScope = angular.element(input).isolateScope();
48 form = scope.form;
49 };
50
51 beforeEach(module('xos.helpers'));
52
53 beforeEach(inject(function ($compile, $rootScope) {
54 compile = $compile;
55 rootScope = $rootScope;
56 }));
57
58 beforeEach(() => {
59 scope = rootScope.$new();
60 scope.validator = 'validator';
61 scope.value = '';
62 compileElement();
63 });
64
65 it('should bind the validator', () => {
66 expect(isolatedScope.fn).toEqual('validator');
67 });
68
69 describe('given a validator function', () => {
70
71 beforeEach(() => {
72 scope = rootScope.$new();
73 scope.value = '';
74 scope.validator = (model) => angular.equals(model, 'test');
75 spyOn(scope, 'validator').and.callThrough();
76 compileElement();
77 });
78
79 it('should call the validator function on value change', () => {
80 form.testInput.$setViewValue('something');
81 scope.$digest();
82 expect(scope.validator).toHaveBeenCalledWith('something');
83 expect(scope.value).toEqual('something');
84 });
85
86 it('should set the field invalid', () => {
87 form.testInput.$setViewValue('something');
88 scope.$digest();
89 expect(scope.validator).toHaveBeenCalledWith('something');
90 expect(input).toHaveClass('ng-invalid');
91 expect(input).toHaveClass('ng-invalid-custom');
92 });
93
94 it('should set the field valid', () => {
95 form.testInput.$setViewValue('test');
96 scope.$digest();
97 expect(scope.validator).toHaveBeenCalledWith('test');
98 expect(input).not.toHaveClass('ng-invalid');
99 expect(input).not.toHaveClass('ng-invalid-custom');
100 });
101
102 describe('if the validation function return an array', () => {
103
104 beforeEach(() => {
105 scope = rootScope.$new();
106 scope.value = '';
107 scope.validator = (model) => {
108 return ['randomTest', angular.equals(model, 'test')];
109 };
110 spyOn(scope, 'validator').and.callThrough();
111 compileElement();
112 });
113
114 it('should set the field invalid', () => {
115 form.testInput.$setViewValue('something');
116 scope.$digest();
117 expect(scope.validator).toHaveBeenCalledWith('something');
118 expect(input).toHaveClass('ng-invalid');
119 expect(input).toHaveClass('ng-invalid-random-test');
120 });
121 });
122 });
123 });
124 });
125})();