2017-02-24 16:37:32 -05:00
|
|
|
module DropzoneHelper
|
|
|
|
# Provides a way to perform `attach_file` for a Dropzone-based file input
|
|
|
|
#
|
|
|
|
# This is accomplished by creating a standard HTML file input on the page,
|
|
|
|
# performing `attach_file` on that field, and then triggering the appropriate
|
|
|
|
# Dropzone events to perform the actual upload.
|
|
|
|
#
|
|
|
|
# This method waits for the upload to complete before returning.
|
2017-05-16 04:52:17 -04:00
|
|
|
# max_file_size is an optional parameter.
|
|
|
|
# If it's not 0, then it used in dropzone.maxFilesize parameter.
|
|
|
|
# wait_for_queuecomplete is an optional parameter.
|
|
|
|
# If it's 'false', then the helper will NOT wait for backend response
|
|
|
|
# It lets to test behaviors while AJAX is processing.
|
|
|
|
def dropzone_file(files, max_file_size = 0, wait_for_queuecomplete = true)
|
2017-02-24 16:37:32 -05:00
|
|
|
# Generate a fake file input that Capybara can attach to
|
|
|
|
page.execute_script <<-JS.strip_heredoc
|
2017-05-16 04:52:17 -04:00
|
|
|
$('#fakeFileInput').remove();
|
2017-02-24 16:37:32 -05:00
|
|
|
var fakeFileInput = window.$('<input/>').attr(
|
2017-05-16 04:52:17 -04:00
|
|
|
{id: 'fakeFileInput', type: 'file', multiple: true}
|
2017-02-24 16:37:32 -05:00
|
|
|
).appendTo('body');
|
|
|
|
|
|
|
|
window._dropzoneComplete = false;
|
|
|
|
JS
|
|
|
|
|
2017-05-16 04:52:17 -04:00
|
|
|
# Attach files to the fake input selector with Capybara
|
|
|
|
attach_file('fakeFileInput', files)
|
2017-02-24 16:37:32 -05:00
|
|
|
|
|
|
|
# Manually trigger a Dropzone "drop" event with the fake input's file list
|
|
|
|
page.execute_script <<-JS.strip_heredoc
|
|
|
|
var dropzone = $('.div-dropzone')[0].dropzone;
|
2017-05-16 04:52:17 -04:00
|
|
|
dropzone.options.autoProcessQueue = false;
|
|
|
|
|
|
|
|
if (#{max_file_size} > 0) {
|
|
|
|
dropzone.options.maxFilesize = #{max_file_size};
|
|
|
|
}
|
|
|
|
|
2017-02-24 16:37:32 -05:00
|
|
|
dropzone.on('queuecomplete', function() {
|
|
|
|
window._dropzoneComplete = true;
|
|
|
|
});
|
2017-05-16 04:52:17 -04:00
|
|
|
|
|
|
|
var fileList = [$('#fakeFileInput')[0].files];
|
|
|
|
|
|
|
|
$.map(fileList, function(file){
|
|
|
|
var e = jQuery.Event('drop', { dataTransfer : { files : file } });
|
|
|
|
|
|
|
|
dropzone.listeners[0].events.drop(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
dropzone.processQueue();
|
2017-02-24 16:37:32 -05:00
|
|
|
JS
|
|
|
|
|
2017-05-16 04:52:17 -04:00
|
|
|
if wait_for_queuecomplete
|
|
|
|
# Wait until Dropzone's fired `queuecomplete`
|
|
|
|
loop until page.evaluate_script('window._dropzoneComplete === true')
|
|
|
|
end
|
2017-02-24 16:37:32 -05:00
|
|
|
end
|
|
|
|
end
|