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

Allow recording state without MavericksView for Composables #544

Open
inktomi opened this issue May 4, 2021 · 4 comments
Open

Allow recording state without MavericksView for Composables #544

inktomi opened this issue May 4, 2021 · 4 comments

Comments

@inktomi
Copy link
Contributor

inktomi commented May 4, 2021

When using Mavericks with Compose, the UI is not extending from MavericksView. This means the existing mock recording code using mavericks-mocking won't work. Could we have this functionality moved from the View layer into the ViewModel layer?

As ViewModels are responsible for emitting UI state, this seems like a reasonable change - and it would enable the recording to happen even when the UI is composed of Composables. This would make testing with Compose driven apps easier, as we would have the ability to record potentially complicated state emitted from our ViewModels for use in even the most focused Composable tests (eg, using the createComposeRule() and composeTestRule.setContent.

It would also be ok if, at first, we had to use a "flow test" (createAndroidComposeRule<SomeActivity>()) to enable this functionality - but I think it would be idea if both cases were supported - testing via an Activity and testing the Composable directly.

https://airbnb.io/mavericks/#/mocking

@inktomi
Copy link
Contributor Author

inktomi commented May 5, 2021

Another use case would be, if you use a ViewModel in a composable, but not in a MavericksView - there's no way to provide a faked version for your tests.

@elihart
Copy link
Contributor

elihart commented Jan 12, 2022

yes, we will likely need to do something like this, but it would be a fairly large update and won't happen immediately

@leggomymeggos
Copy link

@elihart Any updates on this? Being able to mock a mavericks view model in a compose test would be incredibly helpful

@elihart
Copy link
Contributor

elihart commented Jul 16, 2024

In terms of recording viewmodel state, I believe that is already possible. The MockMavericksViewModelConfigFactory that is used when mocking is enabled builds a MockableMavericksViewModelConfig that registers a ViewModelStatePrinter broadcast receiver when the viewmodel is created (and removes it when the viewmodel is cleared.

The documentation doesn't reflect this exactly, but the mock printer script should generally work with viewmodels created and used in Compose UI (if that isn't the case, please raise the exact issue and we can suggest a fix).

The other part, I believe, is how to apply the mock state. With Fragments we had a system for declaring mock states and using them in tests, but with Compose UI there isn't anything as cohesive. You would have to create the viewmodel manually with the mock state for a test, which should work fine.

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

No branches or pull requests

3 participants