Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created January 24, 2014 19:44
Show Gist options
  • Save kentcdodds/8604635 to your computer and use it in GitHub Desktop.
Save kentcdodds/8604635 to your computer and use it in GitHub Desktop.
Potential docsSearchInputSpec
describe('docsSearchInput', function() {
var FORWARD_SLASH_KEYCODE = 191;
var inputElement, body;
beforeEach(function() {
body = angular.element(document.body);
body.empty();
});
beforeEach(module('docsApp'));
beforeEach(inject(function($rootScope, $compile) {
$scope = $rootScope.$new();
inputElement = angular.element('<input docs-search-input />');
body.append(inputElement);
$compile(inputElement)($scope);
$scope.$apply();
}));
function addElementAndFocus(html, appendTo) {
var input = angular.element(html);
(appendTo || body).append(input);
input.focus();
return input;
}
function triggerAndExpectFocus(expectedElement) {
browserTrigger(body, 'keypress', {
keyCode: FORWARD_SLASH_KEYCODE
});
expect(document.activeElement === expectedElement);
}
it('should focus on docsSearchInput when forward slash is pressed', function() {
triggerAndExpectFocus(inputElement);
});
it('should not focus on docsSearchInput when focus on input element even when forward slash is pressed', function() {
var input = addElementAndFocus('<input />');
triggerAndExpectFocus(input);
});
it('should not focus on docsSearchInput when focus on select element even when forward slash is pressed', function() {
var select = addElementAndFocus('<select><option></option></select>');
triggerAndExpectFocus(select);
});
it('should not focus on docsSearchInput when focus on option element even when forward slash is pressed', function() {
var select = addElementAndFocus('<select></select>');
var option = addElementAndFocus('<option></option>', select);
triggerAndExpectFocus(option);
});
it('should not focus on docsSearchInput when focus on textarea element even when forward slash is pressed', function() {
var textarea = addElementAndFocus('<textarea></textarea>');
triggerAndExpectFocus(textarea);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment