2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
2016-04-15 17:45:50 +03:00
|
|
|
import Pretender from 'pretender';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Service from '@ember/service';
|
2017-05-29 21:50:03 +03:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import wait from 'ember-test-helpers/wait';
|
2016-08-22 14:45:33 +03:00
|
|
|
import {UnsupportedMediaTypeError} from 'ghost-admin/services/ajax';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {createFile, fileUpload} from '../../helpers/file-upload';
|
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {setupComponentTest} from 'ember-mocha';
|
2016-06-30 17:45:02 +03:00
|
|
|
|
|
|
|
const notificationsStub = Service.extend({
|
2016-11-14 16:16:51 +03:00
|
|
|
showAPIError() {
|
2016-06-30 17:45:02 +03:00
|
|
|
// noop - to be stubbed
|
|
|
|
}
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
const stubSuccessfulUpload = function (server, delay = 0) {
|
|
|
|
server.post('/ghost/api/v0.1/uploads/', function () {
|
|
|
|
return [200, {'Content-Type': 'application/json'}, '"/content/images/test.png"'];
|
|
|
|
}, delay);
|
|
|
|
};
|
|
|
|
|
|
|
|
const stubFailedUpload = function (server, code, error, delay = 0) {
|
|
|
|
server.post('/ghost/api/v0.1/uploads/', function () {
|
|
|
|
return [code, {'Content-Type': 'application/json'}, JSON.stringify({
|
|
|
|
errors: [{
|
|
|
|
errorType: error,
|
|
|
|
message: `Error: ${error}`
|
|
|
|
}]
|
|
|
|
})];
|
|
|
|
}, delay);
|
|
|
|
};
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
describe('Integration: Component: gh-file-uploader', function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
setupComponentTest('gh-file-uploader', {
|
2016-04-15 17:45:50 +03:00
|
|
|
integration: true
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
let server;
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
server = new Pretender();
|
|
|
|
this.set('uploadUrl', '/ghost/api/v0.1/uploads/');
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.register('service:notifications', notificationsStub);
|
|
|
|
this.inject.service('notifications', {as: 'notifications'});
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
afterEach(function () {
|
|
|
|
server.shutdown();
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
it('renders', function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader}}`);
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('label').text().trim(), 'default label')
|
|
|
|
.to.equal('Select or drag-and-drop a file');
|
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('allows file input "accept" attribute to be changed', function () {
|
|
|
|
this.render(hbs`{{gh-file-uploader}}`);
|
|
|
|
expect(
|
|
|
|
this.$('input[type="file"]').attr('accept'),
|
|
|
|
'default "accept" attribute'
|
|
|
|
).to.equal('text/csv');
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader accept="application/zip"}}`);
|
|
|
|
expect(
|
|
|
|
this.$('input[type="file"]').attr('accept'),
|
|
|
|
'specified "accept" attribute'
|
|
|
|
).to.equal('application/zip');
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('renders form with supplied label text', function () {
|
|
|
|
this.set('labelText', 'My label');
|
|
|
|
this.render(hbs`{{gh-file-uploader labelText=labelText}}`);
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('label').text().trim(), 'label')
|
|
|
|
.to.equal('My label');
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('generates request to supplied endpoint', function (done) {
|
|
|
|
stubSuccessfulUpload(server);
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
|
|
|
|
|
|
|
wait().then(() => {
|
|
|
|
expect(server.handledRequests.length).to.equal(1);
|
|
|
|
expect(server.handledRequests[0].url).to.equal('/ghost/api/v0.1/uploads/');
|
|
|
|
done();
|
2016-04-15 17:45:50 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('fires uploadSuccess action on successful upload', function (done) {
|
|
|
|
let uploadSuccess = sinon.spy();
|
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadSuccess=(action uploadSuccess)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadSuccess.calledOnce).to.be.true;
|
|
|
|
expect(uploadSuccess.firstCall.args[0]).to.equal('/content/images/test.png');
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('doesn\'t fire uploadSuccess action on failed upload', function (done) {
|
|
|
|
let uploadSuccess = sinon.spy();
|
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubFailedUpload(server, 500);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadSuccess=(action uploadSuccess)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadSuccess.calledOnce).to.be.false;
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('fires fileSelected action on file selection', function (done) {
|
|
|
|
let fileSelected = sinon.spy();
|
|
|
|
this.set('fileSelected', fileSelected);
|
2016-08-19 14:42:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-08-19 14:42:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl fileSelected=(action fileSelected)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-08-19 14:42:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(fileSelected.calledOnce).to.be.true;
|
2018-03-26 13:41:45 +03:00
|
|
|
expect(fileSelected.args[0]).to.not.be.empty;
|
2016-11-24 01:50:57 +03:00
|
|
|
done();
|
2016-08-19 14:42:47 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-08-19 14:42:47 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('fires uploadStarted action on upload start', function (done) {
|
|
|
|
let uploadStarted = sinon.spy();
|
|
|
|
this.set('uploadStarted', uploadStarted);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadStarted=(action uploadStarted)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadStarted.calledOnce).to.be.true;
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('fires uploadFinished action on successful upload', function (done) {
|
|
|
|
let uploadFinished = sinon.spy();
|
|
|
|
this.set('uploadFinished', uploadFinished);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadFinished=(action uploadFinished)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadFinished.calledOnce).to.be.true;
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('fires uploadFinished action on failed upload', function (done) {
|
|
|
|
let uploadFinished = sinon.spy();
|
|
|
|
this.set('uploadFinished', uploadFinished);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubFailedUpload(server);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadFinished=(action uploadFinished)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadFinished.calledOnce).to.be.true;
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays invalid file type error', function (done) {
|
|
|
|
stubFailedUpload(server, 415, 'UnsupportedMediaTypeError');
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
|
|
|
|
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/The file type you uploaded is not supported/);
|
2017-02-16 22:50:05 +03:00
|
|
|
expect(this.$('.gh-btn-green').length, 'reset button is displayed').to.equal(1);
|
|
|
|
expect(this.$('.gh-btn-green').text()).to.equal('Try Again');
|
2016-11-24 01:50:57 +03:00
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('displays file too large for server error', function (done) {
|
|
|
|
stubFailedUpload(server, 413, 'RequestEntityTooLargeError');
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/The file you uploaded was larger/);
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles file too large error directly from the web server', function (done) {
|
|
|
|
server.post('/ghost/api/v0.1/uploads/', function () {
|
|
|
|
return [413, {}, ''];
|
|
|
|
});
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/The file you uploaded was larger/);
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('displays other server-side error with message', function (done) {
|
|
|
|
stubFailedUpload(server, 400, 'UnknownError');
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/Error: UnknownError/);
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles unknown failure', function (done) {
|
|
|
|
server.post('/ghost/api/v0.1/uploads/', function () {
|
|
|
|
return [500, {'Content-Type': 'application/json'}, ''];
|
|
|
|
});
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/Something went wrong/);
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('triggers notifications.showAPIError for VersionMismatchError', function (done) {
|
|
|
|
let showAPIError = sinon.spy();
|
|
|
|
this.set('notifications.showAPIError', showAPIError);
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubFailedUpload(server, 400, 'VersionMismatchError');
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(showAPIError.calledOnce).to.be.true;
|
|
|
|
done();
|
2016-06-30 17:45:02 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('doesn\'t trigger notifications.showAPIError for other errors', function (done) {
|
|
|
|
let showAPIError = sinon.spy();
|
|
|
|
this.set('notifications.showAPIError', showAPIError);
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubFailedUpload(server, 400, 'UnknownError');
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(showAPIError.called).to.be.false;
|
|
|
|
done();
|
2016-06-30 17:45:02 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-06-30 17:45:02 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('can be reset after a failed upload', function (done) {
|
|
|
|
stubFailedUpload(server, 400, 'UnknownError');
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
run(() => {
|
2017-02-16 22:50:05 +03:00
|
|
|
this.$('.gh-btn-green').click();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(this.$('input[type="file"]').length).to.equal(1);
|
|
|
|
done();
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('displays upload progress', function (done) {
|
|
|
|
this.set('done', done);
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
// pretender fires a progress event every 50ms
|
|
|
|
stubSuccessfulUpload(server, 150);
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadFinished=(action done)}}`);
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
// after 75ms we should have had one progress event
|
|
|
|
run.later(this, function () {
|
|
|
|
expect(this.$('.progress .bar').length).to.equal(1);
|
|
|
|
let [, percentageWidth] = this.$('.progress .bar').attr('style').match(/width: (\d+)%?/);
|
2018-03-26 13:41:45 +03:00
|
|
|
percentageWidth = Number.parseInt(percentageWidth);
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(percentageWidth).to.be.above(0);
|
|
|
|
expect(percentageWidth).to.be.below(100);
|
|
|
|
}, 75);
|
|
|
|
});
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('handles drag over/leave', function () {
|
|
|
|
this.render(hbs`{{gh-file-uploader}}`);
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
2016-11-14 16:16:51 +03:00
|
|
|
// eslint-disable-next-line new-cap
|
2016-11-24 01:50:57 +03:00
|
|
|
let dragover = $.Event('dragover', {
|
2016-05-27 15:27:46 +03:00
|
|
|
dataTransfer: {
|
2016-11-24 01:50:57 +03:00
|
|
|
files: []
|
2016-05-27 15:27:46 +03:00
|
|
|
}
|
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
this.$('.gh-image-uploader').trigger(dragover);
|
|
|
|
});
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('.gh-image-uploader').hasClass('-drag-over'), 'has drag-over class').to.be.true;
|
2016-05-27 15:27:46 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
this.$('.gh-image-uploader').trigger('dragleave');
|
2016-05-27 15:27:46 +03:00
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
expect(this.$('.gh-image-uploader').hasClass('-drag-over'), 'has drag-over class').to.be.false;
|
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('triggers file upload on file drop', function (done) {
|
|
|
|
let uploadSuccess = sinon.spy();
|
|
|
|
// eslint-disable-next-line new-cap
|
|
|
|
let drop = $.Event('drop', {
|
|
|
|
dataTransfer: {
|
|
|
|
files: [createFile(['test'], {name: 'test.csv'})]
|
|
|
|
}
|
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
|
|
|
this.render(hbs`{{gh-file-uploader url=uploadUrl uploadSuccess=(action uploadSuccess)}}`);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
run(() => {
|
|
|
|
this.$('.gh-image-uploader').trigger(drop);
|
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadSuccess.calledOnce).to.be.true;
|
|
|
|
expect(uploadSuccess.firstCall.args[0]).to.equal('/content/images/test.png');
|
|
|
|
done();
|
2016-08-22 14:45:33 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('validates extension by default', function (done) {
|
|
|
|
let uploadSuccess = sinon.spy();
|
|
|
|
let uploadFailed = sinon.spy();
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
|
|
|
this.set('uploadFailed', uploadFailed);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader
|
|
|
|
url=uploadUrl
|
|
|
|
uploadSuccess=(action uploadSuccess)
|
|
|
|
uploadFailed=(action uploadFailed)}}`);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.txt'});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(uploadSuccess.called).to.be.false;
|
|
|
|
expect(uploadFailed.calledOnce).to.be.true;
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/The file type you uploaded is not supported/);
|
|
|
|
done();
|
2016-08-22 14:45:33 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
it('uploads if validate action supplied and returns true', function (done) {
|
|
|
|
let validate = sinon.stub().returns(true);
|
|
|
|
let uploadSuccess = sinon.spy();
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.set('validate', validate);
|
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
stubSuccessfulUpload(server);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-file-uploader
|
|
|
|
url=uploadUrl
|
|
|
|
uploadSuccess=(action uploadSuccess)
|
|
|
|
validate=(action validate)}}`);
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
2016-08-22 14:45:33 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
wait().then(() => {
|
|
|
|
expect(validate.calledOnce).to.be.true;
|
|
|
|
expect(uploadSuccess.calledOnce).to.be.true;
|
|
|
|
done();
|
2016-08-22 14:45:33 +03:00
|
|
|
});
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips upload and displays error if validate action supplied and doesn\'t return true', function (done) {
|
|
|
|
let validate = sinon.stub().returns(new UnsupportedMediaTypeError());
|
|
|
|
let uploadSuccess = sinon.spy();
|
|
|
|
let uploadFailed = sinon.spy();
|
|
|
|
|
|
|
|
this.set('validate', validate);
|
|
|
|
this.set('uploadSuccess', uploadSuccess);
|
|
|
|
this.set('uploadFailed', uploadFailed);
|
|
|
|
|
|
|
|
stubSuccessfulUpload(server);
|
|
|
|
|
|
|
|
this.render(hbs`{{gh-file-uploader
|
|
|
|
url=uploadUrl
|
|
|
|
uploadSuccess=(action uploadSuccess)
|
|
|
|
uploadFailed=(action uploadFailed)
|
|
|
|
validate=(action validate)}}`);
|
|
|
|
|
|
|
|
fileUpload(this.$('input[type="file"]'), ['test'], {name: 'test.csv'});
|
|
|
|
|
|
|
|
wait().then(() => {
|
|
|
|
expect(validate.calledOnce).to.be.true;
|
|
|
|
expect(uploadSuccess.called).to.be.false;
|
|
|
|
expect(uploadFailed.calledOnce).to.be.true;
|
|
|
|
expect(this.$('.failed').length, 'error message is displayed').to.equal(1);
|
|
|
|
expect(this.$('.failed').text()).to.match(/The file type you uploaded is not supported/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|