From e3a192bcea5f8d78932f34f7fadb49e19a57cb05 Mon Sep 17 00:00:00 2001 From: joan Date: Mon, 9 Aug 2021 12:37:52 +0200 Subject: [PATCH] Updated unit test --- front/salix/components/upload-photo/index.js | 7 +- .../components/upload-photo/index.spec.js | 82 +++++++++++++++++-- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/front/salix/components/upload-photo/index.js b/front/salix/components/upload-photo/index.js index 8114b7b4b..4a0180946 100644 --- a/front/salix/components/upload-photo/index.js +++ b/front/salix/components/upload-photo/index.js @@ -143,14 +143,13 @@ export default class UploadPhoto extends Component { height: output.height } }; - this.editor.result(options) + return this.editor.result(options) .then(blob => this.newPhoto.blob = blob) .then(() => this.makeRequest()); } catch (e) { this.vnApp.showError(this.$t(e.message)); return false; } - return true; } /** @@ -171,7 +170,9 @@ export default class UploadPhoto extends Component { const formData = new FormData(); const now = new Date(); const timestamp = now.getTime(); - formData.append('blob', this.newPhoto.blob, file.name + '_' + timestamp); + const fileName = `${file.name}_${timestamp}`; + + formData.append('blob', this.newPhoto.blob, fileName); return formData; }, diff --git a/front/salix/components/upload-photo/index.spec.js b/front/salix/components/upload-photo/index.spec.js index 0ae7a5425..e8ac05fd4 100644 --- a/front/salix/components/upload-photo/index.spec.js +++ b/front/salix/components/upload-photo/index.spec.js @@ -6,7 +6,9 @@ describe('Salix', () => { let $scope; let $httpBackend; - beforeEach(ngModule('salix')); + beforeEach(ngModule('salix', $translateProvider => { + $translateProvider.translations('en', {}); + })); beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { $scope = $rootScope.$new(); @@ -14,12 +16,58 @@ describe('Salix', () => { const $element = angular.element(''); controller = $componentController('vnUploadPhoto', {$element, $scope}); controller.newPhoto = {}; + controller.$t = m => m; })); afterEach(() => { $scope.$destroy(); }); + describe('viewportSelection()', () => { + it('should call to displayEditor() and updatePhotoPreview() methods', () => { + controller.displayEditor = jest.fn(); + controller.updatePhotoPreview = jest.fn(); + + const files = [{name: 'test.jpg'}]; + controller.newPhoto.files = files; + + controller.viewportSelection = {code: 'normal'}; + + expect(controller.displayEditor).toHaveBeenCalledWith(); + expect(controller.updatePhotoPreview).toHaveBeenCalledWith(files); + }); + }); + + describe('displayEditor()', () => { + it('should define the editor property', () => { + controller.viewportSelection = { + code: 'normal', + description: 'Normal', + viewport: { + width: 400, + height: 400 + }, + output: { + width: 1200, + height: 1200 + } + }; + const element = document.createElement('div'); + + jest.spyOn(document, 'getElementById').mockReturnValue(element); + + controller.displayEditor(); + + const editor = controller.editor; + + expect(editor).toBeDefined(); + expect(editor.options.viewport.width).toEqual(400); + expect(editor.options.viewport.width).toEqual(400); + expect(editor.options.boundary.width).toEqual(600); + expect(editor.options.boundary.height).toEqual(600); + }); + }); + describe('onUploadAccept()', () => { it('should throw an error message containing "Select an image"', () => { jest.spyOn(controller.vnApp, 'showError'); @@ -29,13 +77,33 @@ describe('Salix', () => { expect(controller.vnApp.showError).toHaveBeenCalledWith('Select an image'); }); - it('should call to the makeRequest() method', () => { + it('should call to the makeRequest() method', done => { + controller.editor = { + result: () => {} + }; + jest.spyOn(controller, 'makeRequest'); + jest.spyOn(controller.editor, 'result').mockReturnValue(new Promise(resolve => resolve('blobFile'))); + + controller.viewportSelection = { + code: 'normal', + description: 'Normal', + viewport: { + width: 400, + height: 400 + }, + output: { + width: 1200, + height: 1200 + } + }; controller.newPhoto.files = [0]; - controller.onUploadAccept(); - - expect(controller.makeRequest).toHaveBeenCalledWith(); + controller.onUploadAccept().then(() => { + expect(controller.newPhoto.blob).toEqual('blobFile'); + expect(controller.makeRequest).toHaveBeenCalledWith(); + done(); + }).catch(done.fail); }); }); @@ -44,7 +112,11 @@ describe('Salix', () => { jest.spyOn(controller.vnApp, 'showSuccess'); jest.spyOn(controller, 'emit'); + $httpBackend.expectGET('ImageContainers/allowedContentTypes').respond(200, ['image/jpg']); + controller.newPhoto.files = [{name: 'hola'}]; + controller.newPhoto.blob = new Blob([]); + $httpBackend.expectRoute('POST', 'Images/upload').respond(200); controller.makeRequest(); $httpBackend.flush();