Skip to content

Selectmenu: call refresh(): this.menuItems was undefined #1370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/unit/selectmenu/selectmenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
</optgroup>
</select>

<label for="emptySelect">Select without an option:</label>
<select name="emptySelect" id="emptySelect">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a tab here.

</select>

</div>
</body>
</html>
33 changes: 33 additions & 0 deletions tests/unit/selectmenu/selectmenu_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,39 @@ test( "refresh - disabled optgroup", function() {
}
});

test( "refresh - empty select", function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is unnecessary. The subsequent test already covers this scenario.

expect( 1 );

var element = $( "#emptySelect" ).selectmenu();
try{
element.selectmenu( "refresh" );
ok( true, "refresh empty select without error" );
}catch(err) {
ok( false, err.message );
}
});

test( "refresh - structure remove all options", function() {
expect( 4 );

var element = $( "#speed" ).selectmenu(),
menu = element.selectmenu( "menuWidget" ),
options = element.find( "option" ),
menuItems = menu.find( "li" ).not( ".ui-selectmenu-optgroup" );
// refresh select with some options
element.selectmenu( "refresh" );
ok( true, "first refresh ok" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first refresh() call and this assertion are unnecessary.

// remove all options and refresh
element.children().remove();
element.selectmenu( "refresh" );
ok( true, "refresh empty select ok" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The true test is unnecessary as well. Really all you need is a single assertion that ensures that the length of menuItems is 0. If there is a JavaScript error the test suite will fail.

//check length for option and menuItems is 0
options = element.find( "option" );
menuItems = menu.find( "li" ).not( ".ui-selectmenu-optgroup" );
equal( options.length, 0, "options length" );
equal( menuItems.length, 0, "menu item length" );
});

test( "widget and menuWidget", function() {
expect( 4 );

Expand Down
11 changes: 6 additions & 5 deletions ui/selectmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,16 @@ return $.widget( "ui.selectmenu", {
var item,
options = this.element.find( "option" );

this._parseOptions( options );
this._renderMenu( this.menu, this.items );

this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" );

if ( !options.length ) {
return;
}

this._parseOptions( options );
this._renderMenu( this.menu, this.items );

this.menuInstance.refresh();
this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" );

item = this._getSelectedItem();

Expand All @@ -229,7 +230,7 @@ return $.widget( "ui.selectmenu", {
}

// If this is the first time the menu is being opened, render the items
if ( !this.menuItems ) {
if ( !this.menuItems || this.menuItems.length === 0 ) {
this._refreshMenu();
} else {

Expand Down