2018-12-29 01:27:37 +01:00
|
|
|
import { run } from '@ember/runloop';
|
2018-12-29 20:35:04 +01:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
2016-01-20 02:52:21 +01:00
|
|
|
import moment from 'moment';
|
2019-02-23 23:13:04 +01:00
|
|
|
import { settled } from '@ember/test-helpers';
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
module('Unit | Component | create options datetime', function(hooks) {
|
|
|
|
setupTest(hooks);
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
hooks.beforeEach(function() {
|
|
|
|
this.store = this.owner.lookup('service:store');
|
2016-06-10 20:59:02 +02:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('delete a date', function(assert) {
|
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create();
|
|
|
|
let a, b, c, d, e;
|
|
|
|
run(() => {
|
|
|
|
a = this.store.createFragment('option', { title: moment('2015-01-01T01:01:00.000').toISOString() });
|
|
|
|
b = this.store.createFragment('option', { title: moment('2015-01-01T11:11:00.000').toISOString() });
|
|
|
|
c = this.store.createFragment('option', { title: moment('2015-02-02T11:11:00.000').toISOString() });
|
|
|
|
d = this.store.createFragment('option', { title: '2015-02-02' });
|
|
|
|
e = this.store.createFragment('option', { title: '2015-02-03' });
|
|
|
|
component.set('dates', [a, b, c, d, e]);
|
|
|
|
});
|
|
|
|
component.send('deleteOption', b);
|
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((date) => date.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T01:01:00.000').toISOString(),
|
|
|
|
moment('2015-02-02T11:11:00.000').toISOString(),
|
|
|
|
'2015-02-02',
|
|
|
|
'2015-02-03'
|
|
|
|
],
|
|
|
|
'date get deleted if there is another date with same day (both having a time)'
|
|
|
|
);
|
|
|
|
component.send('deleteOption', d);
|
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((date) => date.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T01:01:00.000').toISOString(),
|
|
|
|
moment('2015-02-02T11:11:00.000').toISOString(),
|
|
|
|
'2015-02-03'
|
|
|
|
],
|
|
|
|
'date get deleted if there is another date with same day (date does not have a time)'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
component.send('deleteOption', c);
|
|
|
|
});
|
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((date) => date.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T01:01:00.000').toISOString(),
|
|
|
|
'2015-02-02',
|
|
|
|
'2015-02-03'
|
|
|
|
],
|
|
|
|
'time is removed from date if there isn\' t any other date with same day'
|
|
|
|
);
|
|
|
|
component.send('deleteOption', d);
|
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((date) => date.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T01:01:00.000').toISOString(),
|
|
|
|
'2015-02-02',
|
|
|
|
'2015-02-03'
|
|
|
|
],
|
|
|
|
'nothing changes if it\'s the only date for this day it it doesn\'t have a time'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
2016-01-20 02:52:21 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('datetimes are grouped by date', function(assert) {
|
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create();
|
|
|
|
let a, b, c;
|
|
|
|
// have to set dates in local time and than convert to ISO 8601 strings
|
|
|
|
// because otherwise test could fail caused by timezone
|
|
|
|
run(() => {
|
|
|
|
a = this.store.createFragment('option', { title: moment('2015-01-01T01:01:00').toISOString() });
|
|
|
|
b = this.store.createFragment('option', { title: moment('2015-01-01T11:11:00').toISOString() });
|
|
|
|
c = this.store.createFragment('option', { title: moment('2015-02-02T01:01:00').toISOString() });
|
|
|
|
component.set('dates', [a, b, c]);
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.length'),
|
|
|
|
2,
|
|
|
|
'length is correct'
|
2016-06-08 13:55:00 +02:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
component.get('groupedDates.firstObject.items').map((item) => {
|
|
|
|
return item.get('date').toISOString();
|
|
|
|
}),
|
|
|
|
[a.get('title'), b.get('title')],
|
|
|
|
'first dates having same day are grouped together'
|
2016-06-08 13:55:00 +02:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.lastObject.items.firstObject.date').toISOString(),
|
|
|
|
[c.get('title')],
|
|
|
|
'last date having another day is in a separate group'
|
2016-06-08 13:55:00 +02:00
|
|
|
);
|
|
|
|
});
|
2016-03-21 22:06:08 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('bindings are working on grouped datetimes', function(assert) {
|
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create();
|
2019-03-14 21:30:48 +01:00
|
|
|
let baseDate = moment('2015-01-01T11:11');
|
|
|
|
|
|
|
|
component.set('dates', [
|
|
|
|
this.store.createFragment('option', {
|
|
|
|
title: baseDate.toISOString()
|
|
|
|
})
|
|
|
|
]);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.firstObject.items.firstObject.time'),
|
2019-03-14 21:30:48 +01:00
|
|
|
baseDate.format('HH:mm'),
|
2018-12-29 20:35:04 +01:00
|
|
|
'time is correct before'
|
|
|
|
);
|
2019-03-14 21:30:48 +01:00
|
|
|
|
|
|
|
component.set(
|
|
|
|
'groupedDates.firstObject.items.firstObject.time',
|
|
|
|
'00:00'
|
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
component.get('dates.firstObject.title'),
|
2019-03-14 21:30:48 +01:00
|
|
|
moment(baseDate).hour(0).minute(0).toISOString(),
|
2018-12-29 20:35:04 +01:00
|
|
|
'option is updated after time changed on grouped datetimes'
|
|
|
|
);
|
2019-03-14 21:30:48 +01:00
|
|
|
|
|
|
|
component.get('dates').pushObject(
|
|
|
|
this.store.createFragment('option', {
|
|
|
|
title: baseDate.add(1, 'hour').add(1, 'minute').toISOString(),
|
|
|
|
})
|
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.firstObject.items.length'),
|
|
|
|
2,
|
|
|
|
'grouped datetimes got updated after option was added (same day)'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.firstObject.items.lastObject.time'),
|
|
|
|
'12:12',
|
|
|
|
'grouped datetimes got updated correctly after option was added (same day)'
|
|
|
|
);
|
2019-03-14 21:30:48 +01:00
|
|
|
|
|
|
|
component.get('dates').pushObject(
|
|
|
|
this.store.createFragment('option', { title: moment('2015-02-02T01:01').toISOString() })
|
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.length'),
|
|
|
|
2,
|
|
|
|
'grouped datetimes got updated after option was added (other day)'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
component.get('groupedDates.lastObject.items.firstObject.time'),
|
|
|
|
'01:01',
|
|
|
|
'grouped datetimes got updated correctly after option was added (same day)'
|
|
|
|
);
|
2016-03-21 22:06:08 +01:00
|
|
|
});
|
|
|
|
|
2019-02-23 23:13:04 +01:00
|
|
|
test('adopt times of first day - simple', async function(assert) {
|
|
|
|
let poll = this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: moment('2015-01-01T11:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-01T22:22:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-02').toISOString() },
|
|
|
|
{ title: moment('2015-01-03').toISOString() }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create({
|
|
|
|
dates: poll.get('options')
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
2019-02-23 23:13:04 +01:00
|
|
|
component.send('adoptTimesOfFirstDay');
|
|
|
|
await settled();
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((option) => option.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-01T22:22:00.000').toISOString(),
|
|
|
|
moment('2015-01-02T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-02T22:22:00.000').toISOString(),
|
|
|
|
moment('2015-01-03T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-03T22:22:00.000').toISOString()
|
|
|
|
],
|
|
|
|
'times adopted correctly'
|
|
|
|
);
|
2016-03-21 22:06:08 +01:00
|
|
|
});
|
2016-06-09 12:27:51 +02:00
|
|
|
|
2019-02-23 23:13:04 +01:00
|
|
|
test('adopt times of first day - having times on the other days', async function(assert) {
|
|
|
|
let poll = this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: moment('2015-01-01T11:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-01T22:22:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-02T09:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-03T01:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-03T11:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-04T02:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-04T05:11:00.000').toISOString() },
|
|
|
|
{ title: moment('2015-01-04T12:11:00.000').toISOString() }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create({
|
|
|
|
dates: poll.get('options')
|
2016-06-09 12:27:51 +02:00
|
|
|
});
|
2019-02-23 23:13:04 +01:00
|
|
|
component.send('adoptTimesOfFirstDay');
|
|
|
|
await settled();
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((option) => option.get('title')),
|
|
|
|
[
|
|
|
|
moment('2015-01-01T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-01T22:22:00.000').toISOString(),
|
|
|
|
moment('2015-01-02T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-02T22:22:00.000').toISOString(),
|
|
|
|
moment('2015-01-03T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-03T22:22:00.000').toISOString(),
|
|
|
|
moment('2015-01-04T11:11:00.000').toISOString(),
|
|
|
|
moment('2015-01-04T22:22:00.000').toISOString()
|
|
|
|
],
|
|
|
|
'times adopted correctly'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-02-23 23:13:04 +01:00
|
|
|
test('adopt times of first day - no times on first day', async function(assert) {
|
|
|
|
let poll = this.store.createRecord('poll', {
|
|
|
|
options: [
|
|
|
|
{ title: '2015-01-01' },
|
|
|
|
{ title: '2015-01-02' },
|
2019-03-14 21:30:48 +01:00
|
|
|
{ title: moment('2015-01-03T11:00').toISOString() },
|
|
|
|
{ title: moment('2015-01-03T15:00').toISOString() }
|
2019-02-23 23:13:04 +01:00
|
|
|
]
|
2016-07-05 11:26:03 +02:00
|
|
|
});
|
2019-02-23 23:13:04 +01:00
|
|
|
let component = this.owner.factoryFor('component:create-options-datetime').create({
|
|
|
|
dates: poll.get('options')
|
|
|
|
});
|
|
|
|
component.send('adoptTimesOfFirstDay');
|
|
|
|
await settled();
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.deepEqual(
|
|
|
|
component.get('dates').map((option) => option.get('title')),
|
|
|
|
[
|
|
|
|
'2015-01-01',
|
|
|
|
'2015-01-02',
|
|
|
|
'2015-01-03'
|
|
|
|
],
|
|
|
|
'times are removed from all days'
|
|
|
|
);
|
2016-06-09 12:27:51 +02:00
|
|
|
});
|
|
|
|
});
|