Skip to content

Commit

Permalink
Add TestMviLogger (#38)
Browse files Browse the repository at this point in the history
* Add TestMviLogger

* Use TestMviLogger in sample test
  • Loading branch information
matejdro authored Jan 13, 2025
1 parent 8a29f34 commit 6ee71e7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ktlint-gradle = "12.1.0"
android-library = "8.3.2"
maven-publish = "0.29.0"
ui = "1.7.1"
mockk = "1.13.11"
mvi-kotest = "0.0.2"

# its beeing used outside this file
Expand All @@ -43,7 +42,6 @@ koinAndroid = { module = "io.insert-koin:koin-android", version.ref = "koin-andr
koinAnnotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin-annotations" }
koinKspCompiler = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koin-annotations" }
koinCompose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin-compose" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
56 changes: 56 additions & 0 deletions mvi-kotest/src/main/kotlin/com/adidas/mvi/kotest/TestMviLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.adidas.mvi.kotest

import com.adidas.mvi.Loggable
import com.adidas.mvi.Logger

/**
* A fake implementation of [Logger] that remembers all logged entries, for use in tests.
*
* You can access list of all logged entries from [loggedTranformations] and [loggedIntents].
*/
public class TestMviLogger : Logger {
public val loggedTranformations: MutableList<LoggedTranformation> = ArrayList<LoggedTranformation>()
public val loggedIntents: MutableList<LoggedIntent> = ArrayList<LoggedIntent>()

override fun logFailedIntent(
intent: Loggable,
throwable: Throwable,
) {
loggedIntents += LoggedIntent.Failure(intent, throwable)
}

override fun logIntent(intent: Loggable) {
loggedIntents += LoggedIntent.Success(intent)
}

override fun logFailedTransformNewState(
transform: Loggable,
state: Loggable,
throwable: Throwable,
) {
loggedTranformations += LoggedTranformation.Failure(transform, state, throwable)
}

override fun logTransformedNewState(
transform: Loggable,
previousState: Loggable,
newState: Loggable,
) {
loggedTranformations += LoggedTranformation.Success(transform, previousState, newState)
}

public sealed class LoggedIntent() {
public abstract val intent: Loggable

public data class Success(override val intent: Loggable) : LoggedIntent()
public data class Failure(override val intent: Loggable, val throwable: Throwable) : LoggedIntent()
}

public sealed class LoggedTranformation() {
public abstract val transform: Loggable
public abstract val previousState: Loggable

public data class Success(override val transform: Loggable, override val previousState: Loggable, val newState: Loggable) : LoggedTranformation()
public data class Failure(override val transform: Loggable, override val previousState: Loggable, val throwable: Throwable) : LoggedTranformation()
}
}
3 changes: 1 addition & 2 deletions mvi-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@ dependencies {
ksp(libs.koinKspCompiler)

testImplementation(libs.kotestRunner)
testImplementation(libs.mockk)
testImplementation(libs.mviKotest)
testImplementation(project(":mvi-kotest"))
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.adidas.mvi.sample.login.viewmodel

import com.adidas.mvi.kotest.GivenViewModel
import com.adidas.mvi.kotest.TestMviLogger
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.UnconfinedTestDispatcher
Expand All @@ -19,7 +19,7 @@ class LoginViewModelTest : BehaviorSpec({

fun getViewModel(): LoginViewModel {
return LoginViewModel(
logger = mockk(relaxed = true),
logger = TestMviLogger(),
coroutineDispatcher = testCoroutineDispatcher
)
}
Expand Down Expand Up @@ -56,4 +56,4 @@ class LoginViewModelTest : BehaviorSpec({

}

})
})

0 comments on commit 6ee71e7

Please sign in to comment.