2017-05-29 21:50:03 +03:00
|
|
|
import $ from 'jquery';
|
2015-10-15 15:03:26 +03:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2017-04-18 10:20:38 +03:00
|
|
|
import wait from 'ember-test-helpers/wait';
|
|
|
|
import {click, find, triggerEvent} from 'ember-native-dom-helpers';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {describe, it} from 'mocha';
|
|
|
|
import {expect} from 'chai';
|
|
|
|
import {setupComponentTest} from 'ember-mocha';
|
2017-04-18 10:20:38 +03:00
|
|
|
|
|
|
|
// NOTE: If the browser window is not focused/visible CodeMirror (or Chrome?) will
|
|
|
|
// take longer to respond to/fire events so it's possible that some of these tests
|
|
|
|
// will take 1-3 seconds
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
describe('Integration: Component: gh-cm-editor', function () {
|
|
|
|
setupComponentTest('gh-cm-editor', {
|
2015-10-15 15:03:26 +03:00
|
|
|
integration: true
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
it('handles change event', function () {
|
2016-11-24 01:50:57 +03:00
|
|
|
this.set('text', '');
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2016-11-24 01:50:57 +03:00
|
|
|
this.render(hbs`{{gh-cm-editor text class="gh-input" update=(action (mut text))}}`);
|
2017-04-18 10:20:38 +03:00
|
|
|
// access CodeMirror directly as it doesn't pick up changes to the textarea
|
|
|
|
let cm = find('.gh-input .CodeMirror').CodeMirror;
|
|
|
|
cm.setValue('Testing');
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
return wait().then(() => {
|
|
|
|
expect(this.get('text'), 'text value after CM editor change')
|
|
|
|
.to.equal('Testing');
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
2017-04-18 10:20:38 +03:00
|
|
|
});
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
it('handles focus event', function (done) {
|
|
|
|
// CodeMirror's events are triggered outside of anything we can watch for
|
|
|
|
// in the tests so let's run the class check when we know the event has
|
|
|
|
// been fired and timeout if it's not fired as we expect
|
|
|
|
let onFocus = () => {
|
|
|
|
// wait for runloop to finish so that the new class has been rendered
|
|
|
|
wait().then(() => {
|
2017-06-02 00:01:43 +03:00
|
|
|
expect($(find('.gh-input')).hasClass('focus'), 'has focused class on first render with autofocus')
|
2017-04-18 10:20:38 +03:00
|
|
|
.to.be.true;
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
this.set('onFocus', onFocus);
|
|
|
|
this.set('text', '');
|
2015-10-15 15:03:26 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
this.render(hbs`{{gh-cm-editor text class="gh-input" update=(action (mut text)) focus-in=(action onFocus)}}`);
|
|
|
|
|
|
|
|
// CodeMirror polls the input for changes every 100ms
|
|
|
|
click('textarea');
|
|
|
|
triggerEvent('textarea', 'focus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles blur event', async function () {
|
|
|
|
this.set('text', '');
|
|
|
|
this.render(hbs`{{gh-cm-editor text class="gh-input" update=(action (mut text))}}`);
|
|
|
|
|
2017-06-02 00:01:43 +03:00
|
|
|
expect($(find('.gh-input')).hasClass('focus')).to.be.false;
|
2017-04-18 10:20:38 +03:00
|
|
|
|
|
|
|
await click('textarea');
|
|
|
|
await triggerEvent('textarea', 'focus');
|
|
|
|
|
2017-06-02 00:01:43 +03:00
|
|
|
expect($(find('.gh-input')).hasClass('focus')).to.be.true;
|
2017-04-18 10:20:38 +03:00
|
|
|
|
|
|
|
await triggerEvent('textarea', 'blur');
|
|
|
|
|
2017-06-02 00:01:43 +03:00
|
|
|
expect($(find('.gh-input')).hasClass('focus')).to.be.false;
|
2017-04-18 10:20:38 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can autofocus', function (done) {
|
|
|
|
// CodeMirror's events are triggered outside of anything we can watch for
|
|
|
|
// in the tests so let's run the class check when we know the event has
|
|
|
|
// been fired and timeout if it's not fired as we expect
|
|
|
|
let onFocus = () => {
|
|
|
|
// wait for runloop to finish so that the new class has been rendered
|
|
|
|
wait().then(() => {
|
2017-06-02 00:01:43 +03:00
|
|
|
expect(this.$('.gh-input').hasClass('focus'), 'has focused class on first render with autofocus')
|
2017-04-18 10:20:38 +03:00
|
|
|
.to.be.true;
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.set('onFocus', onFocus);
|
|
|
|
this.set('text', '');
|
2016-11-24 01:50:57 +03:00
|
|
|
|
2017-04-18 10:20:38 +03:00
|
|
|
this.render(hbs`{{gh-cm-editor text class="gh-input" update=(action (mut text)) autofocus=true focus-in=(action onFocus)}}`);
|
2016-11-24 01:50:57 +03:00
|
|
|
});
|
|
|
|
});
|