decide.nolog.cz/tests/acceptance/create-a-poll-test.js

255 lines
6.7 KiB
JavaScript
Raw Normal View History

import Ember from 'ember';
2015-06-11 10:53:22 +02:00
import { module, test } from 'qunit';
import startApp from '../helpers/start-app';
import Pretender from 'pretender';
import serverPostPolls from '../helpers/server-post-polls';
import moment from 'moment';
/* jshint proto: true */
2015-06-11 10:53:22 +02:00
let application, server;
const randomString = function(length) {
return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1);
};
module('Acceptance | create a poll', {
beforeEach() {
let lastCreatedPoll = {};
const pollId = randomString(10);
application = startApp();
application.__container__.lookup('adapter:application').__proto__.namespace = '';
server = new Pretender();
server.post('/polls',
function(request) {
let ret = serverPostPolls(request.requestBody, pollId);
lastCreatedPoll = ret[2];
return ret;
}
);
server.get(`/polls/${pollId}`,
function() {
return [
200,
{ 'Content-Type': 'application/json' },
lastCreatedPoll
];
}
);
moment.locale(
application.__container__.lookup('service:i18n').get('locale')
);
2015-06-11 10:53:22 +02:00
},
afterEach() {
server.shutdown();
Ember.run(application, 'destroy');
2015-06-11 10:53:22 +02:00
}
});
test('create a default poll', function(assert) {
2015-06-11 10:53:22 +02:00
visit('/create').then(function() {
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.meta');
fillIn('.title input', 'default poll');
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.options');
let dates =
[
moment().add(1, 'day'),
moment().add(1, 'week')
];
selectDates(
'#datepicker .ember-view',
dates
);
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'create.options-datetime');
click('button[type="submit"]');
andThen(() => {
assert.equal(currentPath(), 'create.settings');
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'poll.participation');
pollHasValidURL(assert);
pollTitleEqual(assert, 'default poll');
pollDescriptionEqual(assert, '');
pollHasOptions(
assert,
dates.map((date) => {
return date.format(
moment.localeData().longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim()
);
})
);
pollHasAnswers(assert, [
t('answerTypes.yes.label'),
t('answerTypes.no.label')
]);
pollHasUsersCount(assert, 0);
});
2015-06-11 10:53:22 +02:00
});
});
});
});
});
});
test('create a poll for answering a question', function(assert) {
2015-06-11 10:53:22 +02:00
visit('/create').then(function() {
// select poll type answer a question
fillIn('.poll-type select', 'MakeAPoll');
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.meta');
fillIn('.title input', 'default poll');
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.options');
expectComponent('create-options-text');
assert.equal(
find('input').length,
2,
'there are two input fields as default'
);
2015-06-11 10:53:22 +02:00
// fill in default two option input fields
fillIn(find('input')[0], 'option a');
fillIn(find('input')[1], 'option c');
2015-06-11 10:53:22 +02:00
// add another option input field
click('button.add', find('.form-group')[0]);
andThen(function() {
assert.equal(
find('input').length,
3,
'option was added'
);
fillIn(find('input')[1], 'option b');
click('button.add', find('.form-group')[2]);
andThen(function() {
assert.equal(
find('input').length,
4,
'option was added'
);
fillIn(find('input')[3], 'to be deleted');
click('button.delete', find('.form-group')[3]);
andThen(function() {
assert.equal(
find('input').length,
3,
'option got deleted'
);
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'create.settings');
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'poll.participation');
pollHasValidURL(assert);
pollTitleEqual(assert, 'default poll');
pollDescriptionEqual(assert, '');
pollHasOptions(assert, ['option a', 'option b', 'option c']);
pollHasUsersCount(assert, 0);
});
});
2015-06-11 10:53:22 +02:00
});
});
});
});
});
});
});
test('create a poll with description', function(assert) {
let dates =
[
moment().add(1, 'day'),
moment().add(1, 'week')
];
let formattedDates =
dates.map((date) => {
return date.format(
moment.localeData().longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim()
);
});
2015-06-11 10:53:22 +02:00
visit('/create').then(function() {
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.meta');
fillIn('.title input', 'default poll');
fillIn('.description textarea', 'a sample description');
click('button[type="submit"]');
andThen(function() {
2015-06-11 10:53:22 +02:00
assert.equal(currentPath(), 'create.options');
selectDates('#datepicker .ember-view', dates);
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'create.options-datetime');
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'create.settings');
click('button[type="submit"]');
andThen(function() {
assert.equal(currentPath(), 'poll.participation');
pollHasValidURL(assert);
pollTitleEqual(assert, 'default poll');
pollDescriptionEqual(assert, 'a sample description');
pollHasOptions(assert, formattedDates);
pollHasUsersCount(assert, 0);
});
2015-06-11 10:53:22 +02:00
});
});
});
});
});
});