mvysny / karibu-testing

Vaadin Server-Side Browserless Containerless Unit Testing
Apache License 2.0
111 stars 14 forks source link

testing a single view/route #167

Closed avdhootu27 closed 6 months ago

avdhootu27 commented 6 months ago

I have multiple views in my app and I want to test a single view in isolation. So is there any way to do it? Or we need to write integration test? I also have a MainLayout where I have created a drawer in which I have RouterLink to all routes. Currently I am testing it with integration test but it is taking a lot of time. I am on vaadin 14, spring boot 2.7, java 11, karibu-testing-v10-spring 1.3.24.

mvysny commented 6 months ago

It is definitely possible to test a single route, but that heavily depends on what you're trying to do. By integration test you mean that you bootstrap the app and that's taking long time? Basically you can simply create a test which navigates to a view and focuses on testing of that singular view.

avdhootu27 commented 6 months ago

Yes I bootstrap the app. I am doing MockVaadin setup as:

TestingLifecycleHookKt.setTestingLifecycleHook(testingLifecycleHook);
Routes routes = new Routes().autoDiscoverViews("...");
MockVaadin.setup(routes);

The issue here is when I run this setup, all routes get loaded and because of that I need to initialise beans of those routes, but I want only a specific route to be loaded to test it. So how to do it? If you have an example project which has multiple routes and tests are written for it then can you please share it here?

I also have a doubt- I have a MainLayout and some other routes which are child of MainLayout. I have a drawer in MainLayout which has RouterLinks to those child routes. I have to mention class names of child layouts while creating the RouterLinks in MainLayout and in child route view class I need to add MainLayout.class as layout parameter in @Route annotation, so it is causing circular dependency. So can it cause to load other views if I try to navigate to a single view. (Because all routes are child of MainLayout)

mvysny commented 6 months ago

The new Routes().autoDiscoverViews("..."); statement performs classpath scanning but doesn't instantiate those routes, so no beans are created. You can actually cache the Routes to the static field, to avoid repeated classpath scanning.

So can it cause to load other views if I try to navigate to a single view. (Because all routes are child of MainLayout)

No. class reference is not route instantiation. Only the view you navigate to, plus one instance of MainLayout is initialized. This is not directly related to Karibu-Testing but rather to the way how Vaadin operates; please read Vaadin documentation on this topic and/or ask on Vaadin forums or StackOverflow.

avdhootu27 commented 6 months ago

If you have an example project which has multiple routes and tests are written for it then can you please share it here?

mvysny commented 6 months ago

There is a list of example projects here: https://github.com/mvysny/karibu-testing

avdhootu27 commented 6 months ago

The projects mentioned here doesn't test a single view without integration test. Can you please tell me any way using which I can test a single view? with vaadin 8 I was able to test a single view by setting that view as :

view.getViewComponent().setParent(null);
getUI().getNavigator().addView(viewName, view);
getUI().getNavigator().navigateTo(viewName);
getUI().setContent((Component) view);

But in vaadin 14 there is no Navigator class. So how to test a single view?

mvysny commented 6 months ago

I see no advantage in having only one view registered. You can still do that though: when calling MockVaadin.setup(), you pass in the instance of Routes. You can create Routes with just one route.