DEV Community

Reme Le Hane
Reme Le Hane

Posted on • Originally published at Medium on

Bringing localization into your Widget testing

The more accurate you make your test, the higher the quality of the test itself.

Many apps these days are built to be more accessible, one thing we do to ensure this is we localize our applications, allowing people who do not speak the same language as we do, to also use the application.

In our application, we have been using easy_localization, I know there are quite a few choices, but for us, this was one of the easier ones to implement and for us, the JSON support was a big win with how we manage our localization.

To ensure we have the most accurate widget tests, we rather use โ€œrealโ€ localizations than having to mock the helper classes.

To do this we keep a local copy of the locale JSON files to keep them safe from external updates and have created a helper function createLocalizedWidgetFortesting which looks like:

Widget createLocalizedWidgetForTesting({Widget child}) {
  return EasyLocalization(
    path: '$TEST\_MOCK\_STORAGE/locale',
    useOnlyLangCode: true,
    assetLoader: FileAssetLoader(),
    fallbackLocale: const Locale('en'),
    supportedLocales: globals.supportedLocale,
    saveLocale: false,
    child: MaterialApp(
      home: Scaffold(
        body: child,
      ),
    ),
  );
}
Enter fullscreen mode Exit fullscreen mode

As you will see, EasyLocatlizationrequires a path, that is simply a constant that we have defined a little higher in the file, as it is used as part of other mocks.

const TEST\_MOCK\_STORAGE = './test/fixtures/core';
Enter fullscreen mode Exit fullscreen mode

Beyond that, everything else is pretty much how it is set up in main.dart.

Here is a simplified example of how a test would look with this implementation.

void main() {
  testWidgets(
    'Should render localized widget',
    (
      WidgetTester tester,
    ) async {
      await tester.pumpWidget(
        createLocalizedWidgetForTesting(
          child: SampleWidget(),
        ),
      );

      await tester.pumpAndSettle();

      // expectations to follow
    }
  );
}
Enter fullscreen mode Exit fullscreen mode

Through this would be able to run proper expectations like:

expect(find.text("Sample localized text", findsOneWidget);
Enter fullscreen mode Exit fullscreen mode

More importantly, if you have a language switcher that a user can select you can test for text changes through a setup flow.

I hope you found this useful if you want to learn more about flutter testing why not take a look at some of the other posts we have writtenโ€ฆ

Widget testing passed in function


Top comments (1)

Collapse
 
33nano profile image
Manyong'oments

Wow, super simple and straight to the point.