Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Konsist] Integration & Custom Tests #13824

Draft
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Analysis: Convert compose view model guideline to konsist
This commit demonstrates how an existing documented Compose guideline
can be easily converted to Konstist, and, at the same time showcases how
adding a KDoc on top of this custom test/rule make this a living
document.
  • Loading branch information
ParaskP7 committed Mar 28, 2025
commit 5f5445c3dd72fab261b8d7f240b1a7ef01069007
Original file line number Diff line number Diff line change
@@ -74,9 +74,10 @@ import com.woocommerce.android.ui.woopos.common.composeui.designsystem.WooPosTyp
import com.woocommerce.android.ui.woopos.common.composeui.designsystem.toAdaptivePadding

@Composable
fun WooPosCartScreen(modifier: Modifier = Modifier) {
val viewModel: WooPosCartViewModel = hiltViewModel()

fun WooPosCartScreen(
modifier: Modifier = Modifier,
viewModel: WooPosCartViewModel = hiltViewModel()
) {
viewModel.state.observeAsState().value?.let {
WooPosCartScreen(modifier, it, viewModel::onUIEvent)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.woocommerce.android.konsist.test

import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.ext.list.withAnnotationOf
import com.lemonappdev.konsist.api.verify.assertTrue
import org.junit.Test

@Suppress("MaxLineLength")
class KonsistComposeTest {
@Test
fun `all jetpack compose previews contain 'preview' in method name`() {
@@ -14,4 +16,39 @@ class KonsistComposeTest {
.withAnnotationOf(Preview::class)
.assertTrue { it.hasNameContaining("Preview") }
}

/**
* [Composable Functions Best Practices ✅](https://github.com/woocommerce/woocommerce-android/blob/trunk/docs/compose.md#composable-functions-best-practices--)
*
* Don't acquire the viewModel inside a composable function, this will make testing harder. Inject it as a parameter
* and provide a default value to facilitate reusability:
*
* ❌
*
* ```
* @Composable
* fun MyComposable() {
* val viewModel by viewModel<MyViewModel>()
* ...
* }
* ```
*
* ✅
*
* ```
* @Composable
* fun MyComposable(viewModel : MyViewModel = getViewModel()) {
* ...
* }
* ```
*/
@Test
fun `composable functions should not acquire view model directly`() {
Konsist.scopeFromProject()
.functions()
.withAnnotationOf(Composable::class)
.assertTrue { function ->
!function.text.contains("""(?i)val\s+\w*viewModel""".toRegex())
}
}
}