decide.nolog.cz/tests/unit/components/create-options-test.js

184 lines
5.7 KiB
JavaScript
Raw Normal View History

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';
import moment from 'moment';
2018-12-29 20:35:04 +01:00
module('Unit | Component | create options', function(hooks) {
setupTest(hooks);
hooks.beforeEach(function() {
this.store = this.owner.lookup('service:store');
});
2018-12-29 20:35:04 +01:00
test('validation for make a poll', function(assert) {
let component = this.owner.factoryFor('component:create-options').create();
component.set('options', []);
component.set('isFindADate', false);
component.set('isMakeAPoll', true);
2018-12-29 20:35:04 +01:00
// 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
let poll;
run(() => {
poll = this.store.createRecord('poll', {
isFindADate: component.get('isFindADate'),
isMakeAPoll: component.get('isMakeAPoll')
});
});
2018-12-29 20:35:04 +01:00
assert.notOk(
component.get('validations.isValid'),
'invalid without any options'
);
run(() => {
let option = this.store.createFragment('option', {
title: 'first option'
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.ok(
component.get('validations.isValid'),
'valid if there is atleast one valid option'
);
run(() => {
let option = this.store.createFragment('option', {
title: 'second option'
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.ok(
component.get('validations.isValid'),
'valid for two options which are not empty strings'
);
run(() => {
component.set('options.firstObject.title', '');
});
assert.notOk(
component.get('validations.isValid'),
'invalid if atleast one string is empty'
);
});
2018-12-29 20:35:04 +01:00
test('validation for find a date without times', function(assert) {
let component = this.owner.factoryFor('component:create-options').create();
component.set('options', []);
component.set('isFindADate', true);
component.set('isMakeAPoll', false);
2018-12-29 20:35:04 +01:00
// 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
let poll;
run(() => {
poll = this.store.createRecord('poll', {
isFindADate: component.get('isFindADate'),
isMakeAPoll: component.get('isMakeAPoll')
});
});
2018-12-29 20:35:04 +01:00
assert.notOk(
component.get('validations.isValid'),
'invalid without any options'
);
run(() => {
let option = this.store.createFragment('option', {
title: '2015-01-01'
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.ok(
component.get('validations.isValid'),
'valid if there is atleast one valid date'
);
run(() => {
let option = this.store.createFragment('option', {
title: '2015-01-02'
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.ok(
component.get('validations.isValid'),
'valid for two valid dates'
);
run(() => {
let option = this.store.createFragment('option', {
title: 'foo'
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.notOk(
component.get('validations.isValid'),
'invalid if atleast one option is not a valid date'
);
run(() => {
component.set('options.lastObject.title', '2015-01-03');
});
assert.ok(
component.get('validations.isValid'),
'valid again after title is a valid date again'
);
run(() => {
component.set('options.firstObject.title', '2015-01-01');
component.set('options.lastObject.title', '2015-01-01');
});
assert.ok(
component.get('validations.isInvalid'),
'invalid if dates are not unique'
);
2016-02-08 23:46:30 +01:00
});
2016-04-09 01:52:54 +02:00
2018-12-29 20:35:04 +01:00
test('validation for find a date with times', function(assert) {
let component = this.owner.factoryFor('component:create-options').create();
component.set('options', []);
component.set('isMakeAPoll', false);
2016-04-09 01:52:54 +02:00
2018-12-29 20:35:04 +01:00
// 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
let poll;
run(() => {
poll = this.store.createRecord('poll', {
isFindADate: component.get('isFindADate'),
isMakeAPoll: component.get('isMakeAPoll')
});
2016-04-09 01:52:54 +02:00
});
2018-12-29 20:35:04 +01:00
assert.notOk(
component.get('validations.isValid'),
'invalid without any options'
);
run(() => {
let option = this.store.createFragment('option', {
title: moment().add('1', 'day').format('YYYY-MM-DD')
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
2016-04-09 01:52:54 +02:00
});
2018-12-29 20:35:04 +01:00
assert.ok(
component.get('validations.isValid'),
'valid if there is atleast one valid date'
);
/*
Ember.run(() => {
let option = this.store.createFragment('option', {
title: moment().add('1', 'day').hour(22).minute(30).seconds(0).milliseconds(0).toISOString()
});
poll.get('options').pushObject(option);
component.get('options').pushObject(option);
});
2018-12-29 20:35:04 +01:00
assert.notOk(
component.get('validations.isValid'),
'invalid if there is a option without time for a day with has another option with time specified'
);
*/
});
2016-04-09 01:52:54 +02:00
});