import Pretender from 'pretender'; import hbs from 'htmlbars-inline-precompile'; import sinon from 'sinon'; import {click, find, findAll, render, settled, waitFor, waitUntil} from '@ember/test-helpers'; import {createFile} from '../../helpers/file-upload'; import {describe, it} from 'mocha'; import {expect} from 'chai'; import {setupRenderingTest} from 'ember-mocha'; const stubSuccessfulUpload = function (server, delay = 0) { server.post('/ghost/api/v3/admin/images/upload/', function () { return [200, {'Content-Type': 'application/json'}, '{"images": [{"url": "/content/images/test.png"}]}']; }, delay); }; const stubFailedUpload = function (server, code, error, delay = 0) { server.post('/ghost/api/v3/admin/images/upload/', function () { return [code, {'Content-Type': 'application/json'}, JSON.stringify({ errors: [{ type: error, message: `Error: ${error}` }] })]; }, delay); }; describe('Integration: Component: gh-uploader', function () { setupRenderingTest(); let server; beforeEach(function () { server = new Pretender(); }); afterEach(function () { server.shutdown(); }); describe('uploads', function () { beforeEach(function () { stubSuccessfulUpload(server); }); it('triggers uploads when `files` is set', async function () { await render(hbs`{{#gh-uploader files=files}}{{/gh-uploader}}`); this.set('files', [createFile()]); await settled(); let [lastRequest] = server.handledRequests; expect(server.handledRequests.length).to.equal(1); expect(lastRequest.url).to.equal('/ghost/api/v3/admin/images/upload/'); // requestBody is a FormData object // this will fail in anything other than Chrome and Firefox // https://developer.mozilla.org/en-US/docs/Web/API/FormData#Browser_compatibility expect(lastRequest.requestBody.has('file')).to.be.true; }); it('triggers multiple uploads', async function () { await render(hbs`{{#gh-uploader files=files}}{{/gh-uploader}}`); this.set('files', [createFile(), createFile()]); await settled(); expect(server.handledRequests.length).to.equal(2); }); it('triggers onStart when upload starts', async function () { this.set('uploadStarted', sinon.spy()); await render(hbs`{{#gh-uploader files=files onStart=(action uploadStarted)}}{{/gh-uploader}}`); this.set('files', [createFile(), createFile()]); await settled(); expect(this.get('uploadStarted').calledOnce).to.be.true; }); it('triggers onUploadSuccess when a file uploads', async function () { this.set('fileUploaded', sinon.spy()); await render(hbs`{{#gh-uploader files=files onUploadSuccess=(action fileUploaded)}}{{/gh-uploader}}`); this.set('files', [createFile(['test'], {name: 'file1.png'}), createFile()]); await settled(); // triggered for each file expect(this.get('fileUploaded').calledTwice).to.be.true; // filename and url is passed in arg let firstCall = this.get('fileUploaded').getCall(0); expect(firstCall.args[0].fileName).to.equal('file1.png'); expect(firstCall.args[0].url).to.equal('/content/images/test.png'); }); it('triggers onComplete when all files uploaded', async function () { this.set('uploadsFinished', sinon.spy()); await render(hbs`{{#gh-uploader files=files onComplete=(action uploadsFinished)}}{{/gh-uploader}}`); this.set('files', [ createFile(['test'], {name: 'file1.png'}), createFile(['test'], {name: 'file2.png'}) ]); await settled(); expect(this.get('uploadsFinished').calledOnce).to.be.true; // array of filenames and urls is passed in arg let [result] = this.get('uploadsFinished').getCall(0).args; expect(result.length).to.equal(2); expect(result[0].fileName).to.equal('file1.png'); expect(result[0].url).to.equal('/content/images/test.png'); expect(result[1].fileName).to.equal('file2.png'); expect(result[1].url).to.equal('/content/images/test.png'); }); it('onComplete only passes results for last upload', async function () { this.set('uploadsFinished', sinon.spy()); await render(hbs`{{#gh-uploader files=files onComplete=(action uploadsFinished)}}{{/gh-uploader}}`); this.set('files', [ createFile(['test'], {name: 'file1.png'}) ]); await settled(); this.set('files', [ createFile(['test'], {name: 'file2.png'}) ]); await settled(); let [results] = this.get('uploadsFinished').getCall(1).args; expect(results.length).to.equal(1); expect(results[0].fileName).to.equal('file2.png'); }); it('onComplete returns results in same order as selected', async function () { // first request has a delay to simulate larger file server.post('/ghost/api/v3/admin/images/upload/', function () { // second request has no delay to simulate small file stubSuccessfulUpload(server, 0); return [200, {'Content-Type': 'application/json'}, '"/content/images/test.png"']; }, 100); this.set('uploadsFinished', sinon.spy()); await render(hbs`{{#gh-uploader files=files onComplete=(action uploadsFinished)}}{{/gh-uploader}}`); this.set('files', [ createFile(['test'], {name: 'file1.png'}), // large - finishes last createFile(['test'], {name: 'file2.png'}) // small - finishes first ]); await settled(); let [results] = this.get('uploadsFinished').getCall(0).args; expect(results.length).to.equal(2); expect(results[0].fileName).to.equal('file1.png'); }); it('doesn\'t allow new files to be set whilst uploading', async function () { let errorSpy = sinon.spy(console, 'error'); stubSuccessfulUpload(server, 100); await render(hbs`{{#gh-uploader files=files}}{{/gh-uploader}}`); this.set('files', [createFile()]); // logs error because upload is in progress this.set('files', [createFile()]); await settled(); // runs ok because original upload has finished this.set('files', [createFile()]); await settled(); expect(server.handledRequests.length).to.equal(2); expect(errorSpy.calledOnce).to.be.true; errorSpy.restore(); }); it('yields isUploading whilst upload is in progress', async function () { stubSuccessfulUpload(server, 100); await render(hbs` {{#gh-uploader files=files as |uploader|}} {{#if uploader.isUploading}}
{{/if}} {{/gh-uploader}}`); this.set('files', [createFile(), createFile()]); await waitFor('.is-uploading-test', {timeout: 150}); await settled(); expect(find('.is-uploading-test')).to.not.exist; }); it('yields progressBar component with total upload progress', async function () { stubSuccessfulUpload(server, 100); await render(hbs` {{#gh-uploader files=files as |uploader|}} {{uploader.progressBar}} {{/gh-uploader}}`); this.set('files', [createFile(), createFile()]); await waitFor('[data-test-progress-bar]', {timeout: 150}); let progressBar = find('[data-test-progress-bar]'); await waitUntil(() => { let width = parseInt(progressBar.style.width); return width > 50; }, {timeout: 150}); await settled(); let finalProgressWidth = parseInt(find('[data-test-progress-bar]').style.width); expect(finalProgressWidth, 'final progress width').to.equal(100); }); it('yields files property', async function () { await render(hbs` {{#gh-uploader files=files as |uploader|}} {{#each uploader.files as |file|}}