blob: e21980de225bfb378aeef44b06551f376e673973 [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 // === Tests for isF()
42 it('isF(): null for undefined', function () {
43 expect(fs.isF(undefined)).toBeNull();
44 });
45 it('isF(): null for null', function () {
46 expect(fs.isF(null)).toBeNull();
47 });
48 it('isF(): the reference for function', function () {
49 expect(fs.isF(someFunction)).toBe(someFunction);
50 });
51 it('isF(): null for string', function () {
52 expect(fs.isF(someString)).toBeNull();
53 });
54 it('isF(): null for number', function () {
55 expect(fs.isF(someNumber)).toBeNull();
56 });
57 it('isF(): null for Date', function () {
58 expect(fs.isF(someDate)).toBeNull();
59 });
60 it('isF(): null for array', function () {
61 expect(fs.isF(someArray)).toBeNull();
62 });
63 it('isF(): null for object', function () {
64 expect(fs.isF(someObject)).toBeNull();
65 });
66
67
68 // === Tests for isA()
69 it('isA(): null for undefined', function () {
70 expect(fs.isA(undefined)).toBeNull();
71 });
72 it('isA(): null for null', function () {
73 expect(fs.isA(null)).toBeNull();
74 });
75 it('isA(): null for function', function () {
76 expect(fs.isA(someFunction)).toBeNull();
77 });
78 it('isA(): null for string', function () {
79 expect(fs.isA(someString)).toBeNull();
80 });
81 it('isA(): null for number', function () {
82 expect(fs.isA(someNumber)).toBeNull();
83 });
84 it('isA(): null for Date', function () {
85 expect(fs.isA(someDate)).toBeNull();
86 });
87 it('isA(): the reference for array', function () {
88 expect(fs.isA(someArray)).toBe(someArray);
89 });
90 it('isA(): null for object', function () {
91 expect(fs.isA(someObject)).toBeNull();
92 });
93
94
95 // === Tests for isS()
96 it('isS(): null for undefined', function () {
97 expect(fs.isS(undefined)).toBeNull();
98 });
99 it('isS(): null for null', function () {
100 expect(fs.isS(null)).toBeNull();
101 });
102 it('isS(): null for function', function () {
103 expect(fs.isS(someFunction)).toBeNull();
104 });
105 it('isS(): the reference for string', function () {
106 expect(fs.isS(someString)).toBe(someString);
107 });
108 it('isS(): null for number', function () {
109 expect(fs.isS(someNumber)).toBeNull();
110 });
111 it('isS(): null for Date', function () {
112 expect(fs.isS(someDate)).toBeNull();
113 });
114 it('isS(): null for array', function () {
115 expect(fs.isS(someArray)).toBeNull();
116 });
117 it('isS(): null for object', function () {
118 expect(fs.isS(someObject)).toBeNull();
119 });
120
121
122 // === Tests for isO()
123 it('isO(): null for undefined', function () {
124 expect(fs.isO(undefined)).toBeNull();
125 });
126 it('isO(): null for null', function () {
127 expect(fs.isO(null)).toBeNull();
128 });
129 it('isO(): null for function', function () {
130 expect(fs.isO(someFunction)).toBeNull();
131 });
132 it('isO(): null for string', function () {
133 expect(fs.isO(someString)).toBeNull();
134 });
135 it('isO(): null for number', function () {
136 expect(fs.isO(someNumber)).toBeNull();
137 });
138 it('isO(): null for Date', function () {
139 expect(fs.isO(someDate)).toBeNull();
140 });
141 it('isO(): null for array', function () {
142 expect(fs.isO(someArray)).toBeNull();
143 });
144 it('isO(): the reference for object', function () {
145 expect(fs.isO(someObject)).toBe(someObject);
146 });
Simon Hunt59df0b22014-12-17 10:32:25 -0800147
148 // === Tests for contains()
149 it('contains(): false for improper args', function () {
150 expect(fs.contains()).toBeFalsy();
151 });
152 it('contains(): false for non-array', function () {
153 expect(fs.contains(null, 1)).toBeFalsy();
154 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800155 it('contains(): true for contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800156 expect(fs.contains(someArray, 1)).toBeTruthy();
157 expect(fs.contains(stringArray, 'bar')).toBeTruthy();
158 });
Simon Huntdc6362a2014-12-18 19:55:23 -0800159 it('contains(): false for non-contained item', function () {
Simon Hunt59df0b22014-12-17 10:32:25 -0800160 expect(fs.contains(someArray, 109)).toBeFalsy();
161 expect(fs.contains(stringArray, 'zonko')).toBeFalsy();
162 });
Simon Hunt51fc40b2015-01-06 13:56:12 -0800163
164 // === Tests for areFunctions()
165 it('areFunctions(): false for non-array', function () {
166 expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy();
167 });
168 it('areFunctions(): true for empty-array', function () {
169 expect(fs.areFunctions({}, [])).toBeTruthy();
170 });
171 it('areFunctions(): true for some api', function () {
172 expect(fs.areFunctions({
173 a: function () {},
174 b: function () {}
175 }, ['b', 'a'])).toBeTruthy();
176 });
177 it('areFunctions(): false for some other api', function () {
178 expect(fs.areFunctions({
179 a: function () {},
180 b: 'not-a-function'
181 }, ['b', 'a'])).toBeFalsy();
182 });
Simon Hunt48e61672015-01-30 14:48:25 -0800183 it('areFunctions(): extraneous stuff NOT ignored', function () {
Simon Hunt51fc40b2015-01-06 13:56:12 -0800184 expect(fs.areFunctions({
185 a: function () {},
186 b: function () {},
187 c: 1,
188 d: 'foo'
Simon Hunt48e61672015-01-30 14:48:25 -0800189 }, ['a', 'b'])).toBeFalsy();
190 });
191 it('areFunctions(): extraneous stuff ignored (alternate fn)', function () {
192 expect(fs.areFunctionsNonStrict({
193 a: function () {},
194 b: function () {},
195 c: 1,
196 d: 'foo'
Simon Hunt51fc40b2015-01-06 13:56:12 -0800197 }, ['a', 'b'])).toBeTruthy();
198 });
199
Simon Hunt48e61672015-01-30 14:48:25 -0800200 // == use the now-tested areFunctions on our own api:
201 it('should define api functions', function () {
202 expect(fs.areFunctions(fs, [
203 'isF', 'isA', 'isS', 'isO', 'contains',
204 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'find'
205 ])).toBeTruthy();
206 });
207
208
209
Simon Hunt51fc40b2015-01-06 13:56:12 -0800210
Simon Hunta11b4eb2015-01-28 16:20:50 -0800211 // === Tests for windowSize()
212 it('windowSize(): noargs', function () {
213 var dim = fs.windowSize();
214 expect(dim.width).toEqual(400);
215 expect(dim.height).toEqual(200);
216 });
217
218 it('windowSize(): adjust height', function () {
219 var dim = fs.windowSize(50);
220 expect(dim.width).toEqual(400);
221 expect(dim.height).toEqual(150);
222 });
223
224 it('windowSize(): adjust width', function () {
225 var dim = fs.windowSize(0, 50);
226 expect(dim.width).toEqual(350);
227 expect(dim.height).toEqual(200);
228 });
229
230 it('windowSize(): adjust width and height', function () {
231 var dim = fs.windowSize(101, 201);
232 expect(dim.width).toEqual(199);
233 expect(dim.height).toEqual(99);
234 });
Simon Hunt48e61672015-01-30 14:48:25 -0800235
236 // TODO: write unit tests for find()
237
Simon Huntf3069722014-12-16 18:15:37 -0800238});