-
Notifications
You must be signed in to change notification settings - Fork 78
Open
Labels
Description
If I use withClock with a fixed time, I get a different time if I use it in a new BuildContext in WidgetTesting. I expect that all of the test should have the fixed time I set with withClock.
Here is a small example.
import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
void main() {
final fixedClock = Clock.fixed(DateTime(2023));
setUpAll(loadAppFonts);
testWidgets('Test clock with raw widget', (tester) async {
// returns 2023-01-01
final widget = withClock(
fixedClock,
() => MaterialApp(
home: Scaffold(
body: Center(child: Text(clock.now().toString())),
),
),
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('raw_widget.png'),
);
});
testWidgets('Test clock with builder', (tester) async {
// returns 2023-04-24
final widget = withClock(
fixedClock,
() => MaterialApp(
home: Scaffold(
body: Center(
child: Builder(
builder: (context) => Text(
clock.now().toString(),
),
),
),
),
),
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('builder.png'),
);
});
testWidgets(
'Test widget with clock only wrapped around widget',
(tester) async {
// returns 2023-04-24
final widget = withClock(
fixedClock,
TestWidget.new,
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('clock_wrap_widget_.png'),
);
},
);
testWidgets(
'Test widget with clock wrapped around whole test content',
(tester) async {
// returns 2023-01-01
await withClock(fixedClock, () async {
final widget = withClock(
fixedClock,
TestWidget.new,
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('clock_wrap_test_content.png'),
);
});
},
);
}
class TestWidget extends StatelessWidget {
const TestWidget({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(clock.now().toString()),
),
),
);
}
}
DerJojo11, hampsterx, jlnrrg and dizzib