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

146 lines
4 KiB
JavaScript
Raw Normal View History

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import expectComponent from 'croodle/tests/helpers/201-created/raw/expect-component';
2016-02-08 23:46:30 +01:00
import Ember from 'ember';
moduleForComponent('create-options', 'Integration | Component | create options', {
2016-02-09 01:16:18 +01:00
integration: true,
beforeEach() {
this.inject.service('store');
}
});
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(
expectComponent(this.container, 'create-options-dates', 1).ok
);
assert.notOk(
expectComponent(this.container, 'create-options-text', 0).ok
);
this.set('isDateTime', false);
this.set('isFindADate', false);
this.set('isMakeAPoll', true);
assert.notOk(
expectComponent(this.container, 'create-options-dates', 1).ok
);
assert.ok(
expectComponent(this.container, 'create-options-text', 0).ok
);
});
2016-02-08 23:46:30 +01:00
2016-05-20 21:49:29 +02:00
test('shows validation errors if options are not unique (makeAPoll)', function(assert) {
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
Ember.run(() => {
2016-05-20 21:49:29 +02:00
// there should be two inputs and both are changed to 'foo'
this.$('input').val('foo').trigger('change').trigger('focusout');
2016-02-08 23:46:30 +01:00
});
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
Ember.run(() => {
this.$('input').eq(0).val('bar').trigger('change');
});
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
);
});
2016-05-20 21:49:29 +02:00
test('shows validation errors if option is empty (makeAPoll)', 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
);
Ember.run(() => {
this.$('input').trigger('focusout');
});
assert.equal(
this.$('.form-group.has-error').length, 2
);
Ember.run(() => {
this.$('input').eq(0).val('foo').trigger('change');
});
assert.equal(
this.$('.form-group.has-error').length, 1
);
Ember.run(() => {
this.$('input').eq(1).val('bar').trigger('change');
});
assert.equal(
this.$('.form-group.has-error').length, 0
);
2016-02-08 23:46:30 +01:00
});