Skip to content

Commit e9bf20a

Browse files
committed
test: implement client callable interface
1 parent 5c4623f commit e9bf20a

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

src/test/java/com/flowingcode/vaadin/addons/chipfield/integration/IntegrationView.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,68 @@
77
import com.vaadin.flow.component.html.Div;
88
import com.vaadin.flow.router.Route;
99

10+
import elemental.json.Json;
11+
1012
@Route("it")
11-
public class IntegrationView extends Div {
13+
public class IntegrationView extends Div implements IntegrationViewCallables {
1214

13-
private ChipField<String> field;
15+
public ChipField<String> field;
1416

1517
public IntegrationView() {
1618
setId("view");
1719
add(field = new ChipField<>("Field"));
1820
field.setItems("Lorem", "Ipsum");
1921
}
2022

23+
@Override
2124
@ClientCallable
22-
private void testCallable(boolean arg) {
25+
public void testCallable(boolean arg) {
2326
if (!arg) {
2427
throw new IllegalArgumentException();
2528
}
2629
}
2730

31+
@Override
2832
@ClientCallable
29-
private void allowAdditionalItems(boolean value) {
33+
public void allowAdditionalItems(boolean value) {
3034
field.setAllowAdditionalItems(value);
3135
}
3236

37+
@Override
3338
@ClientCallable
34-
private void setFieldReadOnly(boolean value) {
39+
public void setFieldReadOnly(boolean value) {
3540
field.setReadOnly(value);
3641
}
3742

43+
@Override
3844
@ClientCallable
39-
private void setFieldEnabled(boolean value) {
45+
public void setFieldEnabled(boolean value) {
4046
field.setEnabled(value);
4147
}
4248

49+
@Override
4350
@ClientCallable
44-
private void setValue(String... items) {
51+
public void setValue(String... items) {
4552
field.setValue(Arrays.asList(items));
4653
}
4754

55+
@Override
56+
@ClientCallable
57+
public void useNewItemHandler(boolean useHandler) {
58+
field.setNewItemHandler(useHandler ? Object::toString : null);
59+
}
60+
61+
@ClientCallable
62+
public void assertValue(String... items) {
63+
if (!field.getValue().equals(Arrays.asList(items))) {
64+
throw new AssertionError();
65+
}
66+
}
67+
68+
@Override
69+
@ClientCallable
70+
public JsonArrayList<String> getValue() {
71+
return JsonArrayList.createArray(field.getValue(), Json::create);
72+
}
73+
4874
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
public interface IntegrationViewCallables {
4+
5+
void testCallable(boolean arg);
6+
7+
void allowAdditionalItems(boolean value);
8+
9+
void setFieldReadOnly(boolean value);
10+
11+
void setFieldEnabled(boolean value);
12+
13+
void setValue(String... items);
14+
15+
void useNewItemHandler(boolean useHandler);
16+
17+
JsonArrayList<String> getValue();
18+
19+
}

src/test/java/com/flowingcode/vaadin/addons/chipfield/integration/ViewIT.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class ViewIT extends AbstractChipfieldTest {
1717
private static final String IPSUM = "Ipsum";
1818
private static final String ADDITIONAL = "Additional";
1919

20+
IntegrationViewCallables $server = createCallableProxy(IntegrationViewCallables.class);
21+
2022
@Test
2123
public void testUpgradedToCustomElement() {
2224
ChipFieldElement chipfield = $(ChipFieldElement.class).first();
@@ -25,72 +27,76 @@ public void testUpgradedToCustomElement() {
2527

2628
@Test
2729
public void testCallableSuccess() {
28-
// test that the callable mechanism worls
29-
call("testCallable", true);
30+
// test that the callable mechanism works
31+
$server.testCallable(true);
3032
}
3133

3234
@Test
3335
public void testCallableFailure() {
3436
// test that the callable mechanism detect failures
35-
assertThrows(RuntimeException.class, () -> call("testCallable", false));
37+
assertThrows(RuntimeException.class, () -> $server.testCallable(false));
3638
}
3739

3840
private Matcher<Collection<String>> isEqualTo(String... values) {
3941
return Matchers.equalTo(Arrays.asList(values));
4042
}
4143

42-
@Test
43-
public void testCallableFailure2() {
44-
// test that the callable mechanism detect failures
45-
assertThrows(RuntimeException.class, () -> call("testCallable"));
46-
}
47-
4844
@Test
4945
public void testSelectByText() {
5046
chipfield.selectByText(LOREM);
5147
assertThat(chipfield.getValue(), isEqualTo(LOREM));
48+
assertThat($server.getValue(), isEqualTo(LOREM));
5249

5350
chipfield.selectByText(IPSUM);
5451
assertThat(chipfield.getValue(), isEqualTo(LOREM, IPSUM));
52+
assertThat($server.getValue(), isEqualTo(LOREM, IPSUM));
5553

5654
chipfield.sendKeys(Keys.BACK_SPACE);
5755
assertThat(chipfield.getValue(), isEqualTo(LOREM));
56+
assertThat($server.getValue(), isEqualTo(LOREM));
5857

5958
chipfield.sendKeys(Keys.BACK_SPACE);
6059
assertThat(chipfield.getValue(), Matchers.empty());
60+
assertThat($server.getValue(), Matchers.empty());
6161
}
6262

6363

6464
@Test
6565
public void testAdditionalItemEnabled() {
66-
call("allowAdditionalItems", true);
66+
$server.allowAdditionalItems(true);
67+
$server.useNewItemHandler(true);
6768

6869
chipfield.sendKeys(ADDITIONAL, Keys.ENTER);
6970
assertThat(chipfield.getValue(), isEqualTo(ADDITIONAL));
71+
assertThat($server.getValue(), isEqualTo(ADDITIONAL));
7072

7173
chipfield.sendKeys(LOREM, Keys.ENTER);
7274
assertThat(chipfield.getValue(), isEqualTo(ADDITIONAL, LOREM));
75+
assertThat($server.getValue(), isEqualTo(ADDITIONAL, LOREM));
7376
}
7477

7578
@Test
7679
public void testAdditionalItemDisabled() {
7780
chipfield.sendKeys("Additional", Keys.ENTER);
7881
assertThat(chipfield.getValue(), Matchers.empty());
82+
assertThat($server.getValue(), Matchers.empty());
7983
}
8084

8185
@Test
8286
public void testReadOnly() {
83-
8487
chipfield.selectByText(LOREM);
8588
assertThat(chipfield.getValue(), isEqualTo(LOREM));
89+
assertThat($server.getValue(), isEqualTo(LOREM));
8690

87-
91+
$server.setFieldReadOnly(true);
8892
chipfield.sendKeys(Keys.BACK_SPACE);
8993
assertThat(chipfield.getValue(), isEqualTo(LOREM));
94+
assertThat($server.getValue(), isEqualTo(LOREM));
9095

91-
96+
$server.allowAdditionalItems(true);
9297
chipfield.sendKeys(ADDITIONAL, Keys.ENTER);
9398
assertThat(chipfield.getValue(), isEqualTo(LOREM));
99+
assertThat($server.getValue(), isEqualTo(LOREM));
94100
}
95101

96102

0 commit comments

Comments
 (0)