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

Testing screens that uses DestinationsNavigator and Parcelable args #36

Closed
asrieltiago opened this issue Jan 19, 2022 · 4 comments
Closed

Comments

@asrieltiago
Copy link

Hi,

Previously i was using this to mock the NavHostController, but cannot find a way to do the same with DestinationsNavController.

private val navController = TestNavHostController(ApplicationProvider.getApplicationContext())

I could make it work on the startDestination of the NavGraphs, but i think this is not the correct way.

image

On the detail screen i receive parcelable args, and i'm not able to mock the args on the Destination.
When i tried to run the test, i receive this exception and cannot mock the args:

image

I wanna know if already exists a way to mock or test the screens with this library, receiving the args from the DestinationNavigator.

@raamcosta
Copy link
Owner

Hi @asrieltiago ! 👋

Thanks for reporting.
So yeah, this is something I haven't had time to fiddle with, unfortunately.
The one thing I can think of is that you can create a DestinationsNavigator like this:

     val navController = TestNavHostController(ApplicationProvider.getApplicationContext())

     fun destinationsNavigator(navBackStackEntry: NavBackStackEntry): DestinationsNavigator {
         return DestinationsNavController(navController, navBackStackEntry)
     }
    
    //...
        DestinationsNavHost(navGraph = NavGraphs.root, navController = navController)

I'm not sure if this helps or not. I'd have to fully understand what you're trying to do, so I can test on my side.
Is this an open-source project I can take a look at?
From my understanding, you want to test after navigating if the Parcelable args are correct on the other side?

@raamcosta
Copy link
Owner

raamcosta commented Jan 19, 2022

Or you can create an implementation of DestinationsNavigator any way you want since it is an interface. Again.. not sure if this helps 😅

@asrieltiago
Copy link
Author

I made the UI test work, following your idea of creating a class inheriting from the DestinationsNavigator interface.

image
image

In the end it was something simple, thanks for the quick answer and maybe this will help the next ones who are going to test the UI.

@raamcosta
Copy link
Owner

If you need an empty implementation of DestinationsNavigator, the library has an EmptyDestinationsNavigator 😅
I thought you wanted to test navigating specifically! Yeah if all you need to test is screens on their own, then this will work for you!

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

2 participants