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

Introduce check and uncheck async helpers #9

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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