blob: e5b722305f1812a97866b0d1e852014249178da8 [file] [log] [blame]
Simon Huntf3069722014-12-16 18:15:37 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Huntf3069722014-12-16 18:15:37 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
Simon Huntdc6362a2014-12-18 19:55:23 -080018 ONOS GUI -- Util -- General Purpose Functions - Unit Tests
Simon Huntf3069722014-12-16 18:15:37 -080019 */
Yuta HIGUCHI4f39bcd2014-12-18 20:46:14 -080020describe('factory: fw/util/fn.js', function() {
Simon Hunta11b4eb2015-01-28 16:20:50 -080021 var $window,
22 fs,
Simon Huntf3069722014-12-16 18:15:37 -080023 someFunction = function () {},
24 someArray = [1, 2, 3],
25 someObject = { foo: 'bar'},
26 someNumber = 42,
27 someString = 'xyyzy',
Simon Hunt59df0b22014-12-17 10:32:25 -080028 someDate = new Date(),
29 stringArray = ['foo', 'bar'];
Simon Huntf3069722014-12-16 18:15:37 -080030
Simon Huntdc6362a2014-12-18 19:55:23 -080031 beforeEach(module('onosUtil'));
Simon Huntf3069722014-12-16 18:15:37 -080032
Simon Hunta11b4eb2015-01-28 16:20:50 -080033 beforeEach(inject(function (_$window_, FnService) {
34 $window = _$window_;
Simon Huntf3069722014-12-16 18:15:37 -080035 fs = FnService;
Simon Hunta11b4eb2015-01-28 16:20:50 -080036
37 $window.innerWidth = 400;
38 $window.innerHeight = 200;
Simon Huntf3069722014-12-16 18:15:37 -080039 }));
40
Simon Huntf3069722014-12-16 18:15:37 -080041
42 // === Tests for isF()
43 it('isF(): null for undefined', function () {
44 expect(fs.isF(undefined)).toBeNull();
45 });
46 it('isF(): null for null', function () {
47 expect(fs.isF(null)).toBeNull();
48 });
49 it('isF(): the reference for function', function () {
50 expect(fs.isF(someFunction)).toBe(someFunction);
51 });
52 it('isF(): null for string', function () {
53 expect(fs.isF(someString)).toBeNull();
54 });
55 it('isF(): null for number', function () {
56 expect(fs.isF(someNumber)).toBeNull();
57 });
58 it('isF(): null for Date', function () {
59 expect(fs.isF(someDate)).toBeNull();
60 });
61 it('isF(): null for array', function () {
62 expect(fs.isF(someArray)).toBeNull();
63 });
64 it('isF(): null for object', function () {
65 expect(fs.isF(someObject)).toBeNull();
66 });
67
68
69 // === Tests for isA()
70 it('isA(): null for undefined', function () {
71 expect(fs.isA(undefined)).toBeNull();
72 });
73 it('isA(): null for null', function () {
74 expect(fs.isA(null)).toBeNull();
75 });
76 it('isA(): null for function', function () {
77 expect(fs.isA(someFunction)).toBeNull();
78 });
79 it('isA(): null for string', function () {
80 expect(fs.isA(someString)).toBeNull();
81 });
82 it('isA(): null for number', function () {
83 expect(fs.isA(someNumber)).toBeNull();
84 });
85 it('isA(): null for Date', function () {
86 expect(fs.isA(someDate)).toBeNull();
87 });
88 it('isA(): the reference for array', function () {
89 expect(fs.isA(someArray)).toBe(someArray);
90 });
91 it('isA(): null for object', function () {
92 expect(fs.isA(someObject)).toBeNull();
93 });
94
95
96 // === Tests for isS()
97 it('isS(): null for undefined', function () {
98 expect(fs.isS(undefined)).toBeNull();
99 });
100 it('isS(): null for null', function () {
101 expect(fs.isS(null)).toBeNull();
102 });
103 it('isS(): null for function', function () {
104 expect(fs.isS(someFunction)).toBeNull();
105 });
106 it('isS(): the reference for string', function () {
107 expect(fs.isS(someString)).toBe(someString);
108 });
109 it('isS(): null for number', function () {
110 expect(fs.isS(someNumber)).toBeNull();
111 });
112 it('isS(): null for Date', function () {
113 expect(fs.isS(someDate)).toBeNull();
114 });
115 it('isS(): null for array', function () {
116 expect(fs.isS(someArray)).toBeNull();
117 });
118 it('isS(): null for object', function () {
119 expect(fs.isS(someObject)).toBeNull();
120 });
121
122
123 // === Tests for isO()
124 it('isO(): null for undefined', function () {
125 expect(fs.isO(undefined)).toBeNull();
126 });
127 it('isO(): null for null', function () {
128 expect(fs.isO(null)).toBeNull();
129 });
130 it('isO(): null for function', function () {
131 expect(fs.isO(someFunction)).toBeNull();
132 });
133 it('isO(): null for string', function () {
134 expect(fs.isO(someString)).toBeNull();
135 });
136 it('isO(): null for number', function () {
137 expect(fs.isO(someNumber)).toBeNull();
138 });
139 it('isO(): null for Date', function () {
140 expect(fs.isO(someDate)).toBeNull();
141 });
142 it('isO(): null for array', function () {
143 expect(fs.isO(someArray)).toBeNull();
144 });
145 it('isO(): the reference for object', function () {
146 expect(fs.isO(someObject)).toBe(someObject);
147 });
Simon Hunt59df0b22014-12-17 10:32:25 -0800148
149 // === Tests for contains()
150 it('contains(): false for improper args', function () {
151 expect(fs.contains()).toBeFalsy();
152 });
153 it('contains(): false for non-array', function () {
154 expect(fs.contains(null, 1)).toBeFalsy();
155 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800156 it('contains(): true for contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800157 expect(fs.contains(someArray, 1)).toBeTruthy();
158 expect(fs.contains(stringArray, 'bar')).toBeTruthy();
159 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800160 it('contains(): false for non-contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800161 expect(fs.contains(someArray, 109)).toBeFalsy();
162 expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
163 });
Simon Hunt51fc40b2015-01-06 13:56:12 -0800164
165 // === Tests for areFunctions()
166 it('areFunctions(): false for non-array', function () {
167 expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
168 });
169 it('areFunctions(): true for empty-array', function () {
170 expect(fs.areFunctions({}, [])).toBeTruthy();
171 });
172 it('areFunctions(): true for some api', function () {
173 expect(fs.areFunctions({
174 a: function () {},
175 b: function () {}
176 }, ['b', 'a'])).toBeTruthy();
177 });
178 it('areFunctions(): false for some other api', function () {
179 expect(fs.areFunctions({
180 a: function () {},
181 b: 'not-a-function'
182 }, ['b', 'a'])).toBeFalsy();
183 });
184 it('areFunctions(): extraneous stuff ignored', function () {
185 expect(fs.areFunctions({
186 a: function () {},
187 b: function () {},
188 c: 1,
189 d: 'foo'
190 }, ['a', 'b'])).toBeTruthy();
191 });
192
193
Simon Hunta11b4eb2015-01-28 16:20:50 -0800194 // === Tests for windowSize()
195 it('windowSize(): noargs', function () {
196 var dim = fs.windowSize();
197 expect(dim.width).toEqual(400);
198 expect(dim.height).toEqual(200);
199 });
200
201 it('windowSize(): adjust height', function () {
202 var dim = fs.windowSize(50);
203 expect(dim.width).toEqual(400);
204 expect(dim.height).toEqual(150);
205 });
206
207 it('windowSize(): adjust width', function () {
208 var dim = fs.windowSize(0, 50);
209 expect(dim.width).toEqual(350);
210 expect(dim.height).toEqual(200);
211 });
212
213 it('windowSize(): adjust width and height', function () {
214 var dim = fs.windowSize(101, 201);
215 expect(dim.width).toEqual(199);
216 expect(dim.height).toEqual(99);
217 });
Simon Huntf3069722014-12-16 18:15:37 -0800218});