-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest-api.js
193 lines (163 loc) · 5.23 KB
/
test-api.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
/* jshint unused: true */
'use strict';
var chalk = require('chalk');
var gaLoaderSnippets = require('ga-loader-snippets');
var gaTrackerSnippet = require('ga-tracker-snippet');
var isogram = require('.');
var scriptEqual = require('script-equal');
var test = require('tape');
var scriptTagPattern = /^<script>((?:.|\n)*?)<\/script>$/;
function extractCode(snippet) {
var match = snippet.match(scriptTagPattern)[1];
return match;
}
test('isogram()', function(t) {
t.ok(
scriptEqual(isogram(), gaLoaderSnippets.with6params + gaTrackerSnippet()),
'should create a Google Analytics string with 6 parameters by default.'
);
t.equal(
isogram(null),
isogram(),
'should return the default value when it takes one argument and it is falsy.'
);
['AaB', 'AaBG', 'AaBGT', 'AaあGTå', 'AaアGTåN'].forEach(function(params) {
t.ok(
scriptEqual(
isogram(params),
gaLoaderSnippets['with' + params.length + 'params'] + gaTrackerSnippet()
),
'should create a Google Analytics string with ' + params.length + ' parameters.'
);
});
t.ok(
scriptEqual(
isogram({id: 'UA-36461297-9'}),
gaLoaderSnippets.with6params + gaTrackerSnippet({id: '36461297-9'})
),
'should change web property ID using `id` option.'
);
t.ok(
scriptEqual(
isogram({id: '36461297-3'}),
gaLoaderSnippets.with6params + gaTrackerSnippet({id: '36461297-3'})
),
'should add UA- prefix to web property ID automatically.'
);
t.ok(
scriptEqual(
isogram({domain: 'foo.example.com'}),
gaLoaderSnippets.with6params + gaTrackerSnippet({domain: 'foo.example.com'})
),
'should set domain using `domain` option.'
);
t.ok(
scriptEqual(
isogram({globalName: 'f'}),
gaLoaderSnippets.with6params.replace('ga', 'f') +
gaTrackerSnippet({globalName: 'f'})
),
'should change the global function name using `globalName` option.'
);
t.ok(
scriptEqual(
isogram('ANTqrつぁ', {minify: true}),
gaLoaderSnippets.with7params + gaTrackerSnippet()
),
'should minify the code without changing its structure, using `minify` option.'
);
t.notOk(
/\n|\s(?!Date)/.test(isogram('Vtカxc', {minify: true})),
'should remove unnecessary whitespaces and newlines, using `minify` option.'
);
t.notEqual(
isogram({color: true}).indexOf(chalk.green('G')),
-1,
'should add green color to the parameters using `color` option.'
);
t.equal(
chalk.stripColor(isogram({color: true, minify: true})),
isogram({color: false, minify: true}),
'should not change code structure, even if `color` option is enabled.'
);
t.equal(
isogram({singleQuotes: false}),
isogram(null).replace(/'/g, '\"'),
'should replace all the single quotes with double quotes, using `minify` option.'
);
t.ok(
scriptEqual(
isogram({track: false}),
gaLoaderSnippets.with6params
),
'should not include the tracker snippet if `track` option is false.'
);
t.ok(
scriptTagPattern.test(isogram({scriptTag: true})),
'should surround the code with script tags, using `scriptTag` option.'
);
t.ok(
scriptTagPattern.test(isogram({scriptTag: true, id: '36461297-3'})),
'should surround the code and tracker with script tags, using `scriptTag` option.'
);
t.ok(
scriptEqual(
extractCode(isogram({scriptTag: true})),
gaLoaderSnippets.with6params + gaTrackerSnippet()
),
'should have normal inner code when using `scriptTag`'
);
t.ok(
scriptEqual(
extractCode(isogram({scriptTag: true, id: '36461297-3'})),
gaLoaderSnippets.with6params + gaTrackerSnippet({id: '36461297-3'})
),
'should have normal inner code, even when using tracker.'
);
t.throws(
() => isogram(124578),
/TypeError.*First argument must be a string or an object\.$/,
'should fail when it takes one argument which is not a string or an object.'
);
t.throws(
() => isogram(['zxcvbn'], {}),
/TypeError.*First argument must be a string or an object\.$/,
'should fail when it takes two arguments but the first is not a string or an object.'
);
t.throws(
() => isogram('zxcv', 12),
/Second argument must be an object/,
'should fail when it takes a value as its second argument but it isn\'t an object.'
);
t.throws(
() => isogram('ab', null),
/Number of characters must be no fewer than 3 and no greater than 7\./,
'should not accept less than 3 characters.'
);
t.throws(
() => isogram('abcdefgh', undefined),
/Number of characters must be no fewer than 3 and no greater than 7\./,
'should not accept more than 7 characters.'
);
t.throws(
() => isogram('AaA'),
/"A" is duplicated\./,
'should fail when a character in the string is duplicated.'
);
t.throws(
() => isogram('AAqAqああ'),
/"A", "q" and "あ" are duplicated\./,
'should fail when characters in the string are duplicated.'
);
t.throws(
() => isogram('atW↑v'),
/"↑" cannot be used as a JavaScript parameter name\./,
'should not accept an invalid parameter name.'
);
t.throws(
() => isogram('123'),
/"1", "2" and "3" cannot be used.*s\./,
'should not accept invalid parameter names.'
);
t.end();
});