Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors Finders. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.0.3
* Removes `findInputByLabel` in favor of `findByLabel`
* Nests finders pertaining to form elements under `helpers/finder`
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ fillIn(input, 'Jane Doe');
```

## Install
Please note that this library has not reached version `1.0.0` and
should not be considered stable. Breaking changes may be introduced with
new versions in accordance with [Semantic
Versioning 2.0.0](http://semver.org/). Because of this, we recommend
that you explicitly specify the library version when installing.

For ember-cli >= `0.2.3`:
```shell
ember install ember-cli-test-interactions
ember install ember-cli-test-interactions@x.x.x
```

For ember-cli < `0.2.3`:
```shell
ember install:addon ember-cli-test-interactions
ember install:addon ember-cli-test-interactions@x.x.x
```

## Use
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-cli-test-interactions",
"version": "0.0.2",
"version": "0.0.3",
"description": "The default blueprint for ember-cli addons.",
"directories": {
"doc": "doc",
Expand Down
15 changes: 0 additions & 15 deletions test-support/helpers/finders.js

This file was deleted.

23 changes: 23 additions & 0 deletions test-support/helpers/finders/form-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from 'ember';

/**
* Finds the associated element with an id matching the value of the label's for attribute.
*
* Elements that can be associated with a label element:
* button, input, keygen, meter, output, progress, select, textarea
* http://www.w3.org/html/wg/drafts/html/master/semantics.html#category-label
*/

export function findByLabel(label) {
return findWithAssert(`#${label.attr('for')}`);
}

export function findLabelByText(text) {
let label = findWithAssert(`label:contains('${text}')`);

if (label.length > 1) {
label = $(label.toArray().findProperty('innerText', text));
}

return label;
}
20 changes: 10 additions & 10 deletions test-support/helpers/interactions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Ember from 'ember';
import {
findInputByLabel,
findByLabel,
findLabelByText
} from '../helpers/finders';
} from '../helpers/finders/form-elements';

const { isEmpty } = Ember;

Expand All @@ -15,7 +15,7 @@ export function checkByLabel(labelText) {
}

export function clickButton(text) {
return function() {
return () => {
let button = find(`button:contains('${text}')`);

if (isEmpty(button)) {
Expand All @@ -27,27 +27,27 @@ export function clickButton(text) {
}

export function clickLink(linkText) {
return function() {
return () => {
click(`a:contains('${linkText}')`);
};
}

export function clickRadioByLabel(label) {
return function() {
return () => {
const labelForInput = findLabelByText(label);
const input = findInputByLabel(labelForInput);
const input = findByLabel(labelForInput);

click(input);
}
}

export function fillInByLabel(label, value) {
return function() {
return () => {
const labelForInput = findLabelByText(label);
const input = findInputByLabel(labelForInput);
const inputOrTextarea = findByLabel(labelForInput);

fillIn(input, value);
return find(input).focusout();
fillIn(inputOrTextarea, value);
return find(inputOrTextarea).focusout();
};
}

Expand Down
23 changes: 19 additions & 4 deletions tests/acceptance/finders-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
import startApp from '../helpers/start-app';

import {
findInputByLabel,
findByLabel,
findLabelByText
} from '../helpers/finders';
} from '../helpers/finders/form-elements';

let app;
const { run } = Ember;
Expand Down Expand Up @@ -36,7 +36,7 @@ test('#findLabelByText finds the label by the label text', function(assert) {
});
});

test('#findInputByLabel finds the input by the label text', function(assert) {
test('#findByLabel finds the input by the label text', function(assert) {
assert.expect(1);

let label;
Expand All @@ -45,8 +45,23 @@ test('#findInputByLabel finds the input by the label text', function(assert) {
visit('/');
andThen(function() {
label = findLabelByText('Name');
input = findInputByLabel(label);
input = findByLabel(label);

assert.equal('John Doe', input.val(), 'expected John Doe to be the input value');
});
});

test('#findByLabel finds the select by the label text', function(assert) {
assert.expect(1);

let label;
let select;

visit('/');
andThen(function() {
label = findLabelByText('Label for second select');
select = findByLabel(label);

assert.equal('select_2', select.attr('name'), 'expected John Doe to be the input value');
});
});
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{outlet}}
{{partial "elements/select"}}
{{partial "elements/article"}}
{{partial "elements/aside"}}
{{partial "elements/checkbox"}}
Expand All @@ -11,7 +12,6 @@
{{partial "elements/ol"}}
{{partial "elements/p"}}
{{partial "elements/section"}}
{{partial "elements/select"}}
{{partial "elements/table"}}
{{partial "elements/ul"}}
{{link-to 'First link' 'firstLink'}}
7 changes: 7 additions & 0 deletions tests/dummy/app/templates/elements/select.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
</select>

<label for="select_2">Label for second select</label>
<select id="select_2" name="select_2">
<option value="value4" selected>Value 4</option>
<option value="value5">Value 5</option>
<option value="value6">Value 6</option>
</select>