-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
utils.test.js
151 lines (132 loc) · 5.14 KB
/
utils.test.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
/*jshint mocha:true*/
/*global assert:false, console:true*/
'use strict';
var utils = require('../src/utils');
var Raven = require('../src/raven');
var RavenConfigError = require('../src/configError');
var isUndefined = utils.isUndefined;
var isFunction = utils.isFunction;
var isString = utils.isString;
var isObject = utils.isObject;
var isEmptyObject = utils.isEmptyObject;
var isError = utils.isError;
var joinRegExp = utils.joinRegExp;
var objectMerge = utils.objectMerge;
var truncate = utils.truncate;
var urlencode = utils.urlencode;
var htmlElementAsString = utils.htmlElementAsString;
describe('utils', function () {
describe('isUndefined', function() {
it('should do as advertised', function() {
assert.isTrue(isUndefined());
assert.isFalse(isUndefined({}));
assert.isFalse(isUndefined(''));
assert.isTrue(isUndefined(undefined));
});
});
describe('isFunction', function() {
it('should do as advertised', function() {
assert.isTrue(isFunction(function(){}));
assert.isFalse(isFunction({}));
assert.isFalse(isFunction(''));
assert.isFalse(isFunction(undefined));
});
});
describe('isString', function() {
it('should do as advertised', function() {
assert.isTrue(isString(''));
assert.isTrue(isString(String('')));
assert.isTrue(isString(new String('')));
assert.isFalse(isString({}));
assert.isFalse(isString(undefined));
assert.isFalse(isString(function(){}));
});
});
describe('isObject', function() {
it('should do as advertised', function() {
assert.isTrue(isObject({}));
assert.isTrue(isObject(new Error()))
assert.isFalse(isObject(''));
});
});
describe('isEmptyObject', function() {
it('should work as advertised', function() {
assert.isTrue(isEmptyObject({}));
assert.isFalse(isEmptyObject({foo: 1}));
});
});
describe('isError', function() {
it('should work as advertised', function() {
assert.isTrue(isError(new Error()));
assert.isTrue(isError(new ReferenceError()));
assert.isTrue(isError(new RavenConfigError()));
assert.isFalse(isError({}));
assert.isFalse(isError(''));
assert.isFalse(isError(true));
});
});
describe('objectMerge', function() {
it('should work as advertised', function() {
assert.deepEqual(objectMerge({}, {}), {});
assert.deepEqual(objectMerge({a:1}, {b:2}), {a:1, b:2});
assert.deepEqual(objectMerge({a:1}), {a:1});
});
});
describe('truncate', function() {
it('should work as advertised', function() {
assert.equal(truncate('lolol', 3), 'lol\u2026');
assert.equal(truncate('lolol', 10), 'lolol');
assert.equal(truncate('lol', 3), 'lol');
assert.equal(truncate(new Array(1000).join('f'), 0), new Array(1000).join('f'));
});
});
describe('joinRegExp', function() {
it('should work as advertised', function() {
assert.equal(joinRegExp([
'a', 'b', 'a.b', /d/, /[0-9]/
]).source, 'a|b|a\\.b|d|[0-9]');
});
it('should not process empty or undefined variables', function() {
assert.equal(joinRegExp([
'a', 'b', null, undefined
]).source, 'a|b');
});
it('should skip entries that are not strings or regular expressions in the passed array of patterns', function() {
assert.equal(joinRegExp([
'a', 'b', null, 'a.b', undefined, true, /d/, 123, {}, /[0-9]/, []
]).source, 'a|b|a\\.b|d|[0-9]');
});
});
describe('urlencode', function() {
it('should work', function() {
assert.equal(urlencode({}), '');
assert.equal(urlencode({'foo': 'bar', 'baz': '1 2'}), 'foo=bar&baz=1%202');
});
});
describe('htmlElementAsString', function () {
it('should work', function () {
assert.equal(htmlElementAsString({
tagName: 'INPUT',
getAttribute: function (key){
return {
id: 'the-username',
name: 'username',
class: 'form-control',
placeholder: 'Enter your username'
}[key];
}
}), '<input id="the-username" name="username" class="form-control" placeholder="Enter your username" />');
assert.equal(htmlElementAsString({
tagName: 'IMG',
getAttribute: function (key){
return {
id: 'image-3',
title: 'A picture of an apple',
'data-something': 'This should be ignored' // skipping data-* attributes in first implementation
}[key];
}
}), '<img id="image-3" title="A picture of an apple" />');
});
it
});
});