import {appConfig} from 'appConfig'; describe('Upload controller', () => { var files = [{type: 'image/jpeg'}], UPLOAD_URL = 'upload_url'; beforeEach(window.module('superdesk.apps.searchProviders')); beforeEach(window.module('superdesk.apps.archive')); beforeEach(window.module(($provide) => { $provide.service('upload', function($q) { this.start = function(config) { this.defer = $q.defer(); return this.defer.promise; }; }); $provide.service('archiveService', function() { this.addTaskToArticle = function(item) { /* no-op */ }; }); })); beforeEach(inject((session) => { session.identity = {_id: 'user:1', byline: 'Admin'}; })); beforeEach(inject((api, $q) => { spyOn(api.archive, 'getUrl').and.returnValue($q.when(UPLOAD_URL)); spyOn(api.archive, 'getHeaders').and.returnValue({}); spyOn(api.archive, 'update').and.returnValue($q.when({})); })); it('can upload files when added', inject(($controller, $rootScope, api, upload) => { var scope = $rootScope.$new(true); appConfig.validator_media_metadata = { headline: { required: true, }, alt_text: { required: true, }, description_text: { required: true, }, copyrightholder: { required: false, }, byline: { required: false, }, usageterms: { required: false, }, copyrightnotice: { required: false, }, }; spyOn(upload, 'start').and.callThrough(); scope.resolve = function() { /* no-op */ }; var resolve = spyOn(scope, 'resolve'); $controller('UploadController', {$scope: scope}); $rootScope.$digest(); expect(scope.items.length).toBe(0); scope.addFiles(files).then(() => { $rootScope.$digest(); expect(scope.items.length).toBe(1); expect(scope.items[0].file.type).toBe('text/plain'); expect(scope.items[0].meta).not.toBe(undefined); expect(scope.items[0].progress).toBe(0); // mandatory fields scope.items[0].meta.headline = 'headline text'; scope.items[0].meta.slugline = 'slugline text'; scope.items[0].meta.description_text = 'description'; scope.items[0].meta.alt_text = 'alt text'; scope.save(); $rootScope.$digest(); expect(upload.start).toHaveBeenCalledWith({ method: 'POST', url: UPLOAD_URL, data: {media: files[0]}, headers: api.archive.getHeaders(), }); upload.defer.notify({ total: 100, loaded: 50, }); $rootScope.$digest(); expect(scope.items[0].progress).toBe(50); upload.defer.resolve({data: {}}); $rootScope.$digest(); expect(resolve).toHaveBeenCalledWith([{}]); }); })); it('can try again to upload when try again is clicked', inject(($controller, $rootScope, $q, api, upload) => { var scope = $rootScope.$new(true); spyOn(upload, 'start').and.callThrough(); scope.resolve = function() { /* no-op */ }; $controller('UploadController', {$scope: scope}); $rootScope.$digest(); expect(scope.items.length).toBe(0); scope.addFiles(files).then(() => { $rootScope.$digest(); expect(scope.items.length).toBe(1); expect(scope.items[0].file.type).toBe('text/plain'); expect(scope.items[0].meta).not.toBe(undefined); expect(scope.items[0].progress).toBe(0); // mandatory fields scope.items[0].meta.headline = 'headline text'; scope.items[0].meta.slugline = 'slugline text'; scope.items[0].meta.description_text = 'description'; scope.failed = true; scope.tryAgain(); $rootScope.$digest(); expect(upload.start).toHaveBeenCalledWith({ method: 'POST', url: UPLOAD_URL, data: {media: files[0]}, headers: api.archive.getHeaders(), }); }); })); });