Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 08b3238

Browse files
author
Jeffrey Cherewaty
committed
Add isFocused getter
1 parent fbbebc4 commit 08b3238

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55

66
## [Unreleased]
77

8+
- Add isFocused getter
9+
810
## [0.9.2] - 2018-02-05
911

1012
### Fixed
1113

12-
- Make sure $root descriptor function is not wrapped in the chaining mechanism - https://github.com/bigtestjs/interactor/pull/60
14+
- Make sure $root descriptor function is not wrapped in the chaining mechanism - https://github.com/bigtestjs/interactor/pull/60
1315

1416
## [0.9.1] - 2018-10-23
1517

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ describe('Logging in', () => {
211211
- `isVisible(selector)`
212212
- `isHidden(selector)`
213213
- `isPresent(selector)`
214+
- `isFocused(selector)`
214215
- `is(selector, match)`
215216
- `hasClass(selector)`
216217

src/interactions/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export { default as is } from './is';
2222
export { default as isVisible } from './is-visible';
2323
export { default as isHidden } from './is-hidden';
2424
export { default as isPresent } from './is-present';
25+
export { default as isFocused } from './is-focused';
2526

2627
// interaction helpers
2728
export { computed, action } from './helpers';

src/interactions/is-focused.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { computed } from './helpers';
2+
3+
/**
4+
* Property creator for returning `true` or `false` when an element
5+
* should have focus.
6+
*
7+
* ``` html
8+
* <form>
9+
* <input type="email" id="email" />
10+
* <input type="password" id="password" />
11+
* </form>
12+
* ```
13+
*
14+
* ``` javascript
15+
* new Interactor('#email').isFocused //=> true when email input focused
16+
* new Interactor('#password').isFocused //=> false when email input focused
17+
* ```
18+
*
19+
* @function isFocused
20+
* @param {String} [selector] - Nested element query selector
21+
* @returns {Object} Property descriptor
22+
*/
23+
export default function(selector) {
24+
return computed(function() {
25+
return this.$(selector) === document.activeElement;
26+
});
27+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div id="focus-form">
2+
<label class="test-field">
3+
<input type="email" id="email" autofocus />
4+
</label>
5+
<label class="test-field">
6+
<input type="password" id="password" />
7+
</label>
8+
</div>

tests/interactions/is-focused.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* global describe, beforeEach, it */
2+
import { expect } from 'chai';
3+
import { useFixture } from '../helpers';
4+
import { interactor, isFocused } from '../../src';
5+
6+
@interactor class FocusedInteractor {
7+
isEmailFocused = isFocused('#email');
8+
isPasswordFocused = isFocused('#password');
9+
}
10+
11+
describe.only('BigTest Interaction: isFocused', () => {
12+
let test;
13+
14+
useFixture('is-focused-fixture');
15+
16+
beforeEach(() => {
17+
test = new FocusedInteractor('#focus-form');
18+
});
19+
20+
it('has isFocused properties', () => {
21+
expect(test).to.have.property('isFocused').that.is.a('boolean');
22+
expect(test).to.have.property('isEmailFocused').that.is.a('boolean');
23+
expect(test).to.have.property('isPasswordFocused').that.is.a('boolean');
24+
});
25+
26+
it('returns true if the element is focused', () => {
27+
expect(test.isFocused).to.be.false;
28+
expect(test.isEmailFocused).to.be.true;
29+
expect(test.isPasswordFocused).to.be.true;
30+
});
31+
32+
it('returns false if the element is not focused', () => {
33+
test.focus('#password');
34+
expect(test.isFocused).to.be.false;
35+
expect(test.isEmailFocused).to.be.false;
36+
expect(test.isPasswordFocused).to.be.true;
37+
});
38+
});

0 commit comments

Comments
 (0)