Skip to content

Commit

Permalink
Add server side unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Anisimov committed Oct 2, 2019
1 parent b01f826 commit 52e2545
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
35 changes: 15 additions & 20 deletions flow-server/src/main/java/com/vaadin/flow/shared/ui/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class Dependency implements Serializable {
public static final String KEY_URL = "url";
public static final String KEY_TYPE = "type";
public static final String KEY_LOAD_MODE = "mode";
public static final String KEY_EXPRESSION = "expression";
public static final String KEY_CONTENTS = "contents";

/**
Expand All @@ -61,7 +60,6 @@ public static boolean contains(String value) {
private final Type type;
private final String url;
private final LoadMode loadMode;
private final String expression;

/**
* Creates a new dependency of the given type, to be loaded from the given
Expand All @@ -87,33 +85,35 @@ public Dependency(Type type, String url, LoadMode loadMode) {
if (url == null) {
throw new IllegalArgumentException("url cannot be null");
}
assert type != null;
this.type = Objects.requireNonNull(type);

this.type = type;
if (type.equals(Type.JS_MODULE)) {
if (type.equals(Type.JS_MODULE) || type.equals(Type.DYNAMIC_IMPORT)) {
this.url = url;
} else {
this.url = SharedUtil.prefixIfRelative(url,
ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
}
this.loadMode = loadMode;
this.expression = null;
}

/**
* Creates a new dependency of the given type, to be loaded using JS
* expression which is supposed to return a Promise.
* <p>
* The created instance dependency mode is {@link LoadMode#LAZY}.
*
* @param type
* the type of the dependency, not {@code null}
* @param expression
* the JS expression to load the dependency, not {@code null}
*/
public Dependency(Type type, String expression) {
this.type = Objects.requireNonNull(type);
this.expression = Objects.requireNonNull(expression);
this.loadMode = LoadMode.LAZY;
this.url = null;
// It's important that the load mode of the dependency is Lazy because
// any other mode is not sent to the client at all when it's added at
// the initial request: it's processed by the bootstrap handler via
// adding an element into the document head right away (no client side
// processing is involved).
this(type, expression, LoadMode.LAZY);
}

/**
Expand Down Expand Up @@ -151,20 +151,15 @@ public LoadMode getLoadMode() {
*/
public JsonObject toJson() {
JsonObject jsonObject = Json.createObject();
if (url != null) {
jsonObject.put(KEY_URL, url);
}
jsonObject.put(KEY_URL, url);
jsonObject.put(KEY_TYPE, type.name());
jsonObject.put(KEY_LOAD_MODE, loadMode.name());
if (expression != null) {
jsonObject.put(KEY_EXPRESSION, expression);
}
return jsonObject;
}

@Override
public int hashCode() {
return Objects.hash(type, url, loadMode, expression);
return Objects.hash(type, url, loadMode);
}

@Override
Expand All @@ -177,13 +172,13 @@ public boolean equals(Object o) {
}
Dependency that = (Dependency) o;
return type == that.type && loadMode == that.loadMode
&& Objects.equals(url, that.url)
&& Objects.equals(expression, that.expression);
&& Objects.equals(url, that.url);

}

@Override
public String toString() {
return "Dependency [type=" + type + ", url=" + url + ", loadMode="
+ loadMode + ", expression: " + expression + "]";
+ loadMode + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.vaadin.flow.component.internal;

import java.util.Collection;

import net.jcip.annotations.NotThreadSafe;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -23,6 +25,7 @@
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.page.Page;
import com.vaadin.flow.component.page.Page.ExecutionCanceler;
import com.vaadin.flow.shared.ui.Dependency;
import com.vaadin.tests.util.MockUI;

@NotThreadSafe
Expand Down Expand Up @@ -86,6 +89,17 @@ public void testJavaScriptExecutionTooLateCancel() {
Assert.assertFalse(executeJavaScript.cancelExecution());
}

@Test
public void addDynamicImport_dynamicDependencyIsAvaialbleViaGetPendingSendToClient() {
page.addDynamicImport("foo");

DependencyList list = ui.getInternals().getDependencyList();
Collection<Dependency> dependencies = list.getPendingSendToClient();
Assert.assertEquals(1, dependencies.size());
Dependency dependency = dependencies.iterator().next();
Assert.assertEquals("foo", dependency.getUrl());
}

private long countPendingInvocations() {
return ui.getInternals().getPendingJavaScriptInvocations().count();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,67 @@

package com.vaadin.flow.shared.ui;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

import com.vaadin.flow.shared.ui.Dependency;
import com.vaadin.flow.shared.ui.LoadMode;

import elemental.json.JsonObject;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
* @author Vaadin Ltd
* @since 1.0.
*/
public class DependencyTest {

@Test
public void checkJsonSerialization() {
Dependency dependency = new Dependency(Dependency.Type.HTML_IMPORT, "url", LoadMode.INLINE);
public void checkJsonSerialization_3ArgsCTor() {
Dependency dependency = new Dependency(Dependency.Type.HTML_IMPORT,
"url", LoadMode.INLINE);

assertDependency(dependency);
}

@Test
public void dynamicDependency_hasLazyMode() {
Dependency dependency = new Dependency(Dependency.Type.DYNAMIC_IMPORT,
"foo");

// It's important that the load mode of the dependency is Lazy because
// any other mode is not sent to the client at all when it's added at
// the initial request: it's processed by the bootstrap handler via
// adding an element into the document head right away (no client side
// processing is involved).
assertThat(dependency.getLoadMode(),
CoreMatchers.equalTo(LoadMode.LAZY));
}

@Test
public void checkJsonSerialization_2ArgsCTor() {
Dependency dependency = new Dependency(Dependency.Type.DYNAMIC_IMPORT,
"foo");

assertDependency(dependency);

}

private void assertDependency(Dependency dependency) {
JsonObject dependencyJson = dependency.toJson();

assertThat("No contents should be present in json now",
dependencyJson.hasKey(Dependency.KEY_CONTENTS), is(false));
assertThat("Dependency type should match corresponding enum name in pojo",
dependencyJson.getString(Dependency.KEY_TYPE), is(dependency.getType().name()));
assertThat(
"Dependency type should match corresponding enum name in pojo",
dependencyJson.getString(Dependency.KEY_TYPE),
is(dependency.getType().name()));
assertThat("Dependency url should match corresponding url in pojo",
dependencyJson.getString(Dependency.KEY_URL), is(dependency.getUrl()));
assertThat("Dependency load mode should match corresponding enum name in pojo",
dependencyJson.getString(Dependency.KEY_LOAD_MODE), is(dependency.getLoadMode().name()));
dependencyJson.getString(Dependency.KEY_URL),
is(dependency.getUrl()));
assertThat(
"Dependency load mode should match corresponding enum name in pojo",
dependencyJson.getString(Dependency.KEY_LOAD_MODE),
is(dependency.getLoadMode().name()));
}

}

0 comments on commit 52e2545

Please sign in to comment.