-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for component module (#16)
* add unit test for Flow.toValue() * add unit test for LifecycleCoroutineScope * add test module for decompose testing && write component tests * add component view model test * add run test on CI
- Loading branch information
1 parent
fe0ff00
commit 73b08c0
Showing
18 changed files
with
2,526 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...onents/src/commonTest/kotlin/ru/vladislavsumin/core/decompose/components/ComponentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ru.vladislavsumin.core.decompose.components | ||
|
||
import kotlinx.coroutines.test.runTest | ||
import ru.vladislavsumin.core.decompose.test.BaseComponentTest | ||
import ru.vladislavsumin.core.decompose.test.setMain | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertNotSame | ||
import kotlin.test.assertSame | ||
|
||
class ComponentTest : BaseComponentTest() { | ||
@Test | ||
fun testComponentRecreateFull() = runTest { | ||
setMain() | ||
val component = TestComponent(context) | ||
val viewModel = component.testViewModel | ||
|
||
recreateContext() | ||
|
||
val component2 = TestComponent(context) | ||
val viewModel2 = component2.testViewModel | ||
|
||
assertNotSame(viewModel, viewModel2) | ||
assertNotEquals(viewModel.testSaveableFlow.value, viewModel2.testSaveableFlow.value) | ||
} | ||
|
||
@Test | ||
fun testComponentRecreateProcessDeath() = runTest { | ||
setMain() | ||
val component = TestComponent(context) | ||
val viewModel = component.testViewModel | ||
|
||
recreateContext(RecreateContextType.ProcessDeath) | ||
|
||
val component2 = TestComponent(context) | ||
val viewModel2 = component2.testViewModel | ||
|
||
assertNotSame(viewModel, viewModel2) | ||
assertEquals(viewModel.testSaveableFlow.value, viewModel2.testSaveableFlow.value) | ||
} | ||
|
||
@Test | ||
fun testComponentRecreateConfigurationChange() = runTest { | ||
setMain() | ||
val component = TestComponent(context) | ||
val viewModel = component.testViewModel | ||
|
||
recreateContext(RecreateContextType.ConfigurationChange) | ||
|
||
val component2 = TestComponent(context) | ||
val viewModel2 = component2.testViewModel | ||
|
||
assertSame(viewModel, viewModel2) | ||
assertEquals(viewModel.testSaveableFlow.value, viewModel2.testSaveableFlow.value) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/commonTest/kotlin/ru/vladislavsumin/core/decompose/components/ComponentViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ru.vladislavsumin.core.decompose.components | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import kotlinx.coroutines.test.runTest | ||
import ru.vladislavsumin.core.decompose.test.BaseComponentTest | ||
import ru.vladislavsumin.core.decompose.test.setMain | ||
import kotlin.test.Test | ||
import kotlin.test.assertFailsWith | ||
|
||
class ComponentViewModelTest : BaseComponentTest() { | ||
@Test | ||
fun testComponentWrongDoubleViewModelCreation() = runTest { | ||
setMain() | ||
|
||
class TestComponent(context: ComponentContext) : Component(context) { | ||
val viewModel = viewModel { | ||
TestViewModel() | ||
TestViewModel() | ||
} | ||
} | ||
|
||
assertFailsWith(WrongViewModelUsageException::class) { | ||
TestComponent(context) | ||
} | ||
} | ||
|
||
@Test | ||
fun testComponentWrongPlaceToCreateViewModel() = runTest { | ||
setMain() | ||
|
||
assertFailsWith(WrongViewModelUsageException::class) { | ||
TestViewModel() | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...onents/src/commonTest/kotlin/ru/vladislavsumin/core/decompose/components/TestComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.vladislavsumin.core.decompose.components | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
|
||
class TestComponent(context: ComponentContext) : Component(context) { | ||
val testViewModel = viewModel { TestViewModel() } | ||
} |
7 changes: 7 additions & 0 deletions
7
...onents/src/commonTest/kotlin/ru/vladislavsumin/core/decompose/components/TestViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.vladislavsumin.core.decompose.components | ||
|
||
private var counter = 0 | ||
|
||
class TestViewModel : ViewModel() { | ||
val testSaveableFlow = saveableStateFlow("KEY") { counter++ } | ||
} |
66 changes: 66 additions & 0 deletions
66
...rc/commonTest/kotlin/ru/vladislavsumin/core/decompose/components/utils/FlowToValueTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ru.vladislavsumin.core.decompose.components.utils | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.test.runCurrent | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class FlowToValueTest { | ||
@Test | ||
fun testSimpleValue() = runTest { | ||
val flow = MutableStateFlow(0) | ||
val value = flow.asValue(this) | ||
assertEquals(0, value.value) | ||
} | ||
|
||
@Test | ||
fun testChangeSimpleValue() = runTest { | ||
val flow = MutableStateFlow(0) | ||
val value = flow.asValue(this) | ||
flow.value = 2 | ||
assertEquals(2, value.value) | ||
} | ||
|
||
@Test | ||
fun testSubscribeEmitCurrent() = runTest { | ||
val flow = MutableStateFlow(0) | ||
val value = flow.asValue(this) | ||
var data = -1 | ||
val cancellation = value.subscribe { data = it } | ||
runCurrent() | ||
cancellation.cancel() | ||
assertEquals(0, data) | ||
} | ||
|
||
@Test | ||
fun testSubscribeEmitSequence() = runTest { | ||
val flow = MutableStateFlow(0) | ||
val value = flow.asValue(this) | ||
var data = mutableListOf<Int>() | ||
val cancellation = value.subscribe { data += it } | ||
runCurrent() | ||
|
||
flow.value = 1 | ||
runCurrent() | ||
|
||
cancellation.cancel() | ||
|
||
assertEquals(listOf(0, 1), data) | ||
} | ||
|
||
@Test | ||
fun testSubscribeNotEmitAfterCancel() = runTest { | ||
val flow = MutableStateFlow(0) | ||
val value = flow.asValue(this) | ||
var data = mutableListOf<Int>() | ||
val cancellation = value.subscribe { data += it } | ||
runCurrent() | ||
|
||
cancellation.cancel() | ||
flow.value = 1 | ||
runCurrent() | ||
|
||
assertEquals(listOf(0), data) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...t/kotlin/ru/vladislavsumin/core/decompose/components/utils/LifecycleCoroutineScopeTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru.vladislavsumin.core.decompose.components.utils | ||
|
||
import com.arkivanov.essenty.lifecycle.LifecycleRegistry | ||
import com.arkivanov.essenty.lifecycle.create | ||
import com.arkivanov.essenty.lifecycle.destroy | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.test.runTest | ||
import ru.vladislavsumin.core.decompose.test.setMain | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class LifecycleCoroutineScopeTest { | ||
@Test | ||
fun testScopeCancellation() = runTest { | ||
setMain() | ||
val lifecycle = LifecycleRegistry() | ||
lifecycle.create() | ||
|
||
val scope = lifecycle.createCoroutineScope() | ||
assertTrue(scope.isActive) | ||
|
||
lifecycle.destroy() | ||
assertFalse(scope.isActive) | ||
} | ||
|
||
@Test | ||
fun testCreateScopeOnDestroyedLifecycle() = runTest { | ||
setMain() | ||
val lifecycle = LifecycleRegistry() | ||
lifecycle.create() | ||
lifecycle.destroy() | ||
|
||
val scope = lifecycle.createCoroutineScope() | ||
assertFalse(scope.isActive) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Decompose test | ||
|
||
Набор утилит для написания тестов на compose компоненты |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
id("ru.vladislavsumin.convention.kmp.android-library") | ||
id("ru.vladislavsumin.convention.kmp.js") | ||
id("ru.vladislavsumin.convention.kmp.jvm") | ||
} | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain.dependencies { | ||
implementation(kotlin("test")) | ||
implementation(vsCoreLibs.decompose.core) | ||
implementation(vsCoreLibs.kotlin.coroutines.core) | ||
implementation(vsCoreLibs.kotlin.coroutines.test) | ||
} | ||
} | ||
} |
Oops, something went wrong.