Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix for #271 : "this.selectedItem is undefined" when adding new items to ChosenListBox and calling update() #280

Merged
merged 4 commits into from
Oct 16, 2015
Merged
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
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