Skip to content

Commit

Permalink
#1 Alpha 2 ready. Acceptance test have been written. Still need to te…
Browse files Browse the repository at this point in the history
…st Ctrl-Enter submits. Also need to test before and after closure hooks.
  • Loading branch information
nadnoslen committed Mar 4, 2017
1 parent 6660909 commit f2fe4a2
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 7 deletions.
4 changes: 3 additions & 1 deletion addon/components/input-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import EscapeKeyClears from '../mixins/escape-key-clears';
import FocusSelectsText from '../mixins/focus-selects-text';
import TriggerFocus from '../mixins/trigger-focus';

export default Ember.TextField.extend(CtrlEnterSubmitsForm, EscapeKeyClears, FocusSelectsText, TriggerFocus, {});
export default Ember.TextField.extend(CtrlEnterSubmitsForm, EscapeKeyClears, FocusSelectsText, TriggerFocus, {
classNames: ['input-text']
});
9 changes: 9 additions & 0 deletions addon/mixins/trigger-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import Ember from 'ember';
* Why? Because when Ember RETURNS to a template for a second or more time, the element with autofocus does not
* seem to trigger.
* This fixes that.
*
* This mixin assumes that it is being assigned to a component that will respond to the `focus` event.
*/
export default Ember.Mixin.create({
/**
* @default autofocus is set to false.
*/
autofocus: false,
/**
* When the element is inserted, trigger the focus event using jQuery.
*/
_triggerFocus: Ember.on('didInsertElement', function () {
if (this.get('autofocus')) {
this.$().trigger('focus');
Expand Down
34 changes: 34 additions & 0 deletions tests/acceptance/autofocus-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | autofocus');

test('when autofocus is enabled and focusSelectsText? is also enabled', function (assert) {
visit('/acceptance/autofocus-with-select-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/autofocus-with-select-text');
assert.equal(document.activeElement.autofocus, true);
assert.equal(window.getSelection().toString(), 'Autofocus This Text');
});
});

test('when revisiting a template that already had been autofocused', function (assert) {
visit('/acceptance/autofocus-with-select-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/autofocus-with-select-text');
assert.equal(document.activeElement.autofocus, true);
assert.equal(window.getSelection().toString(), 'Autofocus This Text');
});
});

test('when autofocus is enabled and focusSelectsText? is disabled', function (assert) {
visit('/acceptance/autofocus-select-text-disabled');

andThen(function () {
assert.equal(currentURL(), '/acceptance/autofocus-select-text-disabled');
assert.equal(document.activeElement.autofocus, true);
assert.equal(window.getSelection().toString(), '');
});
});
47 changes: 47 additions & 0 deletions tests/acceptance/escape-clears-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global KeyEvent */
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | escape clears');

test('when escape clears text by default', function (assert) {
visit('/acceptance/escape-clears-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/escape-clears-text');
assert.equal(find('input.escape-clears-by-default').val(), 'Escape Clears This Text');
keyEvent('input.escape-clears-by-default', 'keyup', KeyEvent.DOM_VK_ESCAPE);
andThen(() => {
assert.equal(find('input.escape-clears-by-default').val(), '');
});

});
});

test('when escape clears text because escapeKeyClears? was set to true', function (assert) {
visit('/acceptance/escape-clears-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/escape-clears-text');
assert.equal(find('input.escape-clears-enabled').val(), 'Escape Clears This Text');
keyEvent('input.escape-clears-enabled', 'keyup', KeyEvent.DOM_VK_ESCAPE);
andThen(() => {
assert.equal(find('input.escape-clears-enabled').val(), '');
});

});
});

test('when escape WILL NOT clear text because escapeKeyClears? was set to false', function (assert) {
visit('/acceptance/escape-clears-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/escape-clears-text');
assert.equal(find('input.escape-clears-disabled').val(), 'Escape Will Not Clear This Text');
keyEvent('input.escape-clears-disabled', 'keyup', KeyEvent.DOM_VK_ESCAPE);
andThen(() => {
assert.equal(find('input.escape-clears-disabled').val(), 'Escape Will Not Clear This Text');
});

});
});
32 changes: 32 additions & 0 deletions tests/acceptance/focus-selects-text-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | focus selects text');

test('when focusSelectsText? is false or turned off', function (assert) {
visit('/acceptance/focus-selects-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/focus-selects-text');
assert.equal(find('input.select-enabled').val(), 'Select This Text');
assert.equal(window.getSelection().toString(), '');
triggerEvent('input.select-enabled', 'focus');
andThen(() => {
assert.equal(window.getSelection().toString(), 'Select This Text');
});
});
});

test('when focusSelectsText? is false or turned off', function (assert) {
visit('/acceptance/focus-selects-text');

andThen(function () {
assert.equal(currentURL(), '/acceptance/focus-selects-text');
assert.equal(find('input.select-disabled').val(), 'Text Will Not Select On Focus');
assert.equal(window.getSelection().toString(), '');
triggerEvent('input.select-disabled', 'focus');
andThen(() => {
assert.equal(window.getSelection().toString(), '');
});
});
});
9 changes: 8 additions & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import Ember from 'ember';
//noinspection JSFileReferences
import config from './config/environment';

const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});

Router.map(function() {
Router.map(function () {
this.route('acceptance', function () {
this.route('focus-selects-text');
this.route('autofocus-with-select-text');
this.route('autofocus-select-text-disabled');
this.route('escape-clears-text');
});
});

export default Router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Ember from 'ember';

export default Ember.Route.extend({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Ember from 'ember';

export default Ember.Route.extend({});
3 changes: 3 additions & 0 deletions tests/dummy/app/routes/acceptance/escape-clears-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Ember from 'ember';

export default Ember.Route.extend({});
3 changes: 3 additions & 0 deletions tests/dummy/app/routes/acceptance/focus-selects-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Ember from 'ember';

export default Ember.Route.extend({});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{input-text value="Autofocus This Text" autofocus=true focusSelectsText?=false}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{input-text value="Autofocus This Text" autofocus=true focusSelectsText?=true}}
3 changes: 3 additions & 0 deletions tests/dummy/app/templates/acceptance/escape-clears-text.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{input-text class="escape-clears-by-default" value="Escape Clears This Text"}}
{{input-text class="escape-clears-enabled" value="Escape Clears This Text" escapeKeyClears?=true}}
{{input-text class="escape-clears-disabled" value="Escape Will Not Clear This Text" escapeKeyClears?=false}}
3 changes: 3 additions & 0 deletions tests/dummy/app/templates/acceptance/focus-selects-text.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{input-text class="select-enabled" value="Select This Text" focusSelectsText?=true}}

{{input-text class="select-disabled" value="Text Will Not Select On Focus" focusSelectsText?=false}}
4 changes: 2 additions & 2 deletions tests/integration/components/input-text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ moduleForComponent('input-text', 'Integration | Component | input text', {
integration: true
});

test('it renders', function (assert) {
test('when rendered the .input-text is present', function (assert) {
this.render(hbs`{{input-text}}`);
assert.equal(this.$().text().trim(), '');
assert.ok(this.$('input[type=text]').hasClass('input-text'));
});
2 changes: 2 additions & 0 deletions tests/unit/mixins/ctrl-enter-submits-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ test('it works', function (assert) {
let subject = CtrlEnterSubmitsFormObject.create();
assert.ok(subject);
});

// TODO: test before and after hooks
4 changes: 3 additions & 1 deletion tests/unit/mixins/escape-key-clears-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { module, test } from 'qunit';
module('Unit | Mixin | escape key clears');

// Replace this with your real tests.
test('it works', function (assert) {
test('check out the acceptance tests', function (assert) {
let EscapeKeyClearsObject = Ember.Object.extend(EscapeKeyClearsMixin);
let subject = EscapeKeyClearsObject.create();
assert.ok(subject);
});

// TODO: test before and after hooks
2 changes: 1 addition & 1 deletion tests/unit/mixins/focus-selects-text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { module, test } from 'qunit';
module('Unit | Mixin | focus selects text');

// Replace this with your real tests.
test('it works', function (assert) {
test('check out the acceptance tests', function (assert) {
let FocusSelectsTextObject = Ember.Object.extend(FocusSelectsTextMixin);
let subject = FocusSelectsTextObject.create();
assert.ok(subject);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mixins/trigger-focus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { module, test } from 'qunit';
module('Unit | Mixin | trigger focus');

// Replace this with your real tests.
test('it works', function (assert) {
test('check out the acceptance tests', function (assert) {
let TriggerFocusObject = Ember.Object.extend(TriggerFocusMixin);
let subject = TriggerFocusObject.create();
assert.ok(subject);
Expand Down

0 comments on commit f2fe4a2

Please sign in to comment.