diff --git a/app/controllers/submissions/new.js b/app/controllers/submissions/new.js index 475a466d..81a4b617 100644 --- a/app/controllers/submissions/new.js +++ b/app/controllers/submissions/new.js @@ -135,10 +135,11 @@ export default class SubmissionsNew extends Controller { }); if (result.value) { - await this.submissionHandler.deleteSubmission(submission); - // Clear the shared ignore list, then add the 'deleted' submission ID ignoreList.clearIgnore(); - ignoreList.ignore(get(submission, 'id')); + if (submission.id) { + await this.submissionHandler.deleteSubmission(submission); + ignoreList.ignore(get(submission, 'id')); + } this.router.transitionTo('submissions'); } } diff --git a/tests/unit/controllers/submissions/new-test.js b/tests/unit/controllers/submissions/new-test.js index af30f716..14df52a1 100644 --- a/tests/unit/controllers/submissions/new-test.js +++ b/tests/unit/controllers/submissions/new-test.js @@ -316,8 +316,12 @@ module('Unit | Controller | submissions/new', (hooks) => { test('abort should delete submission and transition', async function (assert) { assert.expect(3); + let submission = EmberObject.create({ + id: 'sub:0', + }); + const model = EmberObject.create({ - newSubmission: EmberObject.create(), + newSubmission: submission, }); const controller = this.owner.lookup('controller:submissions/new'); @@ -349,4 +353,48 @@ module('Unit | Controller | submissions/new', (hooks) => { controller.send('abort'); }); + + test('abort should not delete submission if submission.id is undefined', async function (assert) { + assert.expect(3); + + let submission = EmberObject.create({ + id: undefined, + }); + + const model = EmberObject.create({ + newSubmission: submission, + }); + const controller = this.owner.lookup('controller:submissions/new'); + + controller.set('model', model); + + // Mock global 'swal' for this test + swal = () => { + assert.ok(true); + return Promise.resolve({ + value: 'moo', + }); + }; + + let deleteSubmissionCalled = false; + + // Having this mocked function run shows that the service will delete the submission + controller.set( + 'submissionHandler', + EmberObject.create({ + deleteSubmission(_sub) { + deleteSubmissionCalled = true; + return Promise.resolve(); + }, + }) + ); + controller.set('router', { + transitionTo: (name) => { + assert.false(deleteSubmissionCalled); + assert.strictEqual(name, 'submissions', 'unexpected transition was named'); + }, + }); + + controller.send('abort'); + }); });