Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

How to pass Composable Preview parameters to the Snapshot tests #1687

Open
Ruthvik-t opened this issue Nov 12, 2024 · 1 comment
Open

How to pass Composable Preview parameters to the Snapshot tests #1687

Ruthvik-t opened this issue Nov 12, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@Ruthvik-t
Copy link

Hi Team, I have been using Paparazzi for snapshot tests of our views built using Jetpack compose. We are using Reflections API to scan the code to find the method annotated with previews. These methods are then passed to Paparazzi for snapshot tests. Can we have capability of passing parameters to previews using ParameterPreviewProvider class from previews to snapshot tests. Is it possible ?. What about different previews that got generated with annotations like @PreviewScreenSizes, @PreviewFontScales, @PreviewLightDark, and @PreviewDynamicColors .

Here is my paparazzi snapshot test class

`class PaparazziTest {

@get:Rule
val paparazzi: Paparazzi = Paparazzi()

@Test
fun generateSnapshots() {
    val reflections = Reflections("xxx.zpreview", Scanners.MethodsAnnotated)
    val methods = reflections.getMethodsAnnotatedWith(Preview::class.java)
        .filterNot { java.lang.reflect.Modifier.isPrivate(it.modifiers) }

    val composeProxy = ComposeProxy()
    for (method in methods) {
        val name = "${method.declaringClass.simpleName}_${method.name}"

        try {
            snapshot(name = name) {
                composeProxy.wrappedInstance(method).GenerateSnapshot()
            }
        } catch (e: Throwable) {
            throw RuntimeException("Error generating snapshot $name", e)
        }
    }
}

private fun snapshot(name: String, composable: @Composable () -> Unit) {
    paparazzi.snapshot(name) {
        Box {
            composable()
        }
    }
}

private interface ComposeProxyInterface {

    @Composable
    fun GenerateSnapshot()
}

@Suppress("EmptyFunctionBlock")
private class ComposeProxy : ComposeProxyInterface {
    @Composable
    override fun GenerateSnapshot() {
    }

    fun wrappedInstance(composableFunction: Method): ComposeProxyInterface {
        val proxyClass = Proxy.getProxyClass(
            ComposeProxy::class.java.classLoader,
            ComposeProxyInterface::class.java
        )

        val invocationHandler = InvocationHandler { _, _, args: Array<out Any>? ->
            composableFunction.invoke(null, *args!!)
        }

        return proxyClass
            .getConstructor(InvocationHandler::class.java)
            .newInstance(invocationHandler) as ComposeProxyInterface
    }
}

}`

@Ruthvik-t Ruthvik-t added the enhancement New feature or request label Nov 12, 2024
@geoff-powell
Copy link
Collaborator

This will be possible with the issue: #973

We have some prs we hope to get in for the next alpha version soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants