Skip to content

Commit 2baca4a

Browse files
committed
test modules preloads
1 parent 411e9b1 commit 2baca4a

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

build.sc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ trait Testy extends TestModule.Munit with FormatFix {
5555

5656
}
5757

58-
object routes extends FormatFix {
58+
object routes extends FormatFixPublish {
5959

60-
def scalaVersion: T[String] = sjsls.scalaVersion
61-
62-
def artifactName = "scala-live-server-routes"
60+
def scalaVersion: T[String] = "3.3.3" // Latest LTS
6361

6462
def ivyDeps = Agg(
6563
ivy"org.http4s::http4s-core:${V.http4sVersion}",
@@ -69,15 +67,17 @@ object routes extends FormatFix {
6967
ivy"com.outr::scribe-cats::3.15.0"
7068
)
7169

70+
override def artifactName = "frontend-routes"
71+
7272
object test extends Testy with ScalaTests{
7373
def ivyDeps = super.ivyDeps() ++ sjsls.ivyDeps()
7474
}
7575

7676
}
7777

78-
object sjsls extends FormatFix {
78+
object sjsls extends FormatFixPublish {
7979

80-
override def scalaVersion = "3.5.0-RC4"
80+
override def scalaVersion = "3.4.2" // Latest
8181

8282
def ivyDeps = super.ivyDeps() ++ Seq(
8383
ivy"org.http4s::http4s-ember-server::${V.http4sVersion}",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package io.github.quafadas.sjsls
2+
3+
import munit.CatsEffectSuite
4+
import cats.effect.kernel.Ref
5+
import cats.effect.IO
6+
import java.time.ZonedDateTime
7+
import scribe.Level
8+
import org.http4s.Request
9+
import org.http4s.syntax.literals.uri
10+
import org.http4s.Status
11+
12+
class SpaRoutesSuite extends CatsEffectSuite:
13+
14+
val setupRef = ResourceFunFixture {
15+
for
16+
logger <- IO(scribe.cats[IO]).toResource
17+
modules <- Ref[IO].of(Map("main.js" -> "mainHash", "internal-xxxx.js" -> "internalHash")).toResource
18+
tempDir = os.temp.dir()
19+
tempFile = tempDir / "index.html"
20+
_ = os.write(tempFile, """<head><title>Test</title></head><body><h1>Test</h1></body>""")
21+
yield (logger, modules, tempDir)
22+
}
23+
24+
override def beforeAll(): Unit =
25+
scribe
26+
.Logger
27+
.root
28+
.clearHandlers()
29+
.clearModifiers()
30+
.withHandler(minimumLevel = Some(Level.get("info").get))
31+
.replace()
32+
33+
setupRef.test("spa routes have preloads injected for the easy case") {
34+
(logger, modules, _) =>
35+
val spaRoute = buildSpaRoute(None, modules, ZonedDateTime.now())(logger)
36+
val resp = spaRoute(Request[IO](uri = uri"/anything")).getOrElse(fail("No response"))
37+
for
38+
body <- resp.map(_.bodyText.compile.string)
39+
_ <- assertIOBoolean(resp.map(_.status == Status.Ok))
40+
body <- resp.flatMap(_.bodyText.compile.string)
41+
yield
42+
println(body)
43+
assert(body.contains("""<link rel="modulepreload" href="internal-xxxx.js?h=internalHash" />"""))
44+
assert(!body.contains("mainHash"))
45+
end for
46+
47+
}
48+
49+
setupRef.test("spa routes have preloads injected for the styles case") {
50+
(logger, modules, _) =>
51+
val spaRoute = buildSpaRoute(
52+
Some(IndexHtmlConfig.StylesOnly(fs2.io.file.Path.apply("doestmatter"))),
53+
modules,
54+
ZonedDateTime.now()
55+
)(logger)
56+
val resp = spaRoute(Request[IO](uri = uri"/anything")).getOrElse(fail("No response"))
57+
for
58+
body <- resp.map(_.bodyText.compile.string)
59+
_ <- assertIOBoolean(resp.map(_.status == Status.Ok))
60+
body <- resp.flatMap(_.bodyText.compile.string)
61+
yield
62+
println(body)
63+
assert(body.contains("""<link rel="modulepreload" href="internal-xxxx.js?h=internalHash" />"""))
64+
assert(!body.contains("""mainHash"""))
65+
end for
66+
67+
}
68+
69+
setupRef.test("spa routes have preloads injected for the index.html case") {
70+
(logger, modules, tempDir) =>
71+
val spaRoute = buildSpaRoute(
72+
Some(IndexHtmlConfig.StylesOnly(fs2.io.file.Path.apply(tempDir.toString))),
73+
modules,
74+
ZonedDateTime.now()
75+
)(logger)
76+
val resp = spaRoute(Request[IO](uri = uri"/anything")).getOrElse(fail("No response"))
77+
for
78+
body <- resp.map(_.bodyText.compile.string)
79+
_ <- assertIOBoolean(resp.map(_.status == Status.Ok))
80+
body <- resp.flatMap(_.bodyText.compile.string)
81+
yield
82+
println(body)
83+
assert(!body.contains("mainHash"))
84+
assert(body.contains("""<link rel="modulepreload" href="internal-xxxx.js?h=internalHash" />"""))
85+
end for
86+
87+
}
88+
89+
end SpaRoutesSuite

0 commit comments

Comments
 (0)