1

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?

2 Answers 2

1

If you're wanting to unit test your javascript, the best plan is to create first class functions for your callbacks.

Sign up to request clarification or add additional context in comments.

2 Comments

That would work, but then you lose the advantage of private or anonymous functions.
Very true. You get to decide which is more important to you.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.