goldenVariants function

Future<void> goldenVariants(
  1. WidgetTester tester,
  2. String name, {
  3. required Map<String, Widget Function()> variants,
  4. Size? surfaceSize,
  5. Color? backgroundColor,
  6. bool wrapWithMaterialApp = true,
})

Runs golden tests for multiple variants of a widget.

Implementation

Future<void> goldenVariants(
  WidgetTester tester,
  String name, {
  required Map<String, Widget Function()> variants,
  Size? surfaceSize,
  Color? backgroundColor,
  bool wrapWithMaterialApp = true,
}) async {
  for (final entry in variants.entries) {
    final variantName = entry.key;
    final widgetBuilder = entry.value;

    final widget = widgetBuilder();

    // Pump the widget wrapped in our standard wrapper
    await tester.pumpWidget(
      GoldenWrapper(
        surfaceSize: surfaceSize,
        backgroundColor: backgroundColor,
        wrapWithMaterialApp: wrapWithMaterialApp,
        child: widget,
      ),
    );

    // Wait for any animations to settle (if any, though we aim for deterministic)
    await tester.pumpAndSettle();

    final goldenPath = 'goldens/$name/$variantName.png';

    // Verify golden
    // We expect the Wrapper to match the file.
    // Actually finding by Type GoldenWrapper is safe because we just pumped it.
    await expectLater(
      find.byType(GoldenWrapper),
      matchesGoldenFile(goldenPath),
    );

    // Clear the widget tree to ensure isolation between variants
    // We pump a placeholder to clear the previous widget.
    await tester.pumpWidget(const SizedBox.shrink());
  }
}