-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·282 lines (206 loc) · 8.85 KB
/
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
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
'use strict';
require('mocha');
var assert = require('assert');
var fs = require('fs');
var jsdom = require('jsdom');
var jQuery = fs.readFileSync('./node_modules/jquery/dist/jquery.js', 'utf-8');
var jQueryApp = fs.readFileSync('./dist/jquery-app.js', 'utf-8');
function createTestEnv(fn) {
return function (done) {
var dom = new jsdom.JSDOM('<html><body></body></html>', {'runScripts': 'dangerously'});
var window = dom.window;
var script = window.document.createElement('script');
script.textContent = jQuery;
window.document.body.appendChild(script);
var script = window.document.createElement('script');
script.textContent = jQueryApp;
window.document.body.appendChild(script);
fn(done, window.$);
window.close(); // clean up memory
};
}
describe('jquery-app', function(done) {
it('$.fn.app plugin exists', createTestEnv(function (done, $) {
assert.equal(typeof $.fn.app, 'function');
done();
}));
it('should call multiple plugins separated by comma', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$('body').append('<div data-plugin="foo, bar"></div>').app();
assert.equal(foo, 1);
assert.equal(bar, 1);
done();
}));
it('should call multiple plugins separated by whitespace', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$('body').append('<div data-plugin="foo bar"></div>').app();
assert.equal(foo, 1);
assert.equal(bar, 1);
done();
}));
it('should call multiple plugins on same element', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$('body').append('<div data-plugin="foo"><span data-plugin="bar"></span></div>').app();
assert.equal(foo, 1);
assert.equal(bar, 1);
done();
}));
it('should call plugin only once', createTestEnv(function (done, $) {
var foo = 0;
$.fn.foo = function () { foo++; };
$('body').append('<div data-plugin="foo"><span data-plugin="bar"></span></div>').app().app();
assert.equal(foo, 1);
done();
}));
it('should have options object', createTestEnv(function (done, $) {
$.fn.test = function (options) {
assert.equal(typeof options, 'object');
assert.equal(Object.keys(options).length, 0);
done();
};
$('body').append('<div data-plugin="test"></div>').app();
}));
it('should pass options from data-PLUGIN attribute', createTestEnv(function (done, $) {
$.fn.test = function (options) {
assert.equal(options.name, 'John');
assert.equal(options.age, 21);
done();
};
$('body').append('<div data-plugin="test" data-test=\'{"name": "John", "age": 21}\'></div>').app();
}));
it('should pass options from data-PLUGIN-PROPERTY attributes', createTestEnv(function (done, $) {
$.fn.test = function (options) {
assert.equal(options.name, 'John');
assert.equal(options.age, 21);
done();
};
$('body').append('<div data-plugin="test" data-test-name="John" data-test-age="21"></div>').app();
}));
it('should pass options to all plugins', createTestEnv(function (done, $) {
var foo = false;
var bar = false;
$.fn.foo = function (options) {
assert.equal(options.name, 'John');
foo = true;
};
$.fn.bar = function (options) {
assert.equal(options.name, 'Jane');
bar = true;
};
$('body').append('<div data-plugin="foo, bar" data-foo-name="John" data-bar=\'{"name": "Jane"}\'></div>').app();
assert.equal(foo, true);
assert.equal(bar, true);
done();
}));
it('should call plugin with only specific namespace', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$('body').append('<div data-plugin="foo"><span data-other-plugin="bar"></span></div>').app({
'namespace': 'other-plugin'
});
assert.equal(foo, 0);
assert.equal(bar, 1);
done();
}));
it('should call plugins with only specific namespaces after dynamic DOM insert', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$('body').append('<div data-plugin="foo"><span data-other-plugin="bar"></span></div>');
$('body').app(); // data-plugin
$('body').app({'namespace': 'other-plugin'}); // data-other-plugin
$('body').append('<div data-plugin="foo"></div>');
$('body').app(); // data-plugin
assert.equal(foo, 2);
assert.equal(bar, 1);
done();
}));
it('$.app.hasPluginDefined returns true if plugin is defined', createTestEnv(function (done, $) {
$('body').append('<div data-plugin="foo"><span data-plugin="bar"></span></div>');
var foo = $('body').find('div[data-plugin]');
var bar = $('body').find('span[data-plugin]');
$.fn.foo = function () {};
$.fn.bar = function () {};
assert.equal($.app.hasPluginDefined(foo, 'foo'), true);
assert.equal($.app.hasPluginDefined(bar, 'foo'), false);
assert.equal($.app.hasPluginDefined(foo, 'bar'), false);
assert.equal($.app.hasPluginDefined(bar, 'bar'), true);
done();
}));
it('$.app.hasPluginDefined returns false if plugin doesn\'t exist', createTestEnv(function (done, $) {
$('body').append('<div data-plugin="foo"><span data-plugin="bar"></span></div>');
var foo = $('body').find('div[data-plugin]');
var bar = $('body').find('span[data-plugin]');
assert.equal($.app.hasPluginDefined(foo, 'foo'), false);
assert.equal($.app.hasPluginDefined(bar, 'bar'), false);
done();
}));
it('$.app.hasPlugin returns true if plugin is initialized', createTestEnv(function (done, $) {
$.fn.foo = function () {};
$.fn.bar = function () {};
$('body').append('<div data-plugin="foo baz"><span data-plugin="bar"></span></div>').app();
var foo = $('body').find('div[data-plugin]');
var bar = $('body').find('span[data-plugin]');
assert.equal($.app.hasPlugin(foo, 'baz'), false);
assert.equal($.app.hasPlugin(foo, 'foo'), true);
assert.equal($.app.hasPlugin(bar, 'foo'), false);
assert.equal($.app.hasPlugin(foo, 'bar'), false);
assert.equal($.app.hasPlugin(bar, 'bar'), true);
done();
}));
it('$.app.hasPlugin returns false if plugin doesn\'t exist', createTestEnv(function (done, $) {
$('body').append('<div data-plugin="foo baz"></div>').app();
var baz = $('body').find('div[data-plugin]');
assert.equal($.app.hasPlugin(baz, 'baz'), false);
done();
}));
it('$.app.getPlugins returns list of plugin names', createTestEnv(function (done, $) {
$.fn.foo = function () {};
$.fn.bar = function () {};
$.fn.baz = function () {};
$.fn.biz = function () {};
$.fn.bus = function () {};
$('body').append('<div data-plugin="foo, bar baz,biz, bus"></div>');
var element = $('body').find('div[data-plugin]');
assert.equal($.app.getPlugins(element).join(','), ['foo', 'bar', 'baz', 'biz', 'bus'].join(','));
done();
}));
it('$.app.getPlugins doesn\'t returns non-existing plugin names', createTestEnv(function (done, $) {
$.fn.foo = function () {};
$.fn.biz = function () {};
$.fn.bus = function () {};
$('body').append('<div data-plugin="foo, bar baz,biz, bus"></div>');
var element = $('body').find('div[data-plugin]');
assert.equal($.app.getPlugins(element).join(','), ['foo', 'biz', 'bus'].join(','));
done();
}));
it('Call only specific plugins', createTestEnv(function (done, $) {
var foo = 0;
var bar = 0;
var baz = 0;
$.fn.foo = function () { foo++; };
$.fn.bar = function () { bar++; };
$.fn.baz = function () { baz++; };
$('body').append('<div data-plugin="foo bar baz"></div>');
$('body').app({}, ['foo', 'baz']);
$('body').empty();
$('body').append('<div data-plugin="foo bar baz"></div>');
$('body').app(['foo', 'baz']);
assert.strictEqual(foo, 2);
assert.strictEqual(bar, 0);
assert.strictEqual(baz, 2);
done();
}));
});