Skip to content

Commit

Permalink
Merge pull request #280 from ArcBees/mmc-fix-271
Browse files Browse the repository at this point in the history
Fix for #271 : "this.selectedItem is undefined" when adding new items to ChosenListBox and calling update()
  • Loading branch information
meriouma committed Oct 16, 2015
2 parents 6241a15 + 13098e8 commit 7a30e5f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect;
import com.arcbees.chosen.integrationtest.client.testcases.ChooseOption;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.DisableSearchThreshold;
import com.arcbees.chosen.integrationtest.client.testcases.EnabledDisabled;
import com.arcbees.chosen.integrationtest.client.testcases.HideEmptyValues;
Expand Down Expand Up @@ -70,6 +71,7 @@ public ChosenSampleIntegrationTests() {
registerTestCase(new SingleBackstrokeDelete());
registerTestCase(new SimpleValueListBoxOnChange());
registerTestCase(new SimpleMultiValueListBoxOnChange());
registerTestCase(new ChosenListBoxMultipleSelectAddItems());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.arcbees.chosen.integrationtest.client.testcases;

import com.arcbees.chosen.client.gwt.ChosenListBox;
import com.arcbees.chosen.integrationtest.client.TestCase;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class ChosenListBoxMultipleSelectAddItems extends TestCase {
public static final String BUTTON_ID = "ChosenListBoxMultipleSelectAddItems-button";
public static final String SELECTED_VALUE = "Six";

@Override
public void run() {
final ChosenListBox listBox = new ChosenListBox(true);
listBox.setWidth("200px");
listBox.addItem("One");
listBox.addItem("Two");
listBox.addItem("Three");

Button button = new Button("Add items and select Six");
button.getElement().setId(BUTTON_ID);
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
listBox.clear();
listBox.addItem("Four");
listBox.addItem("Five");
listBox.addItem("Six");
listBox.update();
listBox.setSelectedValue(SELECTED_VALUE);
}
});

RootPanel.get().add(listBox);
RootPanel.get().add(button);
}
}
17 changes: 17 additions & 0 deletions integration-test/src/test/java/DesktopChosenIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.arcbees.chosen.integrationtest.client.testcases.AllowSingleDeselect;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox;
Expand Down Expand Up @@ -269,6 +270,22 @@ public void singleMobile_mobileLayoutUsed() throws InterruptedException {
assertThat(getOptions()).isEqualTo(CarBrand.getAllNames(RENDERER));
}

/**
* Tests that the values are added/selected when using update/setItemSelected.
* See https://github.com/ArcBees/gwtchosen/issues/271
*/
@Test
public void chosenListBox_updateAndSelect_addsAndSelectItem() {
// Given
loadTestCase(new ChosenListBoxMultipleSelectAddItems());

// When
getElementById(ChosenListBoxMultipleSelectAddItems.BUTTON_ID).click();

// Then
assertThat(getSelectedOptionText()).isEqualTo(ChosenListBoxMultipleSelectAddItems.SELECTED_VALUE);
}

/**
* Goal: verify that tab navigation is possible when Chosen is within a form.
*/
Expand Down
17 changes: 17 additions & 0 deletions integration-test/src/test/java/MobileChosenIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openqa.selenium.WebElement;

import com.arcbees.chosen.integrationtest.client.domain.CarBrand;
import com.arcbees.chosen.integrationtest.client.testcases.ChosenListBoxMultipleSelectAddItems;
import com.arcbees.chosen.integrationtest.client.testcases.MaxSelectedOptions;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleMultiValueListBox;
import com.arcbees.chosen.integrationtest.client.testcases.SimpleValueListBox;
Expand Down Expand Up @@ -133,6 +134,22 @@ public void singleMobile_closeDropdown() throws InterruptedException {
assertDropdownIsClosed();
}

/**
* Tests that the values are added/selected when using update/setItemSelected.
* See https://github.com/ArcBees/gwtchosen/issues/271
*/
@Test
public void chosenListBox_updateAndSelect_addsAndSelectItem() {
// Given
loadTestCase(new ChosenListBoxMultipleSelectAddItems());

// When
getElementById(ChosenListBoxMultipleSelectAddItems.BUTTON_ID).click();

// Then
assertThat(getSelectedOptionText()).isEqualTo("1 item selected");
}

@Override
protected void assertDropdownIsClosed() {
webDriverWait().until(new Predicate<WebDriver>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2014 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down Expand Up @@ -630,7 +630,9 @@ protected void resultsHide() {
}

protected void resultsResetCleanup() {
selectedItem.find("abbr").remove();
if (selectedItem != null) {
selectedItem.find("abbr").remove();
}
}

protected void resultsSearch() {
Expand Down Expand Up @@ -837,9 +839,6 @@ protected void closeField() {
container.removeClass(css.chznContainerActive());
winnowResultsClear();

// TODO check if it's needed
// clearBackstroke();

showSearchFieldDefault(defaultText);
searchFieldScale(fWidth);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2014 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down Expand Up @@ -80,6 +80,13 @@ protected void onResultSelected(OptionItem item, String newValue, String oldValu
fireChosenChangeEventIfNotEqual(item, newValue, oldValue);
}

@Override
protected void update() {
super.update();

closeField();
}

private void resultDeselect(OptionItem item, GQuery element) {
choices--;

Expand All @@ -103,9 +110,9 @@ protected void resultsHide() {
private void updateSelectedText() {
String selectedText;
if (choices > 1) {
selectedText = getOptions().getManySelectedTextMultipleMobile();
selectedText = getOptions().getManySelectedTextMultipleMobile();
} else {
selectedText = getOptions().getOneSelectedTextMultipleMobile();
selectedText = getOptions().getOneSelectedTextMultipleMobile();
}

selectedText = selectedText.replace("{}", "" + choices);
Expand Down

0 comments on commit 7a30e5f

Please sign in to comment.