-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang_spec.js
135 lines (119 loc) · 3.73 KB
/
lang_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
var _ = require('../../../../src/util')
describe('Util - Language Enhancement', function () {
it('toString', function () {
expect(_.toString('hi')).toBe('hi')
expect(_.toString(1.234)).toBe('1.234')
expect(_.toString(null)).toBe('')
expect(_.toString(undefined)).toBe('')
})
it('toNumber', function () {
expect(_.toNumber('12')).toBe(12)
expect(_.toNumber('1e5')).toBe(1e5)
expect(_.toNumber('0x2F')).toBe(0x2F)
expect(_.toNumber(null)).toBe(null)
expect(_.toNumber(true)).toBe(true)
expect(_.toNumber('hello')).toBe('hello')
})
it('strip quotes', function () {
expect(_.stripQuotes('"123"')).toBe('123')
expect(_.stripQuotes("'fff'")).toBe('fff')
expect(_.stripQuotes("'fff")).toBe(false)
})
it('camelize', function () {
expect(_.camelize('abc')).toBe('abc')
expect(_.camelize('some-long-name')).toBe('someLongName')
})
it('classify', function () {
expect(_.classify('abc')).toBe('Abc')
expect(_.classify('some-long-name')).toBe('SomeLongName')
expect(_.classify('what_about_this')).toBe('WhatAboutThis')
expect(_.classify('how/about/that')).toBe('HowAboutThat')
})
it('bind', function () {
var original = function (a) {
return this.a + a
}
var ctx = { a: 'ctx a ' }
var bound = _.bind(original, ctx)
var res = bound('arg a')
expect(res).toBe('ctx a arg a')
})
it('toArray', function () {
// should make a copy of original array
var arr = [1,2,3]
var res = _.toArray(arr)
expect(Array.isArray(res)).toBe(true)
expect(res.toString()).toEqual('1,2,3')
expect(res).not.toBe(arr)
// should work on arguments
;(function () {
var res = _.toArray(arguments)
expect(Array.isArray(res)).toBe(true)
expect(res.toString()).toEqual('1,2,3')
})(1,2,3)
})
it('extend', function () {
var from = {a:1,b:2}
var to = {}
var res = _.extend(to, from)
expect(to.a).toBe(from.a)
expect(to.b).toBe(from.b)
expect(res).toBe(to)
})
it('isObject', function () {
expect(_.isObject({})).toBe(true)
expect(_.isObject([])).toBe(true)
expect(_.isObject(null)).toBeFalsy()
expect(_.isObject(123)).toBeFalsy()
expect(_.isObject(true)).toBeFalsy()
expect(_.isObject('hi')).toBeFalsy()
expect(_.isObject(undefined)).toBeFalsy()
expect(_.isObject(function(){})).toBeFalsy()
})
it('isPlainObject', function () {
expect(_.isPlainObject({})).toBe(true)
expect(_.isPlainObject([])).toBe(false)
expect(_.isPlainObject(null)).toBe(false)
expect(_.isPlainObject(null)).toBeFalsy()
expect(_.isPlainObject(123)).toBeFalsy()
expect(_.isPlainObject(true)).toBeFalsy()
expect(_.isPlainObject('hi')).toBeFalsy()
expect(_.isPlainObject(undefined)).toBeFalsy()
expect(_.isPlainObject(function(){})).toBe(false)
if (_.inBrowser) {
expect(_.isPlainObject(window)).toBe(false)
}
})
it('isArray', function () {
expect(_.isArray([])).toBe(true)
expect(_.isArray({})).toBe(false)
expect(_.isArray(arguments)).toBe(false)
})
it('define', function () {
var obj = {}
_.define(obj, 'test', 123)
expect(obj.test).toBe(123)
var desc = Object.getOwnPropertyDescriptor(obj, 'test')
expect(desc.enumerable).toBe(false)
_.define(obj, 'test2', 123, true)
expect(obj.test2).toBe(123)
desc = Object.getOwnPropertyDescriptor(obj, 'test2')
expect(desc.enumerable).toBe(true)
})
it('debounce', function (done) {
var count = 0
var fn = _.debounce(function () {
count++
}, 100)
fn()
setTimeout(fn, 10)
setTimeout(fn, 20)
setTimeout(function () {
expect(count).toBe(0)
}, 30)
setTimeout(function () {
expect(count).toBe(1)
done()
}, 200)
})
})