2016-01-19 04:56:51 +01:00
|
|
|
import { moduleForComponent, test } from 'ember-qunit';
|
2017-07-29 15:00:11 +02:00
|
|
|
import { blur, fillIn, findAll, focus } from 'ember-native-dom-helpers';
|
2016-01-19 04:56:51 +01:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2017-07-29 15:00:11 +02:00
|
|
|
import hasComponent from 'croodle/tests/helpers/201-created/raw/has-component';
|
2016-02-08 23:46:30 +01:00
|
|
|
import Ember from 'ember';
|
2016-01-19 04:56:51 +01:00
|
|
|
|
|
|
|
moduleForComponent('create-options', 'Integration | Component | create options', {
|
2016-02-09 01:16:18 +01:00
|
|
|
integration: true,
|
|
|
|
beforeEach() {
|
|
|
|
this.inject.service('store');
|
|
|
|
}
|
2016-01-19 04:56:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('renders component', function(assert) {
|
|
|
|
this.set('options', []);
|
|
|
|
this.set('isDateTime', false);
|
|
|
|
this.set('isFindADate', true);
|
|
|
|
this.set('isMakeAPoll', false);
|
|
|
|
this.render(hbs`{{create-options options=options isDateTime=isDateTime isFindADate=isFindADate isMakeAPoll=isMakeAPoll}}`);
|
|
|
|
|
|
|
|
assert.ok(
|
2017-07-29 15:00:11 +02:00
|
|
|
hasComponent(this.container, assert, 'create-options-dates').ok
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
|
|
|
assert.notOk(
|
2017-07-29 15:00:11 +02:00
|
|
|
hasComponent(this.container, assert, 'create-options-text').ok
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.set('isDateTime', false);
|
|
|
|
this.set('isFindADate', false);
|
|
|
|
this.set('isMakeAPoll', true);
|
|
|
|
|
|
|
|
assert.notOk(
|
2017-07-29 15:00:11 +02:00
|
|
|
hasComponent(this.container, assert, 'create-options-dates').ok
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
|
|
|
assert.ok(
|
2017-07-29 15:00:11 +02:00
|
|
|
hasComponent(this.container, assert, 'create-options-text').ok
|
2016-01-19 04:56:51 +01:00
|
|
|
);
|
|
|
|
});
|
2016-02-08 23:46:30 +01:00
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
test('shows validation errors if options are not unique (makeAPoll)', async function(assert) {
|
2016-05-20 21:49:29 +02:00
|
|
|
assert.expect(5);
|
2016-02-09 01:16:18 +01:00
|
|
|
|
2016-02-08 23:46:30 +01:00
|
|
|
this.set('isDateTime', false);
|
|
|
|
this.set('isFindADate', false);
|
|
|
|
this.set('isMakeAPoll', true);
|
2016-02-09 01:16:18 +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;
|
|
|
|
Ember.run(() => {
|
|
|
|
poll = this.store.createRecord('poll', {
|
|
|
|
isFindADate: this.get('isFindADate'),
|
|
|
|
isDateTime: this.get('isDateTime'),
|
|
|
|
isMakeAPoll: this.get('isMakeAPoll')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.set('options', poll.get('options'));
|
|
|
|
|
2016-02-08 23:46:30 +01:00
|
|
|
this.render(hbs`{{create-options options=options isDateTime=isDateTime isFindADate=isFindADate isMakeAPoll=isMakeAPoll}}`);
|
|
|
|
|
2016-05-20 21:49:29 +02:00
|
|
|
assert.ok(
|
|
|
|
this.$('input').length === 2,
|
|
|
|
'assumptions are correct'
|
|
|
|
);
|
2016-02-08 23:46:30 +01:00
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
await fillIn(findAll('input')[0], 'foo');
|
|
|
|
await blur(findAll('input')[0]);
|
|
|
|
await fillIn(findAll('input')[1], 'foo');
|
|
|
|
await blur(findAll('input')[1]);
|
2016-05-20 21:49:29 +02:00
|
|
|
assert.ok(
|
|
|
|
this.$('.form-group').eq(1).hasClass('has-error'),
|
|
|
|
'second input field has validation error'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.$('.form-group').eq(1).find('.help-block').length === 1,
|
|
|
|
'validation error is shown'
|
2016-02-08 23:46:30 +01:00
|
|
|
);
|
2016-02-09 01:16:18 +01:00
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
await fillIn(findAll('input')[0], 'bar');
|
|
|
|
await blur(findAll('input')[0]);
|
2016-05-20 21:49:29 +02:00
|
|
|
assert.ok(
|
|
|
|
this.$('.form-group .help-block').length === 0,
|
|
|
|
'there is no validation error anymore after a unique value is entered'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.$('.form-group.has-error').length === 0,
|
|
|
|
'has-error classes are removed'
|
2016-02-09 01:16:18 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
test('shows validation errors if option is empty (makeAPoll)', async function(assert) {
|
2016-02-09 01:16:18 +01:00
|
|
|
this.set('isDateTime', false);
|
|
|
|
this.set('isFindADate', false);
|
|
|
|
this.set('isMakeAPoll', true);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
Ember.run(() => {
|
|
|
|
poll = this.store.createRecord('poll', {
|
|
|
|
isFindADate: this.get('isFindADate'),
|
|
|
|
isDateTime: this.get('isDateTime'),
|
|
|
|
isMakeAPoll: this.get('isMakeAPoll')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.set('options', poll.get('options'));
|
|
|
|
|
|
|
|
this.render(hbs`{{create-options options=options isDateTime=isDateTime isFindADate=isFindADate isMakeAPoll=isMakeAPoll}}`);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
this.$('.form-group.has-error').length, 0
|
|
|
|
);
|
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
await focus(findAll('input')[0]);
|
|
|
|
await blur(findAll('input')[0]);
|
|
|
|
await focus(findAll('input')[1]);
|
|
|
|
await blur(findAll('input')[1]);
|
2016-02-09 01:16:18 +01:00
|
|
|
assert.equal(
|
|
|
|
this.$('.form-group.has-error').length, 2
|
|
|
|
);
|
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
await fillIn(findAll('input')[0], 'foo');
|
|
|
|
await blur(findAll('input')[0]);
|
2016-02-09 01:16:18 +01:00
|
|
|
assert.equal(
|
|
|
|
this.$('.form-group.has-error').length, 1
|
|
|
|
);
|
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
await fillIn(findAll('input')[1], 'bar');
|
|
|
|
await blur(findAll('input')[1]);
|
|
|
|
assert.equal(
|
|
|
|
this.$('.form-group.has-error').length, 0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('label reflects validation state of all inputs (makeAPoll)', async function(assert) {
|
|
|
|
this.set('isDateTime', false);
|
|
|
|
this.set('isFindADate', false);
|
|
|
|
this.set('isMakeAPoll', true);
|
|
|
|
|
|
|
|
// 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;
|
2016-02-09 01:16:18 +01:00
|
|
|
Ember.run(() => {
|
2017-07-29 15:00:11 +02:00
|
|
|
poll = this.store.createRecord('poll', {
|
|
|
|
isFindADate: this.get('isFindADate'),
|
|
|
|
isDateTime: this.get('isDateTime'),
|
|
|
|
isMakeAPoll: this.get('isMakeAPoll')
|
|
|
|
});
|
2016-02-09 01:16:18 +01:00
|
|
|
});
|
2017-07-29 15:00:11 +02:00
|
|
|
this.set('options', poll.get('options'));
|
2016-02-09 01:16:18 +01:00
|
|
|
|
2017-07-29 15:00:11 +02:00
|
|
|
this.render(hbs`{{create-options options=options isDateTime=isDateTime isFindADate=isFindADate isMakeAPoll=isMakeAPoll}}`);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.$('form').children().hasClass('label-has-no-validation'),
|
|
|
|
'does not show validation state if there wasn\'t any user interaction yet'
|
|
|
|
);
|
|
|
|
|
|
|
|
await focus(findAll('input')[0]);
|
|
|
|
await blur(findAll('input')[0]);
|
|
|
|
assert.ok(
|
|
|
|
this.$('form').children().hasClass('label-has-error'),
|
|
|
|
'shows as having error if atleast on field has an error'
|
|
|
|
);
|
|
|
|
|
|
|
|
await fillIn(findAll('input')[0], 'foo');
|
|
|
|
await blur(findAll('input')[0]);
|
|
|
|
assert.ok(
|
|
|
|
this.$('form').children().hasClass('label-has-no-validation'),
|
|
|
|
'does not show validation state if no field has error but not all fields are showing error yet'
|
|
|
|
);
|
|
|
|
|
|
|
|
await fillIn(findAll('input')[1], 'bar');
|
|
|
|
await blur(findAll('input')[1]);
|
|
|
|
assert.ok(
|
|
|
|
this.$('form').children().hasClass('label-has-success'),
|
|
|
|
'shows as having success if all fields are showing success'
|
2016-02-09 01:16:18 +01:00
|
|
|
);
|
2016-02-08 23:46:30 +01:00
|
|
|
});
|