-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
helper.spec.js
348 lines (296 loc) · 11.5 KB
/
helper.spec.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
'use strict'
const path = require('path')
describe('helper', () => {
const helper = require('../../lib/helper')
describe('browserFullNameToShort', () => {
// helper function
const expecting = (name) => expect(helper.browserFullNameToShort(name))
it('should parse iOS', () => {
expecting(
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 ' +
'(KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25'
)
.to.be.equal('Mobile Safari 6.0.0 (iOS 6.0.0)')
})
it('should parse Linux', () => {
expecting(
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.19) Gecko/20081216 ' +
'Ubuntu/8.04 (hardy) Firefox/2.0.0.19'
)
.to.be.equal('Firefox 2.0.0 (Ubuntu 8.04.0)')
})
it('should degrade gracefully when OS not recognized', () => {
expecting(
'Mozilla/5.0 (X11; U; FreeBSD; i386; en-US; rv:1.7) Gecko/20081216 ' +
'Firefox/2.0.0.19'
).to.be.equal('Firefox 2.0.0 (FreeBSD 0.0.0)')
})
it('should parse Chrome', () => {
expecting(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 ' +
'(KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7'
)
.to.be.equal('Chrome 16.0.912 (Mac OS X 10.6.8)')
expecting(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.15 ' +
'(KHTML, like Gecko) Chrome/18.0.985.0 Safari/535.15'
)
.to.be.equal('Chrome 18.0.985 (Mac OS X 10.6.8)')
})
it('should parse Firefox', () => {
expecting(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 ' +
'Firefox/7.0.1'
)
.to.be.equal('Firefox 7.0.1 (Mac OS X 10.6.0)')
})
it('should parse Opera', () => {
expecting(
'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 ' +
'Version/11.52'
)
.to.be.equal('Opera 11.52.0 (Mac OS X 10.6.8)')
})
it('should parse Safari', () => {
expecting(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.52.7 ' +
'(KHTML, like Gecko) Version/5.1.2 Safari/534.52.7'
)
.to.be.equal('Safari 5.1.2 (Mac OS X 10.6.8)')
})
it('should parse IE7', () => {
expecting(
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; ' +
'.NET CLR 2.0.50727; .NET4.0C; .NET4.0E)'
)
.to.be.equal('IE 7.0.0 (Windows Vista.0.0)')
})
it('should parse IE8', () => {
expecting(
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; ' +
'SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)"'
)
.to.be.equal('IE 8.0.0 (Windows 7.0.0)')
})
it('should parse IE9', () => {
expecting(
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; ' +
'.NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)'
)
.to.be.equal('IE 9.0.0 (Windows 7.0.0)')
})
it('should parse IE10', () => {
expecting(
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; ' +
'.NET4.0E; .NET4.0C)'
)
.to.be.equal('IE 10.0.0 (Windows 8.0.0)')
})
it('should parse PhantomJS', () => {
expecting(
'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) ' +
'PhantomJS/1.6.0 Safari/534.34'
)
.to.be.equal('PhantomJS 1.6.0 (Mac OS X 0.0.0)')
})
// Fix for #318
it('should parse old Android Browser', () => {
expecting(
'Mozilla/5.0 (Linux; U; Android 4.2; en-us; sdk Build/JB_MR1) ' +
'AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'
)
.to.be.equal('Android 4.2.0 (Android 4.2.0)')
})
it('should parse Headless Chrome', () => {
expecting(
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'HeadlessChrome/70.0.3538.77 Safari/537.36'
)
.to.be.equal('HeadlessChrome 70.0.3538 (Linux 0.0.0)')
})
})
describe('isDefined', () => {
const isDefined = helper.isDefined
it('should return true if defined', () => {
expect(isDefined()).to.equal(false)
expect(isDefined(undefined)).to.equal(false)
expect(isDefined(false)).to.equal(true)
expect(isDefined(0)).to.equal(true)
expect(isDefined(null)).to.equal(true)
expect(isDefined('')).to.equal(true)
})
})
describe('camelToSnake', () => {
const camelToSnake = helper.camelToSnake
it('should convert camelCase string to snake_case', () => {
expect(camelToSnake('OneMoreThing')).to.equal('one_more_thing')
})
})
describe('dashToCamel', () => {
const dashToCamel = helper.dashToCamel
it('should convert dash-case to camelCase', () => {
expect(dashToCamel('one-more-thing')).to.equal('oneMoreThing')
expect(dashToCamel('one')).to.equal('one')
})
})
describe('arrayRemove', () => {
const arrayRemove = helper.arrayRemove
it('should remove object from array', () => {
const a = 'one'
const b = []
const c = {}
const d = () => null
const collection = [a, b, c, d]
expect(arrayRemove(collection, b)).to.equal(true)
expect(collection).to.deep.equal([a, c, d])
expect(arrayRemove(collection, {})).to.equal(false)
expect(collection).to.deep.equal([a, c, d])
expect(arrayRemove(collection, d)).to.equal(true)
expect(collection).to.deep.equal([a, c])
expect(arrayRemove(collection, a)).to.equal(true)
expect(collection).to.deep.equal([c])
})
})
describe('merge', () => {
it('should copy properties to first argument', () => {
const destination = {a: 1, b: 2}
const result = helper.merge(destination, {a: 4, c: 5})
expect(destination.a).to.equal(1)
expect(result).to.deep.equal({a: 4, b: 2, c: 5})
})
})
describe('isUrlAbsolute', () => {
it('should check http/https protocol', () => {
expect(helper.isUrlAbsolute('some/path/http.html')).to.equal(false)
expect(helper.isUrlAbsolute('/some/more.py')).to.equal(false)
expect(helper.isUrlAbsolute('http://some.com/path')).to.equal(true)
expect(helper.isUrlAbsolute('https://more.org/some.js')).to.equal(true)
})
})
describe('formatTimeInterval', () => {
it('should format into seconds', () => {
expect(helper.formatTimeInterval(23000)).to.equal('23 secs')
})
it('should format into minutes', () => {
expect(helper.formatTimeInterval(142000)).to.equal('2 mins 22 secs')
})
it('should handle singular minute or second', () => {
expect(helper.formatTimeInterval(61000)).to.equal('1 min 1 sec')
})
it('should round to miliseconds', () => {
expect(helper.formatTimeInterval(163017)).to.equal('2 mins 43.017 secs')
})
})
describe('mkdirIfNotExists', () => {
const fsMock = require('mocks').fs
const loadFile = require('mocks').loadFile
const fs = fsMock.create({
home: {'some.js': fsMock.file()}
})
// load file under test
const m = loadFile(path.join(__dirname, '/../../lib/helper.js'), {'graceful-fs': fs, lodash: require('lodash')})
const mkdirIfNotExists = m.exports.mkdirIfNotExists
it('should not do anything, if dir already exists', (done) => {
mkdirIfNotExists('/home', done)
})
it('should create directory if it does not exist', (done) => {
mkdirIfNotExists('/home/new', () => {
const stat = fs.statSync('/home/new')
expect(stat).to.exist
expect(stat.isDirectory()).to.equal(true)
done()
})
})
it('should create even parent directories if it does not exist', (done) => {
mkdirIfNotExists('/home/new/parent/child', () => {
const stat = fs.statSync('/home/new/parent/child')
expect(stat).to.exist
expect(stat.isDirectory()).to.equal(true)
done()
})
})
})
describe('mmComparePatternWeights', () => {
const helper = require('../../lib/helper')
it('should compare right on equal', () => {
helper.mmComparePatternWeights([1, 2, 3], [1, 2, 3]).should.be.equal(0)
})
it('should compare right on less', () => {
helper.mmComparePatternWeights([1, 2, 3], [1, 2, 5]).should.be.equal(-1)
})
it('should compare right on greater than', () => {
helper.mmComparePatternWeights([1, 3, 3], [1, 2, 5]).should.be.equal(1)
})
it('should compare right on larger size', () => {
helper.mmComparePatternWeights([1, 2, 3, 4], [1, 2, 3, 4]).should.be.equal(0)
})
})
describe('mmPatternWeight', () => {
const helper = require('../../lib/helper')
it('should calculate right weight of empty', () => {
helper.mmPatternWeight('').should.be.deep.equal([0, 0, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with no magic', () => {
helper.mmPatternWeight('foo').should.be.deep.equal([1, 0, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with optional', () => {
helper.mmPatternWeight('foo?').should.be.deep.equal([1, 0, 0, 0, 0, 1])
})
it('should calculate right weight of pattern with range', () => {
helper.mmPatternWeight('[fo]').should.be.deep.equal([1, 0, 0, 0, 1, 0])
})
it('should calculate right weight of pattern with glob sets', () => {
helper.mmPatternWeight('{0..9}').should.be.deep.equal([10, 0, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with two glob sets', () => {
helper.mmPatternWeight('{a,b}').should.be.deep.equal([2, 0, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with ext glob', () => {
helper.mmPatternWeight('+(a|b)').should.be.deep.equal([1, 0, 0, 1, 0, 0])
})
it('should calculate right weight of pattern with ext glob misuse', () => {
helper.mmPatternWeight('(a|b)').should.be.deep.equal([1, 0, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with star', () => {
helper.mmPatternWeight('*').should.be.deep.equal([1, 0, 1, 0, 0, 0])
})
it('should calculate right weight of pattern with glob star', () => {
helper.mmPatternWeight('**/a').should.be.deep.equal([1, 1, 0, 0, 0, 0])
})
it('should calculate right weight of pattern with misused glob star', () => {
helper.mmPatternWeight('***/a').should.be.deep.equal([1, 0, 3, 0, 0, 0])
})
it('should calculate right weight of pattern with more magic', () => {
helper.mmPatternWeight('{0..9}/?(a|b)c/[abc]/**/*.jsx?').should.be.deep.equal([10, 1, 1, 1, 1, 1])
})
it('should calculate right weight of pattern as worst glob set', () => {
helper.mmPatternWeight('{**,*}').should.be.deep.equal([2, 1, 0, 0, 0, 0])
})
})
describe('saveOriginalArgs', () => {
it('should clone config.client.args', () => {
var config = {
client: {
args: ['--somearg']
}
}
expect(config.client.originalArgs).to.not.exist
helper.saveOriginalArgs(config)
config.client.args.should.be.deep.equal(['--somearg'])
config.client.originalArgs.should.be.deep.equal(['--somearg'])
})
})
describe('restoreOriginalArgs', () => {
it('should restore config.client.originalArgs', () => {
var config = {
client: {
args: ['--somearg'],
originalArgs: []
}
}
helper.restoreOriginalArgs(config)
config.client.args.should.be.deep.equal([])
config.client.originalArgs.should.be.deep.equal([])
})
})
})