-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.js
141 lines (122 loc) · 6.27 KB
/
tests.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
$(function() {
'use strict';
test('test environment', function() {
ok(window.FileReader !== undefined, 'window.FileReader is defined');
ok(new window.XMLHttpRequest().upload !== undefined, 'window.XMLHttpRequest has upload property');
});
test('xhrUploadQueue defined', function() {
var form = $('<form></form>');
ok(form.xhrUploadQueue, 'xhrUploadQueue method is defined');
deepEqual(typeof form.xhrUploadQueue, 'function', 'xhrUploadQueue is a function');
});
test('expose default settings', function() {
ok($.fn.xhrUploadQueue.defaults, 'defaults is defined');
// existance check for each property
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('maximumQueueSize'), 'maximumQueueSize exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('maximumFileSize'), 'maximumFileSize exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('uploadConcurrency'), 'uploadConcurrency exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('acceptedMimeTypes'), 'acceptedMimeTypes exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('postUrl'), 'postUrl exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('filesInputName'), 'filesInputName exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('hookDragAndDrop'), 'hookDragAndDrop exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('customFormData'), 'customFormData exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('silenceZeroByteErrors'), 'silenceZeroByteErrors exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('hoverClass'), 'hoverClass exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('fullSupportClass'), 'fullSupportClass exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('noSupportClass'), 'noSupportClass exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('noFilesAPIClass'), 'noFilesAPIClass exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('noXHR2Class'), 'noXHR2Class exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('init'), 'init exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('queueAdd'), 'queueAdd exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('queueChange'), 'queueChange exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('queueRemove'), 'queueRemove exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('handleUnacceptedFiles'), 'handleUnacceptedFiles exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('uploadStart'), 'uploadStart exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('uploadFinish'), 'uploadFinish exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('noSupport'), 'noSupport exists');
ok($.fn.xhrUploadQueue.defaults.hasOwnProperty('processFileList'), 'processFileList exists');
});
test('expose FileError enum', function() {
ok($.fn.xhrUploadQueue.FileError, 'FileError is defined');
// existance check for each property
ok($.fn.xhrUploadQueue.FileError.hasOwnProperty('QUEUE_FULL'), 'QUEUE_FULL exists');
ok($.fn.xhrUploadQueue.FileError.hasOwnProperty('FILE_TOO_LARGE'), 'FILE_TOO_LARGE exists');
ok($.fn.xhrUploadQueue.FileError.hasOwnProperty('UNACCEPTED_MIME_TYPE'), 'UNACCEPTED_MIME_TYPE exists');
ok($.fn.xhrUploadQueue.FileError.hasOwnProperty('ZERO_BYTE_FILE'), 'ZERO_BYTE_FILE exists');
});
test('return original jQuery object', function() {
var form = $('<form></form>'),
empty = $('#thisDoesntExist');
deepEqual(form.xhrUploadQueue(), form, 'original jQuery object returned for matched selector');
deepEqual(empty.xhrUploadQueue(), empty, 'original jQuery object returned for empty selector');
});
test('add support class', function() {
var form = $('<form></form>').xhrUploadQueue();
ok(form.hasClass($.fn.xhrUploadQueue.defaults.fullSupportClass), 'fullSupportClass added');
});
test('queue callbacks fire', function() {
expect(13);
var lastFile = null,
form = $('<form></form>').xhrUploadQueue({
acceptedMimeTypes: [ 'text/plain' ],
init: function() {
ok(this instanceof Object, 'init callback fired with context');
},
queueAdd: function(file) {
ok(this instanceof Object, 'queueAdd callback fired with context');
equal(file.name, 'test.txt', 'file name is correct');
lastFile = file;
},
queueChange: function() {
ok(this instanceof Object, 'queueChange callback fired with context');
},
queueRemove: function() {
ok(this instanceof Object, 'queueRemove callback fired with context');
},
handleUnacceptedFiles: function() {
ok(this instanceof Object, 'handleUnacceptedFiles callback fired with context');
},
uploadStart: function() {
},
uploadFinish: function() {
},
noSupport: function() {
},
processFileList: function(files) {
ok(this instanceof Object, 'processFileList callback fired with context');
return files;
}
});
form.trigger($.Event('drop', { originalEvent: { dataTransfer: { files: [ new MockFile(100, 'test.txt', 'text/plain') ] } } }));
form.trigger($.Event('drop', { originalEvent: { dataTransfer: { files: [ new MockFile(100, 'test.txt', 'text/plain') ] } } }));
lastFile.removeFromQueue();
form.trigger($.Event('drop', { originalEvent: { dataTransfer: { files: [ new MockFile(100, 'test.css', 'text/css') ] } } }));
});
test('hook drag and drop events', function() {
var form = $('<form></form>').xhrUploadQueue();
ok(!form.hasClass($.fn.xhrUploadQueue.defaults.hoverClass), 'form does not have hover class');
form.trigger('dragenter');
ok(form.hasClass($.fn.xhrUploadQueue.defaults.hoverClass), 'dragenter added hover class');
form.trigger('dragleave');
ok(!form.hasClass($.fn.xhrUploadQueue.defaults.hoverClass), 'dragleave removed hover class');
form.trigger('dragover');
ok(form.hasClass($.fn.xhrUploadQueue.defaults.hoverClass), 'dragover added hover class');
form.trigger($.Event('drop', { originalEvent: { dataTransfer: { files: [] } } }));
ok(!form.hasClass($.fn.xhrUploadQueue.defaults.hoverClass), 'drop removed hover class');
});
test('hook form submit event', function() {
var uploadStart = false,
form = $('<form></form>').xhrUploadQueue({
uploadStart: function() {
uploadStart = true;
}
});
form.trigger('submit');
ok(uploadStart, 'form submitted');
});
function MockFile(size, name, type) {
this.size = size;
this.name = name;
this.type = type;
}
});