Skip to content

Commit

Permalink
[javalinvue] Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
tipsy committed Jun 30, 2019
1 parent 773f2fa commit d6bd5bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/javalin/plugin/rendering/vue/JavalinVue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object JavalinVue {
val vueRootPath by lazy { if (vueRootUri.scheme == "jar") fileSystem.getPath("/vue") else Paths.get(localPath) }
val fileSystem by lazy { FileSystems.newFileSystem(vueRootUri, emptyMap<String, Any>()) }

fun createLayout() = layout().replace("@componentRegistration", "@routeParams${components()}") // add params anchor for later
fun createLayout() = layout().replace("@componentRegistration", "@serverState${components()}") // add state anchor for later
fun layout() = paths.find { it.endsWith("vue/layout.html") }!!.readText()
fun components() = paths.filter { it.toString().endsWith(".vue") }.joinToString("") { it.readText() }
fun walkPaths() = Files.walk(vueRootPath, 10).collect(Collectors.toSet())
Expand All @@ -53,6 +53,6 @@ class VueComponent(val component: String) : Handler {
JavalinVue.paths = if (ctx.isLocalhost()) JavalinVue.walkPaths() else JavalinVue.cachedPaths
val view = if (ctx.isLocalhost()) JavalinVue.createLayout() else JavalinVue.cachedLayout
val state = JavalinVue.getState(ctx, JavalinVue.stateFunction.invoke(ctx))
ctx.html(view.replace("@routeParams", state).replace("@routeComponent", component)) // insert current route component
ctx.html(view.replace("@serverState", state).replace("@routeComponent", component)) // insert current route component
}
}
33 changes: 21 additions & 12 deletions src/test/java/io/javalin/TestJavalinVue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,31 @@ class TestJavalinVue {
private val state = State(User("tipsy", "tipsy@tipsy.tipsy"), Role("Maintainer"))

@Test
fun `hello vue world`() = TestUtil.test { app, http ->
fun `vue component with state`() = TestUtil.test { app, http ->
JavalinVue.localPath = "src/test/resources/vue"
JavalinVue.stateFunction = { ctx -> state }
app.get("/vue/:my-param", VueComponent("<test-component></test-component>"))
val stateResponse = http.getBody("/vue/test-path-param?qp=test-query-param")
assertThat(stateResponse).contains("""pathParams: {"my-param":"test-path-param"}""")
assertThat(stateResponse).contains("""queryParams: {"qp":["test-query-param"]}""")
assertThat(stateResponse).contains("""Vue.component("test-component", {template: "#test-component"});""")
assertThat(stateResponse).contains("""state: {"user":{"name":"tipsy","email":"tipsy@tipsy.tipsy"},"role":{"name":"Maintainer"}}""")
assertThat(stateResponse).contains("<body><test-component></test-component></body>")
val res = http.getBody("/vue/test-path-param?qp=test-query-param")
assertThat(res).contains("""
| Vue.prototype.${"$"}javalin = {
| pathParams: {"my-param":"test-path-param"},
| queryParams: {"qp":["test-query-param"]},
| state: {"user":{"name":"tipsy","email":"tipsy@tipsy.tipsy"},"role":{"name":"Maintainer"}}
| }""".trimMargin())
assertThat(res).contains("""Vue.component("test-component", {template: "#test-component"});""")
assertThat(res).contains("<body><test-component></test-component></body>")
}

@Test
fun `vue component without state`() = TestUtil.test { app, http ->
JavalinVue.localPath = "src/test/resources/vue"
JavalinVue.stateFunction = { ctx -> mapOf<String, String>() }
app.get("/no-state", VueComponent("<test-component></test-component>"))
val noStateResponse = http.getBody("/no-state")
assertThat(noStateResponse).contains("""pathParams: {}""")
assertThat(noStateResponse).contains("""queryParams: {}""")
assertThat(noStateResponse).contains("""state: {}""")
app.get("/no-state", VueComponent("<other-component></other-component>"))
val res = http.getBody("/no-state")
assertThat(res).contains("""pathParams: {}""")
assertThat(res).contains("""queryParams: {}""")
assertThat(res).contains("""state: {}""")
assertThat(res).contains("<body><other-component></other-component></body>")
}

}

0 comments on commit d6bd5bb

Please sign in to comment.