Reputation: 949
I'm creating a jQuery UI autocomplete widget passing in a callback function, like this:
input.autocomplete( {
...,
select: function ( event, ui ) {
valueField.val( ui.item.id );
},
...
} );
How can I unit test the select
function to assert that when it's called the valueField
is updated with the correct value?
Upvotes: 1
Views: 453
Reputation: 386305
If you're wanting to unit test your javascript, the best plan is to create first class functions for your callbacks.
Upvotes: 1
Reputation: 949
Using JsMockito you can capture the parameter passed in to the autocomplete
function, eg:
var params;
when( input ).autocomplete().then( function ( p ) {
params = p;
} );
By doing this you're saving the reference to the anonymous function, so you can call it later.
params.select( null, { item: { id: someId } } );
verify( valueField ).val( someId );
This technique can also be used for functions that are private, for example, when using the module pattern.
Upvotes: 0