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';
|
2020-01-30 00:23:12 +01:00
|
|
|
import { setupIntl } from 'ember-intl/test-support';
|
2016-06-08 13:55:00 +02:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
module('Unit | Model | option', function(hooks) {
|
|
|
|
setupTest(hooks);
|
2020-01-30 00:23:12 +01:00
|
|
|
setupIntl(hooks, 'en');
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
hooks.beforeEach(function() {
|
2016-06-08 13:55:00 +02:00
|
|
|
moment.locale('en');
|
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('date property (get)', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01'
|
|
|
|
}));
|
|
|
|
assert.ok(
|
|
|
|
moment.isMoment(option.get('date')),
|
|
|
|
'returns a moment instance if title is an ISO 8601 day string'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
option.get('date').format('YYYY-MM-DD HH:mm:ss.SSS'),
|
|
|
|
'2015-01-01 00:00:00.000',
|
|
|
|
'string to date conversion is correct for ISO 8601 day string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', '2015-01-01T11:11:00.000Z');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
moment.isMoment(option.get('date')),
|
|
|
|
'returns a moment instance if title is an ISO 8601 datetime string'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
option.get('date').toISOString(),
|
|
|
|
'2015-01-01T11:11:00.000Z',
|
|
|
|
'string to date conversion is correct for ISO 8601 datetime string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', null);
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('date'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is empty'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', 'abc');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('date'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is not a valid ISO 8601 date string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', '2015');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('date'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title ISO 8601 string only contains a year'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', '2015-01');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('date'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title ISO 8601 string only contains a year and a month'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', '2013W06');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('date'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title ISO 8601 string only contains a year and a week'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('day property (get)', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01'
|
|
|
|
}));
|
|
|
|
assert.equal(
|
|
|
|
option.get('day'),
|
|
|
|
'2015-01-01',
|
|
|
|
'returns ISO 8601 day string if title is ISO 8601 day string'
|
|
|
|
);
|
2016-06-20 20:48:48 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', '2015-01-01T11:11:00.000Z');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('day'),
|
|
|
|
moment('2015-01-01T11:11:00.000Z').format('YYYY-MM-DD'),
|
|
|
|
'returns ISO 8601 day string if title is ISO 8601 datetime string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', 'abc');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('day'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is not a valid ISO 8601 string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', null);
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('day'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is null'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('dayFormatted property (get)', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01'
|
|
|
|
}));
|
|
|
|
assert.equal(
|
|
|
|
option.get('dayFormatted'),
|
|
|
|
'Thursday, January 1, 2015',
|
|
|
|
'returns formatted date if title is ISO 8601 day string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', moment('2015-01-01').toISOString());
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('dayFormatted'),
|
|
|
|
'Thursday, January 1, 2015',
|
|
|
|
'returns formatted date if title is ISO 8601 datetime string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
2020-01-30 00:23:12 +01:00
|
|
|
option.set('intl.locale', 'de');
|
2018-12-29 20:35:04 +01:00
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('dayFormatted'),
|
|
|
|
'Donnerstag, 1. Januar 2015',
|
|
|
|
'observes locale changes'
|
|
|
|
);
|
2016-06-10 20:59:02 +02:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', 'abc');
|
|
|
|
});
|
|
|
|
assert.equal(
|
|
|
|
option.get('dayFormatted'),
|
|
|
|
undefined,
|
|
|
|
'returns undfined if title is not a valid ISO 8601 string'
|
|
|
|
);
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('hasTime property', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01T11:11:00.000Z'
|
|
|
|
}));
|
|
|
|
assert.ok(option.get('hasTime'));
|
|
|
|
run(() => {
|
|
|
|
option.set('title', '2015-01-01');
|
|
|
|
});
|
|
|
|
assert.notOk(option.get('hasTime'));
|
|
|
|
run(() => {
|
|
|
|
option.set('title', 'foo');
|
|
|
|
});
|
|
|
|
assert.notOk(option.get('hasTime'));
|
2016-06-08 13:55:00 +02:00
|
|
|
});
|
2016-01-19 04:56:51 +01:00
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('time property (get)', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01T11:11:00.000Z'
|
|
|
|
}));
|
|
|
|
assert.equal(
|
|
|
|
option.get('time'),
|
|
|
|
moment('2015-01-01T11:11:00.000Z').format('HH:mm'),
|
|
|
|
'returns time if title is ISO 8601 datetime string'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
option.set('title', '2015-01-01');
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
option.get('time'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is ISO 8601 day string'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
option.set('title', 'abc');
|
2016-02-13 18:37:33 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
option.get('time'),
|
|
|
|
undefined,
|
|
|
|
'returns undefined if title is not an ISO 8601 date string'
|
2016-02-13 18:37:33 +01:00
|
|
|
);
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
test('time property (set)', function(assert) {
|
|
|
|
let option = run(() => this.owner.lookup('service:store').createRecord('option', {
|
|
|
|
title: '2015-01-01'
|
|
|
|
}));
|
|
|
|
|
|
|
|
run(() => {
|
|
|
|
option.set('time', '11:00');
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
option.get('title'),
|
|
|
|
moment('2015-01-01T11:00').toISOString(),
|
|
|
|
'sets title according to time'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
option.set('time', null);
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
option.get('title'),
|
|
|
|
'2015-01-01',
|
|
|
|
'removes time from option if value is false'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
|
|
|
|
const before = option.get('title');
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
option.set('time', 'abc');
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.equal(
|
|
|
|
option.get('title'),
|
|
|
|
before,
|
|
|
|
'does not set title if time is invalid'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
|
|
|
|
2018-12-29 20:35:04 +01:00
|
|
|
run(() => {
|
|
|
|
option.set('title', 'abc');
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
option.set('time', '11:11');
|
|
|
|
},
|
|
|
|
'throws if attempt to set a time if title is not a date string'
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
2018-12-29 20:35:04 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validation for MakeAPoll', function(assert) {
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
let store = this.owner.lookup('service:store');
|
|
|
|
let poll = store.createRecord('poll', {
|
|
|
|
pollType: 'MakeAPoll'
|
|
|
|
});
|
|
|
|
let option = store.createFragment('option');
|
|
|
|
poll.get('options').pushObject(option);
|
|
|
|
option.validate();
|
|
|
|
assert.notOk(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'default value is not valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', 'Spasibo!');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'is valid for a non empty string'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', '!');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'is invalid if set to empty string again'
|
|
|
|
);
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validation for FindADate', function(assert) {
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
let store = this.owner.lookup('service:store');
|
|
|
|
let poll = store.createRecord('poll', {
|
|
|
|
isDateTime: false,
|
|
|
|
pollType: 'FindADate'
|
|
|
|
});
|
|
|
|
let option = store.createFragment('option');
|
|
|
|
poll.get('options').pushObject(option);
|
|
|
|
option.validate();
|
|
|
|
assert.notOk(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'default value is not valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', '1945-05-08');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'iso 8601 date string is valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', 'Spasibo!');
|
|
|
|
});
|
|
|
|
assert.notOk(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'random string is not valid'
|
|
|
|
);
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
2018-12-29 20:35:04 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validation for FindADate', function(assert) {
|
2018-12-29 01:27:37 +01:00
|
|
|
run(() => {
|
2018-12-29 20:35:04 +01:00
|
|
|
let store = this.owner.lookup('service:store');
|
|
|
|
let poll = store.createRecord('poll', {
|
|
|
|
pollType: 'FindADate'
|
|
|
|
});
|
|
|
|
let option = store.createFragment('option');
|
|
|
|
poll.get('options').pushObject(option);
|
|
|
|
option.validate();
|
|
|
|
assert.notOk(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'default value is not valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', '1945-05-08T00:00:00.000Z');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'iso 8601 datetime string is valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', 'Spasibo!');
|
|
|
|
});
|
|
|
|
assert.notOk(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'random string is not valid'
|
|
|
|
);
|
|
|
|
run(() => {
|
|
|
|
option.set('title', '1945-05-08');
|
|
|
|
});
|
|
|
|
assert.ok(
|
|
|
|
option.get('validations.isValid'),
|
|
|
|
'iso 8601 date string is valid'
|
|
|
|
);
|
2016-02-13 18:37:33 +01:00
|
|
|
});
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
|
|
|
});
|