Skip to content

Commit

Permalink
test: WritableUtils to simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Feb 18, 2025
1 parent 208a8df commit ade962b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ class PreactViewRenderSpec extends Specification {
void "views can be rendered with basic props and no request"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).get()

then:
result.contains("/static/client.preact.js")
Expand All @@ -40,10 +37,7 @@ class PreactViewRenderSpec extends Specification {

when:
Writable writable = renderer.render("App", TestProps.basic, req)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).get()

then:
result.contains("/static/client.preact.js")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ class ReactViewRenderSpec extends Specification {
void "views can be rendered with basic props"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).get()

then:
result.contains("Hello there")
Expand All @@ -38,10 +35,7 @@ class ReactViewRenderSpec extends Specification {

when:
Writable writable = renderer.render("App", TestProps.basic, req)
String result = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String result = WritableUtils.writableToString(writable).get()

then:
result.contains("/static/client.js")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ class SandboxReactRenderSpec extends Specification {
void "views can be rendered with sandboxing enabled"() {
when:
Writable writable = renderer.render("App", TestProps.basic, null)
String resultAsString = new StringWriter().with {
writable.writeTo(it)
it.toString()
}
String resultAsString = WritableUtils.writableToString(writable).get()

String dataJSON = resultAsString.find(~/var Micronaut = (\{[^;]+});/).replace("var Micronaut = ", "")
def data = new JsonSlurper().parseText(dataJSON)
Expand All @@ -37,10 +34,7 @@ class SandboxReactRenderSpec extends Specification {
void "host types are inaccessible with the sandbox enabled"() {
when:
Writable writable = renderer.render("App", TestProps.triggerSandbox, null)
new StringWriter().with {
writable.writeTo(it)
it.toString()
}
WritableUtils.writableToString(writable)

then:
def t = thrown(MessageBodyException)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.micronaut.views.react;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.io.Writable;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Optional;

public final class WritableUtils {
private WritableUtils() {
}

@NonNull
public static Optional<String> writableToString(@NonNull Writable writable) {
try {
StringWriter stringWriter = new StringWriter();
writable.writeTo(stringWriter);
return Optional.of(stringWriter.toString());
} catch (IOException e) {
return Optional.empty();
}
}
}

0 comments on commit ade962b

Please sign in to comment.