-
Notifications
You must be signed in to change notification settings - Fork 77
feat: add signal bind methods to crud component #8739
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule flow
added at
edd0fa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...-parent/vaadin-crud-flow/src/test/java/com/vaadin/flow/component/crud/CrudSignalTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * Copyright 2000-2026 Vaadin Ltd. | ||
| * | ||
| * This program is available under Vaadin Commercial License and Service Terms. | ||
| * | ||
| * See {@literal <https://vaadin.com/commercial-license-and-service-terms>} for the full | ||
| * license. | ||
| */ | ||
| package com.vaadin.flow.component.crud; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.component.UI; | ||
| import com.vaadin.flow.component.grid.Grid; | ||
| import com.vaadin.flow.component.html.Div; | ||
| import com.vaadin.flow.data.provider.DataCommunicator; | ||
| import com.vaadin.flow.data.provider.DataKeyMapper; | ||
| import com.vaadin.flow.data.provider.DataProvider; | ||
| import com.vaadin.flow.signals.BindingActiveException; | ||
| import com.vaadin.flow.signals.local.ValueSignal; | ||
| import com.vaadin.tests.AbstractSignalsUnitTest; | ||
|
|
||
| public class CrudSignalTest extends AbstractSignalsUnitTest { | ||
|
|
||
| private Crud<Thing> crud; | ||
| private ValueSignal<Boolean> signal; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| Grid<Thing> grid = Mockito.spy(new Grid<>()); | ||
| Mockito.when(grid.getDataProvider()) | ||
| .thenReturn(Mockito.mock(DataProvider.class)); | ||
| DataCommunicator<Thing> communicator = Mockito | ||
| .mock(DataCommunicator.class); | ||
| Mockito.when(grid.getDataCommunicator()).thenReturn(communicator); | ||
| DataKeyMapper<Thing> keyMapper = Mockito.mock(DataKeyMapper.class); | ||
| Mockito.when(communicator.getKeyMapper()).thenReturn(keyMapper); | ||
|
|
||
| crud = new Crud<>(Thing.class, grid, new ThingEditor()); | ||
| signal = new ValueSignal<>(false); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| if (crud != null && crud.isAttached()) { | ||
| crud.removeFromParent(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void bindDirty_signalBound_propertySync() { | ||
| crud.bindDirty(signal); | ||
| UI.getCurrent().add(crud); | ||
|
|
||
| signal.set(true); | ||
| // The dirty state is pushed via executeJs, so we verify the signal | ||
| // value reflects correctly | ||
| Assert.assertTrue(signal.peek()); | ||
|
|
||
| signal.set(false); | ||
| Assert.assertFalse(signal.peek()); | ||
| } | ||
|
|
||
| @Test | ||
| public void bindDirty_notAttached_noEffect() { | ||
| crud.bindDirty(signal); | ||
|
|
||
| // Signal changes should not throw when not attached | ||
| signal.set(true); | ||
| Assert.assertTrue(signal.peek()); | ||
| } | ||
|
|
||
| @Test | ||
| public void bindDirty_detachAndReattach() { | ||
| crud.bindDirty(signal); | ||
| UI.getCurrent().add(crud); | ||
|
|
||
| signal.set(true); | ||
| Assert.assertTrue(signal.peek()); | ||
|
|
||
| crud.removeFromParent(); | ||
| signal.set(false); | ||
|
|
||
| UI.getCurrent().add(crud); | ||
| Assert.assertFalse(signal.peek()); | ||
| } | ||
|
|
||
| @Test(expected = BindingActiveException.class) | ||
| public void bindDirty_setWhileBound_throws() { | ||
| crud.bindDirty(signal); | ||
| UI.getCurrent().add(crud); | ||
|
|
||
| crud.setDirty(true); | ||
| } | ||
|
|
||
| @Test(expected = BindingActiveException.class) | ||
| public void bindDirty_doubleBind_throws() { | ||
| crud.bindDirty(signal); | ||
| crud.bindDirty(new ValueSignal<>(true)); | ||
| } | ||
|
|
||
| @Test(expected = NullPointerException.class) | ||
| public void bindDirty_nullSignal_throwsNPE() { | ||
| crud.bindDirty(null); | ||
| } | ||
|
|
||
| public static class Thing { | ||
| String name; | ||
| } | ||
|
|
||
| private static class ThingEditor implements CrudEditor<Thing> { | ||
| private Thing item; | ||
|
|
||
| @Override | ||
| public void setItem(Thing item, boolean validate) { | ||
| this.item = item; | ||
| } | ||
|
|
||
| @Override | ||
| public Thing getItem() { | ||
| return item; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| } | ||
|
|
||
| @Override | ||
| public boolean validate() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeItemChanges() { | ||
| } | ||
|
|
||
| @Override | ||
| public Component getView() { | ||
| return new Div(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write proper tests that verify that the executeJs call happens.