1 | "use strict";
|
2 | import 'source-map-support/register';
|
3 | const utils = require("../../lib/utils");
|
4 | const testUtils = require("../test-utils");
|
5 |
|
6 | import expect from 'expect';
|
7 |
|
8 | describe("utils", function() {
|
9 | beforeEach(function() {
|
10 | testUtils.beforeEach(this);
|
11 | });
|
12 |
|
13 | describe("encodeParams", function() {
|
14 | it("should url encode and concat with &s", function() {
|
15 | const params = {
|
16 | foo: "bar",
|
17 | baz: "beer@",
|
18 | };
|
19 | expect(utils.encodeParams(params)).toEqual(
|
20 | "foo=bar&baz=beer%40",
|
21 | );
|
22 | });
|
23 | });
|
24 |
|
25 | describe("encodeUri", function() {
|
26 | it("should replace based on object keys and url encode", function() {
|
27 | const path = "foo/bar/%something/%here";
|
28 | const vals = {
|
29 | "%something": "baz",
|
30 | "%here": "beer@",
|
31 | };
|
32 | expect(utils.encodeUri(path, vals)).toEqual(
|
33 | "foo/bar/baz/beer%40",
|
34 | );
|
35 | });
|
36 | });
|
37 |
|
38 | describe("forEach", function() {
|
39 | it("should be invoked for each element", function() {
|
40 | const arr = [];
|
41 | utils.forEach([55, 66, 77], function(element) {
|
42 | arr.push(element);
|
43 | });
|
44 | expect(arr).toEqual([55, 66, 77]);
|
45 | });
|
46 | });
|
47 |
|
48 | describe("findElement", function() {
|
49 | it("should find only 1 element if there is a match", function() {
|
50 | const matchFn = function() {
|
51 | return true;
|
52 | };
|
53 | const arr = [55, 66, 77];
|
54 | expect(utils.findElement(arr, matchFn)).toEqual(55);
|
55 | });
|
56 | it("should be able to find in reverse order", function() {
|
57 | const matchFn = function() {
|
58 | return true;
|
59 | };
|
60 | const arr = [55, 66, 77];
|
61 | expect(utils.findElement(arr, matchFn, true)).toEqual(77);
|
62 | });
|
63 | it("should find nothing if the function never returns true", function() {
|
64 | const matchFn = function() {
|
65 | return false;
|
66 | };
|
67 | const arr = [55, 66, 77];
|
68 | expect(utils.findElement(arr, matchFn)).toBeFalsy();
|
69 | });
|
70 | });
|
71 |
|
72 | describe("removeElement", function() {
|
73 | it("should remove only 1 element if there is a match", function() {
|
74 | const matchFn = function() {
|
75 | return true;
|
76 | };
|
77 | const arr = [55, 66, 77];
|
78 | utils.removeElement(arr, matchFn);
|
79 | expect(arr).toEqual([66, 77]);
|
80 | });
|
81 | it("should be able to remove in reverse order", function() {
|
82 | const matchFn = function() {
|
83 | return true;
|
84 | };
|
85 | const arr = [55, 66, 77];
|
86 | utils.removeElement(arr, matchFn, true);
|
87 | expect(arr).toEqual([55, 66]);
|
88 | });
|
89 | it("should remove nothing if the function never returns true", function() {
|
90 | const matchFn = function() {
|
91 | return false;
|
92 | };
|
93 | const arr = [55, 66, 77];
|
94 | utils.removeElement(arr, matchFn);
|
95 | expect(arr).toEqual(arr);
|
96 | });
|
97 | });
|
98 |
|
99 | describe("isFunction", function() {
|
100 | it("should return true for functions", function() {
|
101 | expect(utils.isFunction([])).toBe(false);
|
102 | expect(utils.isFunction([5, 3, 7])).toBe(false);
|
103 | expect(utils.isFunction()).toBe(false);
|
104 | expect(utils.isFunction(null)).toBe(false);
|
105 | expect(utils.isFunction({})).toBe(false);
|
106 | expect(utils.isFunction("foo")).toBe(false);
|
107 | expect(utils.isFunction(555)).toBe(false);
|
108 |
|
109 | expect(utils.isFunction(function() {})).toBe(true);
|
110 | const s = { foo: function() {} };
|
111 | expect(utils.isFunction(s.foo)).toBe(true);
|
112 | });
|
113 | });
|
114 |
|
115 | describe("isArray", function() {
|
116 | it("should return true for arrays", function() {
|
117 | expect(utils.isArray([])).toBe(true);
|
118 | expect(utils.isArray([5, 3, 7])).toBe(true);
|
119 |
|
120 | expect(utils.isArray()).toBe(false);
|
121 | expect(utils.isArray(null)).toBe(false);
|
122 | expect(utils.isArray({})).toBe(false);
|
123 | expect(utils.isArray("foo")).toBe(false);
|
124 | expect(utils.isArray(555)).toBe(false);
|
125 | expect(utils.isArray(function() {})).toBe(false);
|
126 | });
|
127 | });
|
128 |
|
129 | describe("checkObjectHasKeys", function() {
|
130 | it("should throw for missing keys", function() {
|
131 | expect(function() {
|
132 | utils.checkObjectHasKeys({}, ["foo"]);
|
133 | }).toThrow();
|
134 | expect(function() {
|
135 | utils.checkObjectHasKeys({
|
136 | foo: "bar",
|
137 | }, ["foo"]);
|
138 | }).toNotThrow();
|
139 | });
|
140 | });
|
141 |
|
142 | describe("checkObjectHasNoAdditionalKeys", function() {
|
143 | it("should throw for extra keys", function() {
|
144 | expect(function() {
|
145 | utils.checkObjectHasNoAdditionalKeys({
|
146 | foo: "bar",
|
147 | baz: 4,
|
148 | }, ["foo"]);
|
149 | }).toThrow();
|
150 |
|
151 | expect(function() {
|
152 | utils.checkObjectHasNoAdditionalKeys({
|
153 | foo: "bar",
|
154 | }, ["foo"]);
|
155 | }).toNotThrow();
|
156 | });
|
157 | });
|
158 |
|
159 | describe("deepCompare", function() {
|
160 | const assert = {
|
161 | isTrue: function(x) {
|
162 | expect(x).toBe(true);
|
163 | },
|
164 | isFalse: function(x) {
|
165 | expect(x).toBe(false);
|
166 | },
|
167 | };
|
168 |
|
169 | it("should handle primitives", function() {
|
170 | assert.isTrue(utils.deepCompare(null, null));
|
171 | assert.isFalse(utils.deepCompare(null, undefined));
|
172 | assert.isTrue(utils.deepCompare("hi", "hi"));
|
173 | assert.isTrue(utils.deepCompare(5, 5));
|
174 | assert.isFalse(utils.deepCompare(5, 10));
|
175 | });
|
176 |
|
177 | it("should handle regexps", function() {
|
178 | assert.isTrue(utils.deepCompare(/abc/, /abc/));
|
179 | assert.isFalse(utils.deepCompare(/abc/, /123/));
|
180 | const r = /abc/;
|
181 | assert.isTrue(utils.deepCompare(r, r));
|
182 | });
|
183 |
|
184 | it("should handle dates", function() {
|
185 | assert.isTrue(utils.deepCompare(new Date("2011-03-31"),
|
186 | new Date("2011-03-31")));
|
187 | assert.isFalse(utils.deepCompare(new Date("2011-03-31"),
|
188 | new Date("1970-01-01")));
|
189 | });
|
190 |
|
191 | it("should handle arrays", function() {
|
192 | assert.isTrue(utils.deepCompare([], []));
|
193 | assert.isTrue(utils.deepCompare([1, 2], [1, 2]));
|
194 | assert.isFalse(utils.deepCompare([1, 2], [2, 1]));
|
195 | assert.isFalse(utils.deepCompare([1, 2], [1, 2, 3]));
|
196 | });
|
197 |
|
198 | it("should handle simple objects", function() {
|
199 | assert.isTrue(utils.deepCompare({}, {}));
|
200 | assert.isTrue(utils.deepCompare({a: 1, b: 2}, {a: 1, b: 2}));
|
201 | assert.isTrue(utils.deepCompare({a: 1, b: 2}, {b: 2, a: 1}));
|
202 | assert.isFalse(utils.deepCompare({a: 1, b: 2}, {a: 1, b: 3}));
|
203 |
|
204 | assert.isTrue(utils.deepCompare({1: {name: "mhc", age: 28},
|
205 | 2: {name: "arb", age: 26}},
|
206 | {1: {name: "mhc", age: 28},
|
207 | 2: {name: "arb", age: 26}}));
|
208 |
|
209 | assert.isFalse(utils.deepCompare({1: {name: "mhc", age: 28},
|
210 | 2: {name: "arb", age: 26}},
|
211 | {1: {name: "mhc", age: 28},
|
212 | 2: {name: "arb", age: 27}}));
|
213 |
|
214 | assert.isFalse(utils.deepCompare({}, null));
|
215 | assert.isFalse(utils.deepCompare({}, undefined));
|
216 | });
|
217 |
|
218 | it("should handle functions", function() {
|
219 |
|
220 |
|
221 |
|
222 | const func = function(x) {
|
223 | return true;
|
224 | };
|
225 | const func2 = function(x) {
|
226 | return true;
|
227 | };
|
228 | assert.isTrue(utils.deepCompare(func, func));
|
229 | assert.isFalse(utils.deepCompare(func, func2));
|
230 | assert.isTrue(utils.deepCompare({ a: { b: func } }, { a: { b: func } }));
|
231 | assert.isFalse(utils.deepCompare({ a: { b: func } }, { a: { b: func2 } }));
|
232 | });
|
233 | });
|
234 |
|
235 |
|
236 | describe("extend", function() {
|
237 | const SOURCE = { "prop2": 1, "string2": "x", "newprop": "new" };
|
238 |
|
239 | it("should extend", function() {
|
240 | const target = {
|
241 | "prop1": 5, "prop2": 7, "string1": "baz", "string2": "foo",
|
242 | };
|
243 | const merged = {
|
244 | "prop1": 5, "prop2": 1, "string1": "baz", "string2": "x",
|
245 | "newprop": "new",
|
246 | };
|
247 | const sourceOrig = JSON.stringify(SOURCE);
|
248 |
|
249 | utils.extend(target, SOURCE);
|
250 | expect(JSON.stringify(target)).toEqual(JSON.stringify(merged));
|
251 |
|
252 |
|
253 | expect(JSON.stringify(SOURCE)).toEqual(sourceOrig);
|
254 | });
|
255 |
|
256 | it("should ignore null", function() {
|
257 | const target = {
|
258 | "prop1": 5, "prop2": 7, "string1": "baz", "string2": "foo",
|
259 | };
|
260 | const merged = {
|
261 | "prop1": 5, "prop2": 1, "string1": "baz", "string2": "x",
|
262 | "newprop": "new",
|
263 | };
|
264 | const sourceOrig = JSON.stringify(SOURCE);
|
265 |
|
266 | utils.extend(target, null, SOURCE);
|
267 | expect(JSON.stringify(target)).toEqual(JSON.stringify(merged));
|
268 |
|
269 |
|
270 | expect(JSON.stringify(SOURCE)).toEqual(sourceOrig);
|
271 | });
|
272 |
|
273 | it("should handle properties created with defineProperties", function() {
|
274 | const source = Object.defineProperties({}, {
|
275 | "enumerableProp": {
|
276 | get: function() {
|
277 | return true;
|
278 | },
|
279 | enumerable: true,
|
280 | },
|
281 | "nonenumerableProp": {
|
282 | get: function() {
|
283 | return true;
|
284 | },
|
285 | },
|
286 | });
|
287 |
|
288 | const target = {};
|
289 | utils.extend(target, source);
|
290 | expect(target.enumerableProp).toBe(true);
|
291 | expect(target.nonenumerableProp).toBe(undefined);
|
292 | });
|
293 | });
|
294 | });
|