Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Introduce check and uncheck async helpers
Browse files Browse the repository at this point in the history
Promotes `check` and `uncheck` test helpers from [emberjs/ember.js] from
behind the [ember-testing-checkbox-helpers] feature flag.

Matches Capybara's [#check] and [#uncheck] helper methods.

Additionally introduces `assert.checked` and `assert.unchecked` QUnit
assertions.

[ember-testing-checkbox-helpers]: https://github.com/emberjs/ember.js/blob/860bb4be498466ac4c914adb1f139e5cd2fbc123/FEATURES.md#feature-flags
[emberjs/ember.js]: emberjs/ember.js#9798
[#check]: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions#check-instance_method
[#uncheck]: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions#uncheck-instance_method
  • Loading branch information
seanpdoyle committed Nov 3, 2015
1 parent 883141c commit 6797f79
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ master
------

* Fix `assert.include` and `assert.notInclude` default failure message.
* Introduce `check` and `uncheck` global async helpers.
Introduce `assert.checked` and `assert.unchecked`. [#9]

[#9]: https://github.com/thoughtbot/ralphs-little-helpers/pull/9

0.0.3
-----
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@ import 'ralphs-little-helpers/extend-qunit';

## Helpers

**Imported**

* `clickOn(text)` - Clicks on elements containing the text
* `clickRole(role)` - Clicks on elements with a matching `[data-role]`
* `findRole(role)` - Finds (with assert) an element with matching `[data-role]`
* `fillInField(name, value)` - Fills in a field with a `[name]` with the given
value

**Global**

* `check(selector, context)` - Ensure an `input[type="checkbox"]` is checked
* `uncheck(selector, context)` - Ensure an `input[type="checkbox"]` is unchecked
* [`within(scope, block)`][within] - Scopes subsequent calls to test helpers by
the provided selector.

Expand All @@ -59,6 +66,10 @@ import 'ralphs-little-helpers/extend-qunit';
node's text equals the `actual` string
* `assert.hasClass(expected, actual)` - Asserts that the `expected` node or
selector has the `actual` class
* `assert.checked(expected, message)` - Asserts that the `expected` node or
selector is `:checked`
* `assert.unchecked(expected, message)` - Asserts that the `expected` node or
selector is not `:checked`

## Installation

Expand Down
31 changes: 31 additions & 0 deletions addon/checkbox-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Ember from 'ember';

const { assert, run } = Ember;

function ensureCheckState(isChecked, app, selector, context) {
const {
click,
findWithAssert
} = app.testHelpers;
const word = isChecked ? 'check' : 'uncheck';

const $element = findWithAssert(selector, context);
assert(
`To ${word} '${selector}', the input must be a checkbox`,
$element.prop('type') === 'checkbox'
);

run(() => {
if ($element.is(':checked') !== isChecked) {
click($element);
}
});
}

Ember.Test.registerAsyncHelper('check', function() {
ensureCheckState(true, ...arguments);
});

Ember.Test.registerAsyncHelper('uncheck', function() {
ensureCheckState(false, ...arguments);
});
22 changes: 22 additions & 0 deletions addon/extend-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,25 @@ assert.hasClass = function(selectorOrNode, expected, message) {

this.ok(node.hasClass(expected.toString()), message);
};

assert.checked = function(selectorOrNode, message) {
const node = Ember.$(selectorOrNode);
const checked = node.is(':checked');

if (!message) {
message = `expected node (${selectorOrNode.toString()}) to be checked`;
}

this.ok(checked, message);
};

assert.unchecked = function(selectorOrNode, message) {
const node = Ember.$(selectorOrNode);
const unchecked = !node.is(':checked');

if (!message) {
message = `expected node (${selectorOrNode.toString()}) to be unchecked`;
}

this.ok(unchecked, message);
};
10 changes: 5 additions & 5 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import findRole from 'ralphs-little-helpers/find-role';
import clickOn from 'ralphs-little-helpers/click-on';
import clickRole from 'ralphs-little-helpers/click-role';
import fillInField from 'ralphs-little-helpers/fill-in-field';
import 'ralphs-little-helpers/extend-qunit';
import findRole from './find-role';
import clickOn from './click-on';
import clickRole from './click-role';
import fillInField from './fill-in-field';
import './extend-qunit';

export {
clickOn,
Expand Down
1 change: 1 addition & 0 deletions test-support/helpers/ralphs-little-helpers/test-helpers.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import 'ralphs-little-helpers/checkbox-helpers';
import 'ralphs-little-helpers/within';
2 changes: 2 additions & 0 deletions tests/.jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"predef": [
"check",
"uncheck",
"within",
"document",
"window",
Expand Down
25 changes: 25 additions & 0 deletions tests/acceptance/app-uses-helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ test('include and notInclude matchers', assert => {
});
});

test('checkbox helpers', assert => {
visit('/');

andThen(() => {
assert.checked('#checked-checkbox');
assert.unchecked('#unchecked-checkbox');
});

check('#checked-checkbox');
uncheck('#unchecked-checkbox');

andThen(() => {
assert.checked('#checked-checkbox');
assert.unchecked('#unchecked-checkbox');
});

check('#unchecked-checkbox');
uncheck('#checked-checkbox');

andThen(() => {
assert.unchecked('#checked-checkbox');
assert.checked('#unchecked-checkbox');
});
});

test('within', assert => {
visit('/');

Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<label for="labelled-checkbox">Checkbox!</label>
<input id="labelled-checkbox" type="checkbox">

<input id="unchecked-checkbox" type="checkbox">
<input id="checked-checkbox" type="checkbox" checked>

{{#if submitted}}
<span id="submitted"></span>
{{/if}}
Expand Down

0 comments on commit 6797f79

Please sign in to comment.