|
| 1 | +/* |
| 2 | + * Copyright 2000-2026 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.component; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import com.vaadin.flow.dom.SignalsUnitTest; |
| 21 | +import com.vaadin.signals.BindingActiveException; |
| 22 | +import com.vaadin.signals.ValueSignal; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertThrows; |
| 26 | + |
| 27 | +public class HasHelperBindHelperTextTest extends SignalsUnitTest { |
| 28 | + |
| 29 | + @Tag("div") |
| 30 | + public static class HasHelperComponent extends Component |
| 31 | + implements HasHelper { |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void bindHelperText_updatesPropertyOnSignalChange() { |
| 36 | + HasHelperComponent c = new HasHelperComponent(); |
| 37 | + UI.getCurrent().add(c); |
| 38 | + |
| 39 | + ValueSignal<String> signal = new ValueSignal<>(""); |
| 40 | + c.bindHelperText(signal); |
| 41 | + |
| 42 | + signal.value("help-1"); |
| 43 | + assertEquals("help-1", c.getElement().getProperty("helperText")); |
| 44 | + |
| 45 | + signal.value("help-2"); |
| 46 | + assertEquals("help-2", c.getElement().getProperty("helperText")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void bindHelperText_setHelperTextWhileBindingActive_throws() { |
| 51 | + HasHelperComponent c = new HasHelperComponent(); |
| 52 | + UI.getCurrent().add(c); |
| 53 | + |
| 54 | + ValueSignal<String> signal = new ValueSignal<>("initial"); |
| 55 | + c.bindHelperText(signal); |
| 56 | + |
| 57 | + assertThrows(BindingActiveException.class, |
| 58 | + () -> c.setHelperText("manual")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void bindHelperText_unbindWithNull_stopsUpdates() { |
| 63 | + HasHelperComponent c = new HasHelperComponent(); |
| 64 | + UI.getCurrent().add(c); |
| 65 | + |
| 66 | + ValueSignal<String> signal = new ValueSignal<>("a"); |
| 67 | + c.bindHelperText(signal); |
| 68 | + assertEquals("a", c.getElement().getProperty("helperText")); |
| 69 | + |
| 70 | + c.bindHelperText(null); |
| 71 | + signal.value("b"); |
| 72 | + |
| 73 | + // After unbinding, value should remain as before |
| 74 | + assertEquals("a", c.getElement().getProperty("helperText")); |
| 75 | + } |
| 76 | +} |
0 commit comments