util.test.js
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* eslint max-nested-callbacks: [2, 10]*/
/* eslint no-new: 0 */
/* globals describe, expect, it, sinon */
define(['screens/player/shared/util'], function(util) {
'use strict';
describe('screens/player/shared/util', function() {
describe('#makePath', function() {
it('returns empty string if input is empty', function() {
expect(util.makePath('')).equals('');
});
it('cuts of last character if it is a slash', function() {
expect(util.makePath('foo/')).equals('foo');
expect(util.makePath('foo')).equals('foo');
});
it('returns the concatenations', function() {
expect(util.makePath('/a', 'b', 'c')).equals('/a/b/c');
expect(util.makePath('/a', 'b/c')).equals('/a/b/c');
expect(util.makePath('/a', '/b', 'c')).equals('/a/b/c');
expect(util.makePath('/a', 'b/', 'c')).equals('/a/b/c');
});
it('works with arrays', function() {
expect(util.makePath(['/a', 'b', 'c'])).equals('/a/b/c');
expect(util.makePath(['/a', 'b/c'])).equals('/a/b/c');
expect(util.makePath(['/a', '/b', 'c'])).equals('/a/b/c');
expect(util.makePath(['/a', 'b/', 'c'])).equals('/a/b/c');
});
it('preserves leading slashy', function() {
expect(util.makePath('/a/', '/b/', '/c')).equals('/a/b/c');
expect(util.makePath(['/a/', '/b/', '/c'])).equals('/a/b/c');
});
});
describe('#makeId', function() {
it('removes the first character', function() {
expect(util.makeId('')).equals('');
expect(util.makeId('a')).equals('');
expect(util.makeId('/a')).equals('a');
});
it('replaces slashes with dots', function() {
expect(util.makeId('//')).equals('.');
expect(util.makeId('/a/b/')).equals('a.b.');
expect(util.makeId('/a/b/c')).equals('a.b.c');
expect(util.makeId('/a.b/c')).equals('a.b.c');
});
});
describe('#randomId', function() {
it('has a default length of 32', function() {
expect(util.randomId().length).equals(32);
});
it('has to respect the passed length', function() {
expect(util.randomId(10).length).equals(10);
});
it('has to respect the passed characters', function() {
var chars = ['a'];
var random = util.randomId(5, chars);
for (var i = 0; i < random.length; i++) {
expect(random[i]).to.eql('a');
}
});
});
describe('#trimTrailingSlash', function() {
it('removes the slashes', function() {
expect(util.trimTrailingSlash('/foo/')).equals('/foo');
expect(util.trimTrailingSlash('/foo/bar///')).equals('/foo/bar');
expect(util.trimTrailingSlash('/a')).equals('/a');
expect(util.trimTrailingSlash('/a/')).equals('/a');
});
it('keeps the root slash', function() {
expect(util.trimTrailingSlash('/')).equals('/');
});
it('handles empty string', function() {
expect(util.trimTrailingSlash('')).equals('');
});
});
describe('#get', function() {
var testString = 'Hello, world.';
var testObject = {
foo: {
bar: {
s: testString,
b: false,
zero: 0
}
}
};
it('gets the proper value', function() {
expect(util.get(testObject, 'foo.bar.s')).equals(testString);
expect(util.get(testObject, 'foo.bar.b', true)).equals(false);
expect(util.get(testObject, 'foo.bar.zero', 1)).equals(0);
});
it('gets the default', function() {
expect(util.get(testObject, 'foo.bar.not-existing', 'default')).equals('default');
expect(util.get(testObject, 'foo.not-existing', 'default')).equals('default');
expect(util.get(testObject, 'not-existing', 'default')).equals('default');
});
});
describe('#set', function() {
var testString = 'Hello, world.';
it('sets a deep value', function() {
var testObject = util.set({}, 'foo.bar.s', testString);
expect(util.get(testObject, 'foo.bar.s')).equals(testString);
});
});
describe('#formatUnit', function() {
it('returns the correct value', function() {
expect(util.formatUnit(100, util.UNITS_DI_METRIC, 0)).equals('100 bytes');
expect(util.formatUnit(1000, util.UNITS_DI_METRIC, 0)).equals('1 kB');
expect(util.formatUnit(1000, util.UNITS_DI_METRIC, 2)).equals('1.00 kB');
expect(util.formatUnit(1500000, util.UNITS_DI_METRIC, 2)).equals('1.50 MB');
expect(util.formatUnit(8192, util.UNITS_DI_IEC, 0)).equals('8 KiB');
expect(util.formatUnit(1200000, [1000, 'm', 'km'], 0)).equals('1200 km');
});
it('returns NaN for undefined values', function() {
expect(util.formatUnit()).equals('NaN');
});
});
describe('#getSchedule', function() {
it('returns null if missing required parameters', function() {
expect(util.getSchedule()).to.be.null;
expect(util.getSchedule(null, 'at 4:00 am')).to.be.null;
expect(util.getSchedule({}, null)).to.be.null;
});
it('returns null if there was an error parsing the expression', function() {
var later = { parse: {
text: sinon.stub(),
cron: sinon.stub()
}};
later.parse.text.returns({ error: 1 });
later.parse.cron.returns({ error: 1 });
expect(util.getSchedule(later, 'at 4:00 am')).to.be.null;
expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
expect(later.parse.cron).to.be.calledWithMatch('at 4:00 am');
});
it('returns the parsed text expression if it parses without error', function() {
var later = { parse: {
text: sinon.stub(),
cron: sinon.stub()
}};
var schedule = { error: -1 };
later.parse.text.returns(schedule);
later.parse.cron.returns({ error: 1 });
expect(util.getSchedule(later, 'at 4:00 am')).to.equal(schedule);
expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
expect(later.parse.cron).to.not.be.called;
});
it('returns the parsed cron expression if the text parsing failed', function() {
var later = { parse: {
text: sinon.stub(),
cron: sinon.stub()
}};
var schedule = { error: -1 };
later.parse.text.returns({ error: 1 });
later.parse.cron.returns(schedule);
expect(util.getSchedule(later, 'at 4:00 am')).to.equal(schedule);
expect(later.parse.text).to.be.calledWithMatch('at 4:00 am');
expect(later.parse.cron).to.be.calledWithMatch('at 4:00 am');
});
});
describe('#getNextScheduleOccurance', function() {
it('returns null if missing required parameters', function() {
expect(util.getNextScheduleOccurance()).to.be.null;
expect(util.getNextScheduleOccurance(null, {})).to.be.null;
expect(util.getNextScheduleOccurance({}, null)).to.be.null;
});
it('returns the next occurence for the schedule', function() {
var later = { schedule: sinon.stub() };
var schedule = {
next: sinon.stub(),
isValid: sinon.stub()
};
var next = [{}, {}];
later.schedule.returns(schedule);
schedule.next.returns(next[0]);
schedule.next.withArgs(2).returns(next);
schedule.isValid.returns(false);
expect(util.getNextScheduleOccurance(later, {})).to.equal(next[0]);
expect(later.schedule).to.be.called;
expect(schedule.isValid).to.be.called;
});
it('returns the second occurence for the schedule if the current one is already active', function() {
var later = { schedule: sinon.stub() };
var schedule = {
next: sinon.stub(),
isValid: sinon.stub()
};
var next = [1, 2];
later.schedule.returns(schedule);
schedule.next.returns(next[0]);
schedule.next.withArgs(2).returns(next);
schedule.isValid.returns(true);
expect(util.getNextScheduleOccurance(later, {})).to.equal(next[1]);
expect(later.schedule).to.be.called;
expect(schedule.isValid).to.be.called;
});
});
});
});