Skip to content

Commit

Permalink
feat: add focus assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
t1st3 committed Nov 5, 2015
1 parent eeb8ca5 commit 4551346
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chai-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,11 @@
};
}
);

chai.Assertion.addMethod('focus', function () {
this.assert(
flag(this, 'object').is(':focus')
, 'expected #{this} to have focus'
, 'expected #{this} not to have focus');
});
}));
39 changes: 39 additions & 0 deletions test/chai-jquery-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,43 @@ describe("jQuery assertions", function(){
}).should.fail("expected " + inspect(subject) + " not to have 'span'");
});
});

describe("focus", function(){
var focused = $('<input type="text" id="focused" name="focused">');
var blurred = $('<input type="text" id="blurred" name="blurred">');

beforeEach(function(done) {
focused.appendTo('#mocha');
blurred.appendTo('#mocha');
focused.on("focus", function () {
done();
});
focused.trigger("focus");
});

afterEach(function() {
focused.remove();
blurred.remove();
});

it("passes when the element has focus", function(){
focused.should.have.focus();
});

it("passes negated when the element does not have focus", function(){
blurred.should.not.have.focus();
});

it("fails when the element does not have focus", function(){
(function(){
blurred.should.have.focus();
}).should.fail("expected " + inspect(blurred) + " to have focus");
});

it("fails negated when element has focus", function(){
(function(){
focused.should.not.have.focus();
}).should.fail("expected " + inspect(focused) + " not to have focus");
});
});
});

0 comments on commit 4551346

Please sign in to comment.