siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
76 lines (56 loc) • 2.19 kB
JavaScript
StartTest(function (t) {
t.it('should replace selected text with typed text', function (t) {
document.body.innerHTML = '<input id="foo" type="text" value="Default" />';
var field = document.getElementById('foo');
t.chain(
function (next) {
t.selectText(field);
t.type(field, 'Replacement', next);
},
function () {
t.is(field.value, 'Replacement', 'Selecting text and typing replaces original value.');
}
);
})
t.it('should delete partially selected text on BACKSPACE', function (t) {
document.body.innerHTML = '<input id="foo" type="text" value="123456123" />';
var field = document.getElementById('foo');
t.selectText(field, 6);
t.chain(
{ type : '[BACKSPACE]', target : '#foo' },
function () {
t.is(field.value, '123456');
t.is(t.getCaretPosition(field), 6);
}
);
});
t.it('should delete partially selected text on DELETE', function (t) {
document.body.innerHTML = '<input id="foo" type="text" value="123456123" />';
var field = document.getElementById('foo');
t.selectText(field, 3, 6);
t.chain(
{ type : '[DELETE]', target : '#foo' },
function () {
t.is(field.value, '123123');
t.is(t.getCaretPosition(field), 3);
}
);
});
// not in the firefox
!t.bowser.firefox && t.it('should mimic caret movement even if keypress was prevented', function (t) {
document.body.innerHTML = '<input id="foo" type="text" value="1000" />';
var field = document.getElementById('foo');
field.addEventListener('keypress', function (ev) {
if (ev.key.match('Arrow')) {
ev.preventDefault();
}
});
t.selectText(field, 0, 2);
t.chain(
{ type : '[ARROWLEFT][ARROWRIGHT][BACKSPACE]28', target : '#foo' },
function () {
t.is(field.value, 28000);
}
);
});
});