Skip to content

Commit

Permalink
Add unit test for WebAssets class (#412)
Browse files Browse the repository at this point in the history
* Add unit test for WebAssets class

* Make asset files map available for reuse

* Make assetPathsMap an internal variable

* Reuse asset paths mapping from sut in the test class

* Use private instead of internal

---------

Co-authored-by: Takahiro Menju <[email protected]>
  • Loading branch information
eyedol and takahirom authored Jul 5, 2024
1 parent 5ed8c0a commit 6a13336
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
3 changes: 3 additions & 0 deletions include-build/roborazzi-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}
}

0 comments on commit 6a13336

Please sign in to comment.