Betterment / alchemist

A Flutter tool that makes golden testing easy.
MIT License
269 stars 37 forks source link

fix: unable to run multiple GoldenTestDeviceScenario when router is required #71

Closed jeremiahlukus closed 2 years ago

jeremiahlukus commented 2 years ago

Is there an existing issue for this?

Version

0.4.1

Description

When running tests that require MaterialApp.router i am unable to run more than one due to duplicate global keys

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.
The key [LabeledGlobalKey<NavigatorState>#e3a60] was used by multiple widgets. The parents of those
widgets were:
- AutoRouteNavigator(state: _AutoRouteNavigatorState#659c4)
- AutoRouteNavigator(state: _AutoRouteNavigatorState#f4c64)
A GlobalKey can only be specified on one widget at a time in the widget tree

Steps to reproduce

1) ``` Widget buildWidgetUnderTest() { router.push(SearchedSongsRoute( searchTerm: 'query', ));

  return ProviderScope(
    overrides: [
      userNotifierProvider.overrideWithValue(
        fakeUserNotifier,
      ),
      authNotifierProvider.overrideWithValue(
        mockAuthNotifier,
      ),
      searchedSongsNotifierProvider.overrideWithProvider(mockSearchedSongsNotifierProvider),
      searchHistoryNotifierProvider.overrideWithValue(mockSearchHistoryProvider),
    ],
    child: MaterialApp.router(
      routerDelegate: AutoRouterDelegate(
        router,
        navigatorObservers: () => [mockObserver],
        initialDeepLink: DashboardRoute.name,
      ),
      routeInformationParser: AppRouter().defaultRouteParser(),
    ),
  );
}
```

2) 
``` 
 goldenTest(
  'renders correctly on mobile',
  fileName: 'SearchedSongsPage',
  builder: () => GoldenTestGroup(
    children: [
      GoldenTestDeviceScenario(
        key: GlobalKey(debugLabel: 'scaffoldKey'),
        device: Device.smallPhone,
        name: 'golden test SearchedSongsPage on small phone',
        builder: buildWidgetUnderTest,
      ),
      GoldenTestDeviceScenario(
        key: GlobalKey(debugLabel: 'scaffoldKeysss'),
        device: Device.tabletLandscape,
        name: 'golden test SearchedSongsPage on tablet landscape',
        builder: buildWidgetUnderTest,
      ),
      // GoldenTestDeviceScenario(
      //   device: Device.tabletPortrait,
      //   name: 'golden test SearchedSongsPage on tablet Portrait',
      //   builder: buildWidgetUnderTest,
      // ),
      // GoldenTestDeviceScenario(
      //   name: 'golden test SearchedSongsPage on iphone11',
      //   builder: buildWidgetUnderTest,
      // ),
    ],
  ),
);
```

Expected behavior

When running it should update the goldens as it does when running one.

Screenshots

No response

Additional context and comments

The test is located https://github.com/jeremiahlukus/guitar_tabs/blob/jlp-search-bar/test/backend/songs/searched_songs/presentation/searched_song_page_test.dart#L230

using flutter test test/backend/songs/searched_songs/presentation/searched_song_page_test.dart --update-goldens

jeremiahlukus commented 2 years ago

i ended up just separating them like this:

  goldenTest(
    'renders correctly on iphone11',
    fileName: 'FavoriteSongsPage iphone11',
    builder: () => const GoldenTestGroup(
      children: [
        GoldenTestDeviceScenario(
          name: 'golden test FavoriteSongsPage on iphone11',
          builder: buildWidgetUnderTest,
        ),
      ],
    ),
  );