Skip to content

Instantly share code, notes, and snippets.

@byelipk
Created December 9, 2014 22:09
Show Gist options
  • Save byelipk/03e3d41020210d6c333b to your computer and use it in GitHub Desktop.
Save byelipk/03e3d41020210d6c333b to your computer and use it in GitHub Desktop.
An example async test helper to use while integration testing autocomplete search with a Select2 component in an ember application. This example assumes we are searching for users based on their username.
import Ember from 'ember';
// register custom helper
Ember.Test.registerAsyncHelper('autocompleteSearch', function(app, username) {
// In order for this to work we need to mimic typing
// a username into the input field.
// 1. Let the initial keydown event activate Select2
triggerEvent('input.select2-input', 'keydown');
// 2. Let the initial value we enter into the Select2
// input field be equal to a substring of the length
// of the search term:
//
// username.slice(0, username.length - 1)
fillIn('input.select2-input', username.slice(0, username.length - 2));
// 3. Let the initial keyup event kick off a request to the autocomplete
// server.
triggerEvent('input.select2-input', 'keyup');
// 4. Let the next keydown event simulate entering the full search term into
// the Select2 input field.
triggerEvent('input.select2-input', 'keydown');
// 5. Let the next fillIn event enter the full search term into
// the Select2 input field.
fillIn('input.select2-input', username.slice(0, username.length - 1));
// 6. Let the final keyup event display results from the server
// in a Select2 dropdown.
triggerEvent('input.select2-input', 'keyup');
// 7. Select the search result from the dropdown list
triggerEvent('li.select2-result-selectable:first', 'mousedown');
triggerEvent('li.select2-result-selectable:first', 'mouseup');
return wait();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment