alchemist icon indicating copy to clipboard operation
alchemist copied to clipboard

request: support multiple themes

Open Giuspepe opened this issue 2 years ago • 2 comments

Is there an existing feature request for this?

  • [X] I have searched the existing issues.

Command

I'd like to run a golden test with multiple themes, not just a single theme

Description

I would like to be able to pass AlchemistConfig a List<ThemeData>? instead of just a ThemeData?. The goldenTest method should then run the test with all given themes in the current AlchemistConfig. This would remove redundancy when testing a widget with multiple themes. Currently you would need a separate goldenTest for each theme that should be tested, even if the widget under test is the same.

So instead of

void main() {
  AlchemistConfig.runWithConfig(
      config: AlchemistConfig(
        theme: MyLightTheme()
      ),
      run: () => goldenTest('widget should look good with light theme', ..., widget: MyWidget());
  );

  AlchemistConfig.runWithConfig(
      config: AlchemistConfig(
        theme: MyDarkTheme()
      ),
      run: () => goldenTest('widget should look good with dark theme', ..., widget: MyWidget());
  );

  // etc. for all other themes
}

I would like to be able to write the following

void main() {
  AlchemistConfig.runWithConfig(
      config: AlchemistConfig(
        themes: [MyLightTheme(), MyDarkTheme(), ...]
      ),
      run: () => goldenTest('widget should look good with all themes', ..., widget: MyWidget());
  );

}

Reasoning

Many apps have multiple themes. E.g. a default light theme and a dark theme. If your app cares about accessibility it may even have a high contrast light theme and a high contrast dark theme.

If you want to write a golden test for a widget and ensure that it looks good on each theme, you would have to write a separate test for each theme because right now you can only pass a single theme to AlchemistConfig. If you could pass a list of themes as suggested, you would have to write only one golden test and pass it all the themes the widget should be tested with.

Additional context and comments

No response

Giuspepe avatar Mar 07 '22 12:03 Giuspepe

Hi! Thanks for the idea!

@Kirpal We could also consider allowing a theme (or even an entire config) to be passed in as a parameter override in the following: https://github.com/Betterment/alchemist/blob/109b799f7181fbd0bda7a319261d08a7f03cd768/lib/src/golden_test.dart#L125-L135

If we did, that would potentially enable something like the following to be possible.

void main() {
  AlchemistConfig.runWithConfig(
      config: AlchemistConfig.current(),
      run: () {
        for (final theme in myThemes) {
          goldenTest('widget should look good with all themes', ..., widget: MyWidget(), theme: theme);
        }
      }
  );
}

definitelyokay avatar Mar 08 '22 23:03 definitelyokay

Great question!

@Giuspepe Is there any reason why simply using a Theme widget in a list of testable widgets wouldn't work? 👀

goldenTest(
  'renders correctly for all themes',
  fileName: 'my_widget',
  builder: () {
    final themes = <ThemeData>[/* ... */];

    return GoldenTestGroup(
      children: [
        for (final theme in themes)
          GoldenTestScenario(
            name: 'using theme $theme',
            child: Theme(
              data: theme,
              child: MyWidget(),
            ),
          ),
      ],
    );
  },
);

jeroen-meijer avatar Jun 02 '22 12:06 jeroen-meijer