Skip to content

Instantly share code, notes, and snippets.

@ksykulev
Created November 11, 2013 02:08
Show Gist options
  • Save ksykulev/7406641 to your computer and use it in GitHub Desktop.
Save ksykulev/7406641 to your computer and use it in GitHub Desktop.
it("should allow the last character to be deleted", function(){
var chosen,
input,
key,
returnQuery,
clock = sinon.useFakeTimers(),
xhr = sinon.useFakeXMLHttpRequest(),
requests = [];
xhr.onCreate = function (xhr) { requests.push(xhr) };
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click').width('135px');
input = $('input', chosen).val('monkey');
key = $.Event('keyup');
key.which = 32;
//fire off request
input.trigger(key);
clock.tick(750);
//respond
returnQuery = 'monkey';
requests[0].respond(200, { "Content-Type": "application/json" }, '{ "q": "'+returnQuery+'", "results": [{"id":1, "text":"first monkey"}]}');
//assert
expect(input.val()).to.equal(returnQuery);
//delete all but 1 character
input.val('m');
key = $.Event('keyup');
key.which = 32;
//fire request
input.trigger(key);
clock.tick(750);
//delete all characters before server responds
key = $.Event('keyup');
key.which = 8;
input.val('');
input.trigger(key);
returnQuery = 'm';
requests[1].respond(200, { "Content-Type": "application/json" }, '{ "q": "'+returnQuery+'", "results": [{"id":1, "text":"first monkey"}]}');
//assert that the text box is empty and doesn't contain the last response q
expect(input.val()).to.be.empty();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment