https://kotlinlang.org logo
#compose
Title
# compose
p

Philip S

07/06/2021, 6:57 AM
How do you mock
rememberLauncherForActivityResult
in Compose? With
registerForActivityResult
you could send in a
registry
parameter to prevent the real implementation from being called in UI-tests. However this is missing for the Compose equivalent.
n

nitrog42

07/06/2021, 7:36 AM
Compose
@Preview
now provides a
LocalActivityResultRegistryOwner
that allows you to preview Composables that use APIs like
rememberLauncherForActivityResult()
that depend on that owner existing. (Ib13d1, b/185693006)
oh sorry I thought it was for the Preview
i

Ian Lake

07/06/2021, 3:10 PM
Just provide your own registry via
LocalActivittResultRegistryOwner
p

Philip S

07/07/2021, 6:44 AM
How? I tried:
Copy code
val registry = registryWrapper.activityResultRegistry

if (registry != null) {
    LocalActivityResultRegistryOwner.provides {
        registry
    }
}
val current = LocalActivityResultRegistryOwner.current?.activityResultRegistry

require(registry == current)
where registry is my mocked registry (i debugged to check that it is not null).
rememberLauncherForActivityResult
Will still not take my mocked registry and calling
.current
does not return my mocked registry
That's what makes that value you provide available to everything under that hierarchy
p

Philip S

07/08/2021, 5:57 AM
It works now thanks for the help
281 Views