DavBfr / flutter_pref

Create Preference Screens easily with advanced features and sub-pages
15 stars 9 forks source link

`flutter_test`'s `find.byType` fails to find page/view when `PrefService` is used #42

Closed MrCsabaToth closed 5 months ago

MrCsabaToth commented 5 months ago

In my latest project I used VGV CLI to scaffold the app structure. The MainPage's test looks like this:

import 'package:flutter_test/flutter_test.dart';
import 'package:inspector_gadget/app/app.dart';
import 'package:inspector_gadget/main/main.dart';

void main() {
  group('App', () {
    testWidgets('renders MainPage', (tester) async {
      await tester.pumpWidget(const App());
      expect(find.byType(MainPage), findsOneWidget);
    });
  });
}

This passes, when the App is like

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        ),
        useMaterial3: true,
      ),
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      home: const MainPage(),
    );
  }
}

however when I introduce PrefService, the test fails:

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return PrefService(
      service: PreferencesState.prefService!,
      child: MaterialApp(
        theme: ThemeData(
          appBarTheme: AppBarTheme(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          ),
          useMaterial3: true,
        ),
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        supportedLocales: AppLocalizations.supportedLocales,
        home: const MainPage(),
      ),
    );
  }
}

I tried skipOffstage: false (expect(find.byType(MainPage, skipOffstage: false), findsOneWidget);), but that didn't help. This might not be a flutter_pref issue, but until I find out more about it I wanted to document it, maybe someone came across to it and knows a workaround.

MrCsabaToth commented 5 months ago

I need to mock and initialize PrefSerivce. I have a hard time conforming PrefService with BLoC / Cubit architecture.