2018-12-29 01:27:37 +01:00
|
|
|
import { alias } from '@ember/object/computed';
|
|
|
|
import { run } from '@ember/runloop';
|
2018-12-29 20:35:04 +01:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import {
|
|
|
|
render,
|
|
|
|
click,
|
|
|
|
fillIn,
|
|
|
|
find,
|
|
|
|
findAll,
|
|
|
|
triggerEvent
|
|
|
|
} from '@ember/test-helpers';
|
2016-01-20 02:52:21 +01:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2017-07-29 15:00:11 +02:00
|
|
|
import jQuery from 'jquery';
|
2016-01-22 00:19:46 +01:00
|
|
|
import moment from 'moment';
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
module('Integration | Component | create options datetime', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
2016-01-29 12:54:54 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
hooks.beforeEach(function() {
|
|
|
|
this.store = this.owner.lookup('service:store');
|
2016-06-06 00:37:26 +02:00
|
|
|
});
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
/*
|
|
|
|
* watch out:
|
|
|
|
* polyfill adds another input[type="text"] for every input[type="time"]
|
|
|
|
* if browser doesn't support input[type="time"]
|
|
|
|
* that ones could be identifed by class 'ws-inputreplace'
|
|
|
|
*/
|
2016-01-22 00:19:46 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('it generates inpute field for options iso 8601 date string (without time)', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false,
|
|
|
|
options: [
|
|
|
|
{ title: '2015-01-01' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group input').length,
|
|
|
|
1,
|
|
|
|
'there is one input field'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
find('.days .form-group input').value,
|
|
|
|
'',
|
|
|
|
'value is an empty string'
|
|
|
|
);
|
2016-06-06 00:37:26 +02:00
|
|
|
});
|
2016-01-22 00:19:46 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('it generates inpute field for options iso 8601 datetime string (with time)', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false,
|
|
|
|
options: [
|
|
|
|
{ title: '2015-01-01T11:11:00.000Z' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
2016-01-22 00:19:46 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group input').length,
|
|
|
|
1,
|
|
|
|
'there is one input field'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
find('.days .form-group input').value,
|
|
|
|
moment('2015-01-01T11:11:00.000Z').format('HH:mm'),
|
|
|
|
'it has time in option as value'
|
|
|
|
);
|
2016-06-06 00:37:26 +02:00
|
|
|
});
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('it hides repeated labels', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false,
|
|
|
|
options: [
|
|
|
|
{ title: moment('2015-01-01T10:11').toISOString() },
|
|
|
|
{ title: moment('2015-01-01T22:22').toISOString() },
|
|
|
|
{ title: '2015-02-02' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
2016-01-22 00:19:46 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
findAll('.days label').length,
|
|
|
|
3,
|
|
|
|
'every form-group has a label'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days label:not(.sr-only)').length,
|
|
|
|
2,
|
|
|
|
'there are two not hidden labels for two different dates'
|
|
|
|
);
|
|
|
|
assert.notOk(
|
|
|
|
this.$('.days .form-group').eq(0).find('label').hasClass('sr-only'),
|
|
|
|
'the first label is shown'
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
this.$('.days .form-group').eq(1).find('label').hasClass('sr-only'),
|
|
|
|
'the repeated label on second form-group is hidden by sr-only class'
|
|
|
|
);
|
|
|
|
assert.notOk(
|
|
|
|
this.$('.days .form-group').eq(2).find('label').hasClass('sr-only'),
|
|
|
|
'the new label on third form-group is shown'
|
|
|
|
);
|
2016-05-20 21:49:29 +02:00
|
|
|
});
|
2016-01-22 00:19:46 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('allows to add another option', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: '2015-01-01' },
|
|
|
|
{ title: '2015-02-02' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group input').length,
|
|
|
|
2,
|
|
|
|
'there are two input fields before'
|
|
|
|
);
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
await click(findAll('.days .form-group')[0].querySelector('.add'));
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group input').length,
|
|
|
|
3,
|
|
|
|
'another input field is added'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
this.$('.days .form-group').eq(1).find('label').text(),
|
|
|
|
this.$('.days .form-group').eq(0).find('label').text(),
|
|
|
|
'new input has correct label'
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
this.$('.days .form-group').eq(1).find('label').hasClass('sr-only'),
|
|
|
|
'label ofnew input is hidden cause it\'s repeated'
|
|
|
|
);
|
2016-06-06 00:37:26 +02:00
|
|
|
});
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('allows to delete an option', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false,
|
|
|
|
options: [
|
|
|
|
{ title: moment('2015-01-01T11:11').toISOString() },
|
|
|
|
{ title: moment('2015-01-01T22:22').toISOString() }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
|
2016-01-22 00:19:46 +01:00
|
|
|
assert.equal(
|
2018-12-29 20:35:04 +01:00
|
|
|
findAll('.days input').length,
|
|
|
|
2,
|
|
|
|
'there are two input fields before'
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
findAll('.delete').every((el) => el.disabled === false),
|
|
|
|
'options are deleteable'
|
|
|
|
);
|
|
|
|
|
|
|
|
await click(findAll('.days .form-group')[0].querySelector('.delete'));
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group input').length,
|
2016-01-22 00:19:46 +01:00
|
|
|
1,
|
|
|
|
'one input field is removed after deletion'
|
|
|
|
);
|
|
|
|
assert.equal(
|
2018-12-29 20:35:04 +01:00
|
|
|
find('.days .form-group input').value,
|
2016-01-22 00:19:46 +01:00
|
|
|
'22:22',
|
|
|
|
'correct input field is deleted'
|
|
|
|
);
|
|
|
|
assert.equal(
|
2016-06-08 13:55:00 +02:00
|
|
|
this.get('poll.options.length'),
|
2016-01-22 00:19:46 +01:00
|
|
|
1,
|
|
|
|
'is also delete from option'
|
|
|
|
);
|
|
|
|
assert.equal(
|
2016-06-08 13:55:00 +02:00
|
|
|
this.get('poll.options.firstObject.title'),
|
2016-01-22 00:19:46 +01:00
|
|
|
moment('2015-01-01T22:22').toISOString(),
|
|
|
|
'correct option is deleted'
|
|
|
|
);
|
|
|
|
});
|
2016-02-16 04:02:59 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('adopt times of first day - simple', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: moment().hour(10).minute(0).toISOString() },
|
|
|
|
{ title: '2015-02-02' },
|
|
|
|
{ title: '2015-03-03' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
await click('button.adopt-times-of-first-day');
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group')[0].querySelector('input').value,
|
|
|
|
'10:00',
|
|
|
|
'time was not changed for first day'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group')[1].querySelector('input').value,
|
|
|
|
'10:00',
|
|
|
|
'time was adopted for second day'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group')[2].querySelector('input').value,
|
|
|
|
'10:00',
|
|
|
|
'time was adopted for third day'
|
|
|
|
);
|
2016-02-16 04:02:59 +01:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('adopt times of first day - more times on first day than on others', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: moment().hour(10).minute(0).toISOString() },
|
|
|
|
{ title: moment().hour(22).minute(0).toISOString() },
|
|
|
|
{ title: '2015-02-02' },
|
|
|
|
{ title: '2015-03-03' }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
await click('button.adopt-times-of-first-day');
|
|
|
|
assert.deepEqual(
|
|
|
|
this.$('.days .form-group input').map((i, el) => jQuery(el).val()).toArray(),
|
|
|
|
['10:00', '22:00', '10:00', '22:00', '10:00', '22:00'],
|
|
|
|
'times were adopted correctly'
|
|
|
|
);
|
2016-02-16 04:02:59 +01:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('adopt times of first day - excess times on other days got deleted', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false,
|
|
|
|
options: [
|
|
|
|
{ title: moment().hour(10).minute(0).toISOString() },
|
|
|
|
{ title: moment().add(1, 'day').hour(10).minute(0).toISOString() },
|
|
|
|
{ title: moment().add(1, 'day').hour(22).minute(0).toISOString() }
|
|
|
|
]
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=poll.options}}`);
|
|
|
|
await click('button.adopt-times-of-first-day');
|
|
|
|
assert.equal(
|
|
|
|
findAll('.days .form-group').length,
|
|
|
|
2,
|
|
|
|
'one excess time input got deleted'
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
this.$('.days .form-group input').map((i, el) => jQuery(el).val()).toArray(),
|
|
|
|
['10:00', '10:00'],
|
|
|
|
'additional time on secondary day got deleted'
|
|
|
|
);
|
2016-02-16 04:02:59 +01:00
|
|
|
});
|
2016-06-06 00:37:26 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('validation', async function(assert) {
|
|
|
|
// validation is based on validation of every option fragment
|
|
|
|
// which validates according to poll model it belongs to
|
|
|
|
// therefore each option needs to be pushed to poll model to have it as
|
|
|
|
// it's owner
|
|
|
|
run(() => {
|
|
|
|
this.set('poll', this.store.createRecord('poll', {
|
|
|
|
isFindADate: true,
|
|
|
|
isMakeAPoll: false
|
|
|
|
}));
|
|
|
|
this.set('options', alias('poll.options'));
|
|
|
|
this.get('options').pushObjects([
|
|
|
|
{ title: '2015-01-01' },
|
|
|
|
{ title: '2015-02-02' }
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
await render(hbs`{{create-options-datetime dates=options}}`);
|
|
|
|
assert.ok(
|
2019-01-03 00:05:21 +01:00
|
|
|
findAll('.has-error').length === 0 && findAll('.has-success').length === 0,
|
2018-12-29 20:35:04 +01:00
|
|
|
'does not show a validation error before user interaction'
|
|
|
|
);
|
|
|
|
|
2019-01-03 00:05:21 +01:00
|
|
|
await fillIn('[data-test-day="2015-01-01"] .form-group input', '10:');
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.ok(
|
2019-01-03 00:05:21 +01:00
|
|
|
find('[data-test-day="2015-01-01"] .form-group').classList.contains('has-error') ||
|
2018-12-29 20:35:04 +01:00
|
|
|
// browsers with input type time support prevent non time input
|
2019-01-03 00:05:21 +01:00
|
|
|
find('[data-test-day="2015-01-01"] .form-group input').value === '',
|
2018-12-29 20:35:04 +01:00
|
|
|
'shows error after invalid input or prevents invalid input'
|
|
|
|
);
|
|
|
|
|
|
|
|
// simulate unique violation
|
2019-01-03 00:05:21 +01:00
|
|
|
await click('[data-test-day="2015-01-01"] .add');
|
|
|
|
await fillIn(findAll('[data-test-day="2015-01-01"]')[0].querySelector('input'), '10:00');
|
|
|
|
await fillIn(findAll('[data-test-day="2015-01-01"]')[1].querySelector('input'), '10:00');
|
|
|
|
await fillIn('[data-test-day="2015-02-02"] .form-group input', '10:00');
|
2018-12-29 20:35:04 +01:00
|
|
|
await triggerEvent('form', 'submit');
|
2019-01-03 00:05:21 +01:00
|
|
|
assert.dom(findAll('[data-test-day="2015-01-01"]')[0].querySelector('.form-group')).hasClass('has-success',
|
2018-12-29 20:35:04 +01:00
|
|
|
'first time shows validation success'
|
|
|
|
);
|
2019-01-03 00:05:21 +01:00
|
|
|
assert.dom(findAll('[data-test-day="2015-01-01"]')[1].querySelector('.form-group')).hasClass('has-error',
|
2018-12-29 20:35:04 +01:00
|
|
|
'same time for same day shows validation error'
|
|
|
|
);
|
2019-01-03 00:05:21 +01:00
|
|
|
assert.dom('[data-test-day="2015-02-02"] .form-group').hasClass('has-success',
|
2018-12-29 20:35:04 +01:00
|
|
|
'same time for different day shows validation success'
|
|
|
|
);
|
2019-01-03 00:05:21 +01:00
|
|
|
|
|
|
|
// label reflects validation state for all times of this day
|
|
|
|
assert.dom(find('[data-test-day="2015-01-01"]')).hasClass('label-has-error',
|
|
|
|
'label reflects validation state for all times (error)'
|
|
|
|
);
|
|
|
|
assert.dom('[data-test-day="2015-02-02"]').hasClass('label-has-success',
|
|
|
|
'label reflects validation state for all times (success)'
|
|
|
|
);
|
2016-06-06 00:37:26 +02:00
|
|
|
});
|
|
|
|
});
|