decide.nolog.cz/tests/integration/components/create-options-text-test.js
jelhan 9c4bd9e669
upgrade ember-bootstrap and ember-bootstrap-cp-validations to latest versions (#146)
Ember Bootstrap has changed a lot since the very outdated pre 1.0 release
which was used here. Changes are mostly about using composable components
and closure actions.

Also replaces PhantomJS with Chrome in CI cause PhantomJS is also very
outdated and causing test failure not related to any real world issues.
Ember CLI has replaced PhantomJS with Chrome in v2.15.1, which is the
upfollowing minor release to the version currently used.
2018-10-28 22:54:14 +01:00

179 lines
4.7 KiB
JavaScript

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import jQuery from 'jquery';
import Ember from 'ember';
moduleForComponent('create-options-text', 'Integration | Component | create options text', {
integration: true,
beforeEach() {
this.inject.service('store');
}
});
test('it generates at least two input fields', function(assert) {
this.set('options', []);
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
assert.equal(this.$('input').length, 2);
});
test('generates input fields according options', function(assert) {
this.set('options', [
Ember.Object.create({ title: 'foo' }),
Ember.Object.create({ title: 'bar' }),
Ember.Object.create({ title: 'baz' })
]);
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
assert.equal(
this.$('input').length,
3,
'correct amount of input fields'
);
assert.deepEqual(
this.$('input').map(function() {
return jQuery(this).val();
}).get(),
['foo', 'bar', 'baz'],
'input fields have correct values and order'
);
});
test('observes changes to options', function(assert) {
this.set('options', [
Ember.Object.create({ title: 'foo' }),
Ember.Object.create({ title: 'bar' })
]);
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
assert.equal(
this.$('input').length,
2,
'has correct amount of input fields before change'
);
Ember.run(() => {
this.get('options').pushObject(
Ember.Object.create({ title: 'baz' })
);
});
assert.equal(
this.$('input').length,
3,
'has correct amount of input fields after change'
);
assert.equal(
this.$('input').eq(2).val(),
'baz',
'input field was added with correct value'
);
});
test('changes to value updates option', function(assert) {
this.set('options', [
Ember.Object.create({ title: 'foo' }),
Ember.Object.create({ title: 'bar' })
]);
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
this.$('input').eq(0).val('baz').trigger('change');
assert.equal(
this.get('options')[0].get('title'),
'baz',
'option was updated'
);
});
test('allows to add another option', function(assert) {
// 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'),
options: [
{ title: 'foo' },
{ title: 'bar' }
]
});
});
this.set('options', poll.get('options'));
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
assert.equal(
this.$('.form-group input').length,
2,
'there are two input fields before'
);
Ember.run(() => {
this.$('.form-group .add').eq(0).click();
});
assert.equal(
this.$('.form-group input').length,
3,
'another input field is added'
);
assert.deepEqual(
this.$('input').map(function() {
return jQuery(this).val();
}).get(),
['foo', '', 'bar'],
'it is added at correct position'
);
Ember.run(() => {
this.$('.form-group input').eq(1).val('baz').trigger('change');
});
assert.equal(
this.get('options').objectAt(1).get('title'),
'baz',
'options are observed for new input field'
);
});
test('allows to delete an option', function(assert) {
this.set('options', [
Ember.Object.create({ title: 'foo' }),
Ember.Object.create({ title: 'bar' }),
Ember.Object.create({ title: 'baz' })
]);
this.render(hbs`{{#bs-form as |form|}}{{create-options-text options=options form=form}}{{/bs-form}}`);
assert.equal(
this.$('.form-group input').length,
3,
'there are three input fields before'
);
assert.ok(
this.$('.delete').get().every((el) => {
return el.disabled === false;
}),
'options are deleteable'
);
this.$('.form-group .delete').eq(1).click();
Ember.run(() => {
assert.equal(
this.$('.form-group input').length,
2,
'one input field is deleted'
);
assert.deepEqual(
this.$('input').map(function() {
return jQuery(this).val();
}).get(),
['foo', 'baz'],
'correct input field is deleted'
);
assert.deepEqual(
this.get('options').map((option) => option.get('title')),
['foo', 'baz'],
'option is updated'
);
});
});