Pruebas para enfocar una directiva AngularJS
¿Cómo se puede probar el enfoque en una directiva AngularJS? Espero que lo siguiente funcione:
describe('focus test', function(){
it('should focus element', function(){
var element = $('<input type="text" />');
// Append to body because otherwise it can't be foccused
element.appendTo(document.body);
element.focus();
expect(element.is(':focus')).toBe(true);
});
});
Sin embargo, esto solo funciona en IE, falla en Firefox y Chrome
Actualizar: La solución de @S McCrohan funciona. Usando esto creé un 'toHaveFocus' matcher:
beforeEach(function(){
this.addMatchers({
toHaveFocus: function(){
this.message = function(){
return 'Expected \'' + angular.mock.dump(this.actual) + '\' to have focus';
};
return document.activeElement === this.actual[0];
}
});
});
Que se utiliza de la siguiente manera:
expect(myElement).toHaveFocus();
Tenga en cuenta que para las pruebas relacionadas con focus, el elemento compilado debe adjuntarse al DOM, lo que se puede hacer de la siguiente manera:
myElement.appendTo(document.body);
22
Author: Mark Lagendijk, 2013-09-17
2 answers
Intenta 'document.activeElement'
en lugar de ':focus'
. No lo he probado en karma, pero $('document.activeElement')
se comporta como se desea bajo jQuery estándar.
10
Author: S McCrohan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-17 23:58:31
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-17 23:58:31
En Jasmine 2, esto es ahora:
beforeEach(function() {
jasmine.addMatchers({
toHaveFocus: function() {
return {
compare: function(actual) {
return {
pass: document.activeElement === actual[0]
};
}
};
}
});
});
7
Author: Christian Iacullo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-17 03:50:29
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-17 03:50:29