Skip to content

Commit

Permalink
Add flash message to submission details page for submission deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrah committed Feb 27, 2024
1 parent f7eb8cc commit 66a7f2d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/controllers/submissions/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}
}
}
27 changes: 27 additions & 0 deletions tests/unit/controllers/submissions/detail-test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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');
});
});

0 comments on commit 66a7f2d

Please sign in to comment.