From 9b221e7861949208dc83c78f9f4b0108b655e550 Mon Sep 17 00:00:00 2001 From: jelhan Date: Fri, 26 Aug 2016 19:04:50 +0200 Subject: [PATCH] add a test for saving a new poll fails due to server --- app/controllers/create/settings.js | 17 +++-- tests/acceptance/create-a-poll-test.js | 94 ++++++++++++++++---------- 2 files changed, 68 insertions(+), 43 deletions(-) diff --git a/app/controllers/create/settings.js b/app/controllers/create/settings.js index 53ba87b..cdef27b 100644 --- a/app/controllers/create/settings.js +++ b/app/controllers/create/settings.js @@ -59,13 +59,18 @@ export default Controller.extend(Validations, { } // save poll - model.save().then((model) => { - // reload as workaround for bug: duplicated records after save - model.reload().then((model) => { - // redirect to new poll - this.get('target').send('transitionToPoll', model); + model.save() + .then((model) => { + // reload as workaround for bug: duplicated records after save + model.reload().then((model) => { + // redirect to new poll + this.get('target').send('transitionToPoll', model); + }); + }) + .catch(() => { + // ToDo: Show feedback to user + return; }); - }); } } }, diff --git a/tests/acceptance/create-a-poll-test.js b/tests/acceptance/create-a-poll-test.js index e029029..e119c8d 100644 --- a/tests/acceptance/create-a-poll-test.js +++ b/tests/acceptance/create-a-poll-test.js @@ -15,6 +15,7 @@ import pagePollParticipation from 'croodle/tests/pages/poll/participation'; const { run } = Ember; let application, server; +let serverAvailable = true; const randomString = function(length) { return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1); @@ -34,6 +35,10 @@ module('Acceptance | create a poll', { server.post('/polls', function(request) { + if (!serverAvailable) { + return [503]; + } + let ret = serverPostPolls(request.requestBody, pollId); lastCreatedPoll = ret[2]; return ret; @@ -42,6 +47,10 @@ module('Acceptance | create a poll', { server.get(`/polls/${pollId}`, function() { + if (!serverAvailable) { + return [503]; + } + return [ 200, { 'Content-Type': 'application/json' }, @@ -177,46 +186,57 @@ test('create a default poll', function(assert) { 'available answers selection has autofocus' ); + // simulate temporate server error + serverAvailable = false; pageCreateSettings .save(); - andThen(function() { - assert.equal(currentPath(), 'poll.participation'); - assert.ok( - pagePollParticipation.urlIsValid() === true, - `poll url ${currentURL()} is valid` - ); - assert.equal( - pagePollParticipation.title, - 'default poll', - 'poll title is correct' - ); - assert.equal( - pagePollParticipation.description, - '', - 'poll description is correct' - ); - const dayFormat = moment.localeData().longDateFormat('LLLL') - .replace( - moment.localeData().longDateFormat('LT'), '') - .trim(); - assert.deepEqual( - pagePollParticipation.options().labels, - dates.map((date) => date.format(dayFormat)), - 'options are correctly labeled' - ); - assert.deepEqual( - pagePollParticipation.options().answers, - [ - t('answerTypes.yes.label').toString(), - t('answerTypes.no.label').toString() - ], - 'answers are correctly labeled' - ); - assert.ok( - pagePollParticipation.nameHasFocus, - 'name input has autofocus' - ); + andThen(() => { + assert.equal(currentPath(), 'create.settings'); + + serverAvailable = true; + + pageCreateSettings + .save(); + + andThen(function() { + assert.equal(currentPath(), 'poll.participation'); + assert.ok( + pagePollParticipation.urlIsValid() === true, + `poll url ${currentURL()} is valid` + ); + assert.equal( + pagePollParticipation.title, + 'default poll', + 'poll title is correct' + ); + assert.equal( + pagePollParticipation.description, + '', + 'poll description is correct' + ); + const dayFormat = moment.localeData().longDateFormat('LLLL') + .replace( + moment.localeData().longDateFormat('LT'), '') + .trim(); + assert.deepEqual( + pagePollParticipation.options().labels, + dates.map((date) => date.format(dayFormat)), + 'options are correctly labeled' + ); + assert.deepEqual( + pagePollParticipation.options().answers, + [ + t('answerTypes.yes.label').toString(), + t('answerTypes.no.label').toString() + ], + 'answers are correctly labeled' + ); + assert.ok( + pagePollParticipation.nameHasFocus, + 'name input has autofocus' + ); + }); }); }); });