Skip to content

Commit

Permalink
Merge pull request #31 from bamlab/feat/golden-key-improvment
Browse files Browse the repository at this point in the history
Feat/golden key improvment
  • Loading branch information
T-moz committed Sep 15, 2024
2 parents a4935df + 2dfcd65 commit bd110e5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* feat: add path and version argument to expectGolden method.

## 0.5.1

* feat: add support for skipping tests instead of failing them based on the enforced target platform defined in the `AdaptiveTestConfiguration` class.
Expand Down
59 changes: 39 additions & 20 deletions example/simple_app/test/src/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,47 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testAdaptiveWidgets(
'$App render',
(tester, variant) async {
await tester.pumpWidget(
AdaptiveWrapper(
windowConfig: variant,
tester: tester,
child: const App(),
),
);
group('Excpect golden', () {
testAdaptiveWidgets(
'$App render',
(tester, variant) async {
await tester.pumpWidget(
AdaptiveWrapper(
windowConfig: variant,
tester: tester,
child: const App(),
),
);

await tester.expectGolden<App>(variant, suffix: 'simple');
await tester.expectGolden<App>(variant, suffix: 'simple');

final textField = find.byType(TextField);
final textField = find.byType(TextField);

await tester.tap(textField);
await tester.pumpAndSettle();
await tester.tap(textField);
await tester.pumpAndSettle();

await tester.expectGolden<App>(
variant,
suffix: 'simple_with_keyboard',
);
},
);
await tester.expectGolden<App>(
variant,
suffix: 'simple_with_keyboard',
);
},
);
testAdaptiveWidgets(
'$App render with custom path',
(tester, variant) async {
await tester.pumpWidget(
AdaptiveWrapper(
windowConfig: variant,
tester: tester,
child: const App(),
),
);

await tester.expectGolden<App>(
variant,
path: 'preview/custom_path_${variant.name}.png',
);
},
);
});
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 23 additions & 5 deletions lib/src/adaptive/adaptive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,29 @@ void testAdaptiveWidgets(
/// [enforcedTestPlatform] defined in the [AdaptiveTestConfiguration].
extension Adaptive on WidgetTester {
/// Visual regression test for a given [WindowConfigData].
///
/// The [suffix] is appended to the golden file name. It defaults to
/// the empty string if not provided.
///
/// By default, the path of the generated golden file is constructed as follows:
/// `preview/${windowConfig.name}-${name.snakeCase}$localSuffix.png`.
/// If a [path] is provided, it will override this default behavior.
///
/// The [byKey] argument allows the test to find the widget by its unique key,
/// which is useful when multiple widgets of the same type are present.
///
/// The [version] argument is an optional integer that can be used to
/// differentiate historical golden files.
///
/// Set [waitForImages] to `false` if you want to skip waiting for all images
/// to load before taking the snapshot. By default, it waits for all images to load.
@isTest
Future<void> expectGolden<T>(
WindowConfigData windowConfig, {
String? suffix,
Key?
byKey, // Sometimes we want to find the widget by its unique key in the case they are multiple of the same type.
String? path,
Key? byKey,
int? version,
bool waitForImages = true,
}) async {
final enforcedTestPlatform =
Expand All @@ -93,12 +110,13 @@ extension Adaptive on WidgetTester {
if (waitForImages) {
await awaitImages();
}

final key = path ??
'preview/${windowConfig.name}-${name.snakeCase}$localSuffix.png';
await expectLater(
// Find by its type except if the widget's unique key was given.
byKey != null ? find.byKey(byKey) : find.byType(AdaptiveWrapper),
matchesGoldenFile(
'preview/${windowConfig.name}-${name.snakeCase}$localSuffix.png',
),
matchesGoldenFile(key, version: version),
);
}
}

0 comments on commit bd110e5

Please sign in to comment.