Repository: takahirom/roborazzi
Pull request #412 (2024-06-20 20:26:45)

Add unit test for WebAssets class
Follow up to #404.

Add missing unit test for `WebAssets` class


Patch:
diff --git a/include-build/roborazzi-gradle-plugin/build.gradle b/include-build/roborazzi-gradle-plugin/build.gradle
index 3d768fac..3db6cd01 100644
--- a/include-build/roborazzi-gradle-plugin/build.gradle
+++ b/include-build/roborazzi-gradle-plugin/build.gradle
@@ -25,6 +25,9 @@ dependencies {
   integrationTestDepImplementation libs.android.tools.build.gradle
   integrationTestDepImplementation libs.kotlin.gradle.plugin
   integrationTestDepImplementation libs.compose.gradle.plugin
+  testImplementation libs.kotlin.test
+  testImplementation libs.kotlin.test.junit
+  testImplementation libs.junit
 }
 
 sourceSets {
diff --git a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/WebAssets.kt b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/WebAssets.kt
index f5cc19f0..e78f7fdd 100644
--- a/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/WebAssets.kt
+++ b/include-build/roborazzi-gradle-plugin/src/main/java/io/github/takahirom/roborazzi/WebAssets.kt
@@ -4,10 +4,12 @@ import org.webjars.WebJarVersionLocator
 import java.io.File
 
 class WebAssets private constructor(private val webJarVersionLocator: WebJarVersionLocator) {
-  private val materializeCss = "materializecss"
-  private val materialIcons = "material-design-icons"
   private val webJarResource = "resources"
 
+  val assets = assetPathsMap.entries.flatMap {
+    assets -> assets.value.map { it.substringAfterLast("/") }
+  }
+
   fun writeToRoborazziReportsDir(reportDir: File) {
     writeLocalAssetsToRoborazziReportsDir(reportDir)
     writeWebJarAssetsToRoborazziReportsDir(reportDir)
@@ -22,16 +24,7 @@ class WebAssets private constructor(private val webJarVersionLocator: WebJarVers
   }
 
   private fun writeWebJarAssetsToRoborazziReportsDir(reportDir: File) {
-    mapOf(
-      materializeCss to listOf(
-        "css/materialize.min.css",
-        "js/materialize.min.js",
-      ),
-      materialIcons to listOf(
-        "material-icons.css",
-        "MaterialIcons-Regular.ttf",
-      )
-    ).forEach { (key, value) ->
+    assetPathsMap.forEach { (key, value) ->
       value.forEach { exactPath ->
         webJarVersionLocator.locate(key, exactPath)?.let {
           writeAssets(
@@ -71,6 +64,17 @@ class WebAssets private constructor(private val webJarVersionLocator: WebJarVers
 
   companion object {
 
+   private val assetPathsMap = mapOf(
+      "materializecss" to listOf(
+        "css/materialize.min.css",
+        "js/materialize.min.js",
+      ),
+      "material-design-icons" to listOf(
+        "material-icons.css",
+        "MaterialIcons-Regular.ttf",
+      )
+    )
+
     fun create(
       webJarVersionLocator: WebJarVersionLocator = WebJarVersionLocator()
     ): WebAssets = WebAssets(webJarVersionLocator)


Test patch:
diff --git a/include-build/roborazzi-gradle-plugin/src/test/java/io/github/takahirom/roborazzi/WebAssetsTest.kt b/include-build/roborazzi-gradle-plugin/src/test/java/io/github/takahirom/roborazzi/WebAssetsTest.kt
new file mode 100644
index 00000000..03419795
--- /dev/null
+++ b/include-build/roborazzi-gradle-plugin/src/test/java/io/github/takahirom/roborazzi/WebAssetsTest.kt
@@ -0,0 +1,33 @@
+package io.github.takahirom.roborazzi
+
+import junit.framework.TestCase.assertTrue
+import org.junit.Before
+import org.junit.Rule
+import org.junit.rules.TemporaryFolder
+import java.io.File
+import kotlin.test.Test
+
+class WebAssetsTest {
+
+  @get:Rule
+  val tmpDir: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()
+
+  private lateinit var sut: WebAssets
+
+  @Before
+  fun setUp() {
+    sut = WebAssets.create()
+  }
+
+  @Test
+  fun testWritesWebAssetsSuccessfullyToReportsDirectory() {
+    val reportDir = tmpDir.newFolder("roborazzi-reports")
+    val assetFiles = sut.assets.map { File(reportDir, "assets/$it") }
+
+   sut.writeToRoborazziReportsDir(reportDir)
+
+    assetFiles.forEach { file->
+      assertTrue(file.exists())
+    }
+  }
+}
\ No newline at end of file