|
| 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