- * // Add a custom plugin
- * flowDefaults.plugins.push(new MyPlugin());
- *
- * // Update the rules to also transpile `.mjs` files
- * if (!flowDefaults.module.rules[0].test) {
- * throw "Unexpected structure in generated webpack config";
- * }
- * flowDefaults.module.rules[0].test = /\.m?js$/
- *
- * // Include a custom JS in the entry point in addition to generated-flow-imports.js
- * if (typeof flowDefaults.entry.index != "string") {
- * throw "Unexpected structure in generated webpack config";
- * }
- * flowDefaults.entry.index = [flowDefaults.entry.index, "myCustomFile.js"];
- *
- * or add new configuration in the merge block.
- *
- * module.exports = merge(flowDefaults, {
- * mode: 'development',
- * devtool: 'inline-source-map'
- * });
- *
- */
From 7d08a9a77b1fa8b1303a81960a3906c79bcf1707 Mon Sep 17 00:00:00 2001
From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com>
Date: Tue, 23 Jul 2024 14:06:52 -0300
Subject: [PATCH 3/4] ci: configure integration tests
---
pom.xml | 111 ++++++++++++++++++
.../integration/AbstractViewTest.java | 106 +++++++++++++++++
2 files changed, 217 insertions(+)
create mode 100644 src/test/java/com/flowingcode/addons/simpletimer/integration/AbstractViewTest.java
diff --git a/pom.xml b/pom.xml
index 8da6b5e..b47d09a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -128,6 +128,34 @@
The tests use Chrome driver (see pom.xml for integration-tests profile) to run integration + * tests on a headless Chrome. If a property {@code test.use .hub} is set to true, {@code + * AbstractViewTest} will assume that the TestBench test is running in a CI environment. In order to + * keep the this class light, it makes certain assumptions about the CI environment (such as + * available environment variables). It is not advisable to use this class as a base class for you + * own TestBench tests. + * + *
To learn more about TestBench, visit Vaadin TestBench. + */ +public abstract class AbstractViewTest extends ParallelTest { + private static final int SERVER_PORT = 8080; + + private final String route; + + @Rule public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true); + + public AbstractViewTest() { + this(""); + } + + protected AbstractViewTest(String route) { + this.route = route; + } + + @BeforeClass + public static void setupClass() { + WebDriverManager.chromedriver().setup(); + } + + @Override + @Before + public void setup() throws Exception { + if (isUsingHub()) { + super.setup(); + } else { + setDriver(TestBench.createDriver(new ChromeDriver())); + } + getDriver().get(getURL(route)); + } + + /** + * Returns deployment host name concatenated with route. + * + * @return URL to route + */ + private static String getURL(String route) { + return String.format("http://%s:%d/%s", getDeploymentHostname(), SERVER_PORT, route); + } + + /** Property set to true when running on a test hub. */ + private static final String USE_HUB_PROPERTY = "test.use.hub"; + + /** + * Returns whether we are using a test hub. This means that the starter is running tests in + * Vaadin's CI environment, and uses TestBench to connect to the testing hub. + * + * @return whether we are using a test hub + */ + private static boolean isUsingHub() { + return Boolean.TRUE.toString().equals(System.getProperty(USE_HUB_PROPERTY)); + } + + /** + * If running on CI, get the host name from environment variable HOSTNAME + * + * @return the host name + */ + private static String getDeploymentHostname() { + return isUsingHub() ? System.getenv("HOSTNAME") : "localhost"; + } +} \ No newline at end of file From fb786ecc1c220e2869d9899cf1a68c9271093291 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:37:30 -0300 Subject: [PATCH 4/4] test: add simple integration tests --- .../integration/IntegrationCallables.java | 17 ++++++ .../integration/IntegrationView.java | 53 +++++++++++++++++ .../simpletimer/integration/SimpleIT.java | 57 +++++++++++++++++++ .../integration/SimpleTimerElement.java | 15 +++++ 4 files changed, 142 insertions(+) create mode 100644 src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationCallables.java create mode 100644 src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationView.java create mode 100644 src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleIT.java create mode 100644 src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleTimerElement.java diff --git a/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationCallables.java b/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationCallables.java new file mode 100644 index 0000000..c1122d0 --- /dev/null +++ b/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationCallables.java @@ -0,0 +1,17 @@ +package com.flowingcode.addons.simpletimer.integration; + +public interface IntegrationCallables { + + void setStartTime(Integer startTime); + + void setEndTime(Integer endTime); + + void start(); + + void pause(); + + void reset(); + + boolean isRunning(); + +} diff --git a/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationView.java b/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationView.java new file mode 100644 index 0000000..26386ce --- /dev/null +++ b/src/test/java/com/flowingcode/addons/simpletimer/integration/IntegrationView.java @@ -0,0 +1,53 @@ +package com.flowingcode.addons.simpletimer.integration; + +import com.flowingcode.vaadin.addons.simpletimer.SimpleTimer; +import com.vaadin.flow.component.ClientCallable; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.router.Route; + +@Route("/it") +public class IntegrationView extends Div implements IntegrationCallables { + + private SimpleTimer timer = new SimpleTimer(); + + public IntegrationView() { + add(timer); + } + + @Override + @ClientCallable + public void setStartTime(Integer startTime) { + timer.setStartTime(startTime); + } + + @Override + @ClientCallable + public void setEndTime(Integer endTime) { + timer.setEndTime(endTime); + } + + @Override + @ClientCallable + public void start() { + timer.start(); + } + + @Override + @ClientCallable + public void pause() { + timer.pause(); + } + + @Override + @ClientCallable + public void reset() { + timer.reset(); + } + + @Override + @ClientCallable + public boolean isRunning() { + return timer.isRunning(); + } + +} diff --git a/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleIT.java b/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleIT.java new file mode 100644 index 0000000..a18902b --- /dev/null +++ b/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleIT.java @@ -0,0 +1,57 @@ +package com.flowingcode.addons.simpletimer.integration; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport; +import org.junit.Test; + +public class SimpleIT extends AbstractViewTest implements HasRpcSupport { + + IntegrationCallables $server = createCallableProxy(IntegrationCallables.class); + + public SimpleIT() { + super("it"); + } + + private Double currentTime() { + return $(SimpleTimerElement.class).first().currentTime(); + } + + @Test + public void countDown() { + assertThat(currentTime(), nullValue()); + assertFalse($server.isRunning()); + + $server.setStartTime(1); + assertThat(currentTime(), equalTo(1.0)); + + $server.start(); + assertTrue($server.isRunning()); + double t0 = currentTime(); + double t1 = currentTime(); + assertThat(t0, lessThan(1.0)); + assertThat(t1, lessThan(t0)); + } + + @Test + public void countUp() { + assertThat(currentTime(), nullValue()); + assertFalse($server.isRunning()); + + $server.setEndTime(1); + assertThat(currentTime(), equalTo(0.0)); + + $server.start(); + assertTrue($server.isRunning()); + double t0 = currentTime(); + double t1 = currentTime(); + assertThat(t0, greaterThan(0.0)); + assertThat(t1, greaterThan(t0)); + } + +} diff --git a/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleTimerElement.java b/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleTimerElement.java new file mode 100644 index 0000000..a84b5b2 --- /dev/null +++ b/src/test/java/com/flowingcode/addons/simpletimer/integration/SimpleTimerElement.java @@ -0,0 +1,15 @@ +package com.flowingcode.addons.simpletimer.integration; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; +import java.util.Optional; + +@Element("simple-timer") +public class SimpleTimerElement extends TestBenchElement { + + Double currentTime() { + Number time = (Number) executeScript("return arguments[0].currentTime", this); + return Optional.ofNullable(time).map(Number::doubleValue).orElse(null); + } + +}