How to set an element's property using Funcunit in qunit window (test window)

175 views Asked by At

I am using FuncUnit with qunit to test an application. I would like to set selected property of an option of <select> menu to be true .

In jQuery, I could do something like

$('select#id option:eq(0)').prop('selected', true).change();

to achieve this. But, in funcunit,

S('select#id option:eq(0)').prop('selected', true).trigger('change');

selector finds the element and just get (not set) the value of prop selected. I want to set prop of the element.

2

There are 2 answers

0
NageN On BEST ANSWER

As per comment from funcunit team - A user wouldn't be able to set the property of an option, they would just know to perform some action such as a click, drag, etc.

so to achieve this we need to do

S.win.$('select#id option:eq(0)').prop('selected', true).trigger('change');

This runs jquery on test window.

1
parrker9 On

Have you tried something like this instead?

S('select#id option:eq(0)').attr('selected', 'selected').trigger('change');

In jQuery this sets any attribute of a tag. For example, for a tag <a href="#">Link</a> you could set an attribute href like so: $('a').attr('href', '#new-link');). This should work for adding the selected property too in old XHTML style. Unsetting a property might be hard though.

The other option might be to use click, like so:

S('select#id option:eq(0)').click();

You should not even need to trigger change.