Add feature specs for three types of user uploads

This commit is contained in:
Robert Speicher 2017-02-24 16:37:32 -05:00
parent a8c62dfe5c
commit 2858bc2037
5 changed files with 112 additions and 29 deletions

View file

@ -1,6 +1,7 @@
require 'spec_helper'
describe 'Issues', feature: true do
include DropzoneHelper
include IssueHelpers
include SortingHelper
include WaitForAjax
@ -570,19 +571,13 @@ describe 'Issues', feature: true do
end
it 'uploads file when dragging into textarea' do
drop_in_dropzone test_image_file
# Wait for the file to upload
sleep 1
dropzone_file Rails.root.join('spec', 'fixtures', 'banana_sample.gif')
expect(page.find_field("issue_description").value).to have_content 'banana_sample'
end
it 'adds double newline to end of attachment markdown' do
drop_in_dropzone test_image_file
# Wait for the file to upload
sleep 1
dropzone_file Rails.root.join('spec', 'fixtures', 'banana_sample.gif')
expect(page.find_field("issue_description").value).to match /\n\n$/
end
@ -665,25 +660,4 @@ describe 'Issues', feature: true do
end
end
end
def drop_in_dropzone(file_path)
# Generate a fake input selector
page.execute_script <<-JS
var fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type: 'file'}
).appendTo('body');
JS
# Attach the file to the fake input selector with Capybara
attach_file("fakeFileInput", file_path)
# Add the file to a fileList array and trigger the fake drop event
page.execute_script <<-JS
var fileList = [$('#fakeFileInput')[0].files[0]];
var e = jQuery.Event('drop', { dataTransfer : { files : fileList } });
$('.div-dropzone')[0].dropzone.listeners[0].events.drop(e);
JS
end
def test_image_file
File.join(Rails.root, 'spec', 'fixtures', 'banana_sample.gif')
end
end

View file

@ -0,0 +1,26 @@
require 'rails_helper'
feature 'User uploads avatar to group', feature: true do
scenario 'they see the new avatar' do
user = create(:user)
group = create(:group)
group.add_owner(user)
login_as(user)
visit edit_group_path(group)
attach_file(
'group_avatar',
Rails.root.join('spec', 'fixtures', 'dk.png'),
visible: false
)
click_button 'Save group'
visit group_path(group)
expect(page).to have_selector(%Q(img[src$="/uploads/group/avatar/#{group.id}/dk.png"]))
# Cheating here to verify something that isn't user-facing, but is important
expect(group.reload.avatar.file).to exist
end
end

View file

@ -0,0 +1,24 @@
require 'rails_helper'
feature 'User uploads avatar to profile', feature: true do
scenario 'they see their new avatar' do
user = create(:user)
login_as(user)
visit profile_path
attach_file(
'user_avatar',
Rails.root.join('spec', 'fixtures', 'dk.png'),
visible: false
)
click_button 'Update profile settings'
visit user_path(user)
expect(page).to have_selector(%Q(img[src$="/uploads/user/avatar/#{user.id}/dk.png"]))
# Cheating here to verify something that isn't user-facing, but is important
expect(user.reload.avatar.file).to exist
end
end

View file

@ -0,0 +1,22 @@
require 'rails_helper'
feature 'User uploads file to note', feature: true do
include DropzoneHelper
let(:user) { create(:user) }
let(:project) { create(:empty_project, creator: user, namespace: user.namespace) }
scenario 'they see the attached file', js: true do
issue = create(:issue, project: project, author: user)
login_as(user)
visit namespace_project_issue_path(project.namespace, project, issue)
dropzone_file(Rails.root.join('spec', 'fixtures', 'dk.png'))
click_button 'Comment'
wait_for_ajax
expect(find('a.no-attachment-icon img[alt="dk"]')['src'])
.to match(%r{/#{project.full_path}/uploads/\h{32}/dk\.png$})
end
end

View file

@ -0,0 +1,37 @@
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.
def dropzone_file(file_path)
# Generate a fake file input that Capybara can attach to
page.execute_script <<-JS.strip_heredoc
var fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type: 'file'}
).appendTo('body');
window._dropzoneComplete = false;
JS
# Attach the file to the fake input selector with Capybara
attach_file('fakeFileInput', file_path)
# Manually trigger a Dropzone "drop" event with the fake input's file list
page.execute_script <<-JS.strip_heredoc
var fileList = [$('#fakeFileInput')[0].files[0]];
var e = jQuery.Event('drop', { dataTransfer : { files : fileList } });
var dropzone = $('.div-dropzone')[0].dropzone;
dropzone.on('queuecomplete', function() {
window._dropzoneComplete = true;
});
dropzone.listeners[0].events.drop(e);
JS
# Wait until Dropzone's fired `queuecomplete`
loop until page.evaluate_script('window._dropzoneComplete === true')
end
end