diff --git a/app/controllers/submissions/detail.js b/app/controllers/submissions/detail.js index 458d9dd1..7220c22c 100644 --- a/app/controllers/submissions/detail.js +++ b/app/controllers/submissions/detail.js @@ -474,10 +474,17 @@ export default class SubmissionsDetail extends Controller { if (result.value) { const ignoreList = this.searchHelper; - await this.submissionHandler.deleteSubmission(submission); - ignoreList.clearIgnore(); - ignoreList.ignore(submission.get('id')); - this.transitionToRoute('submissions'); + try { + await this.submissionHandler.deleteSubmission(submission); + + ignoreList.clearIgnore(); + ignoreList.ignore(submission.get('id')); + this.transitionToRoute('submissions'); + } catch (e) { + this.flashMessages.danger( + 'We encountered an error deleting this draft submission. Please try again later or contact your administrator' + ); + } } } } diff --git a/tests/unit/controllers/submissions/detail-test.js b/tests/unit/controllers/submissions/detail-test.js index 90d8f087..0c48a753 100644 --- a/tests/unit/controllers/submissions/detail-test.js +++ b/tests/unit/controllers/submissions/detail-test.js @@ -1,6 +1,7 @@ import EmberObject from '@ember/object'; import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; +import Sinon from 'sinon'; module('Unit | Controller | submissions/detail', (hooks) => { setupTest(hooks); @@ -38,4 +39,30 @@ module('Unit | Controller | submissions/detail', (hooks) => { controller.send('deleteSubmission', submission); }); + + test('error message shown on submission deletion error', async function (assert) { + const submission = {}; + + // Mock global SweetAlert. Mocks a user clicking OK on the popup + swal = Sinon.fake.resolves({ value: true }); + + const controller = this.owner.lookup('controller:submissions/detail'); + const transitionFake = Sinon.replace(controller, 'transitionToRoute', Sinon.fake()); + + controller.submissionHandler = this.owner.lookup('service:submission-handler'); + const deleteFake = Sinon.replace(controller.submissionHandler, 'deleteSubmission', Sinon.fake.rejects()); + + controller.flashMessages = this.owner.lookup('service:flash-messages'); + const flashFake = Sinon.replace(controller.flashMessages, 'danger', Sinon.fake()); + + // Note: using controller.send resolves immediately + // making subsequent assertions evaluate before the controller action fires + // Can't really use Sinon in a nice way unless we call the controller + // method directly + await controller.deleteSubmission(submission); + + assert.ok(deleteFake.calledOnce, 'Submission handler delete should be called'); + assert.ok(flashFake.calledOnce, 'Flash message should be called'); + assert.equal(transitionFake.callCount, 0, 'Transition should not be called'); + }); });