import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/layout/memo_subtree.dart'; void main() { testWidgets('rebuilds only when the memo changes', (tester) async { var builds = 0; Widget host(Object memo) => Directionality( textDirection: TextDirection.ltr, child: MemoSubtree( memo: memo, builder: (_) { builds++; return Text('$memo'); }, ), ); await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'x'}), true))); // An equal map (a new instance) and equal props keep the built child. await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'x'}), true))); expect(builds, 1); await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'y'}), true))); expect(builds, 2); await tester.pumpWidget(host((MemoMap({'id': 1, 'a': 'y'}), false))); expect(builds, 3); }); testWidgets('the built subtree still follows its inherited widgets', ( tester, ) async { Widget host(Color color) => MaterialApp( theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: color)), home: MemoSubtree( memo: 1, builder: (_) => Builder( builder: (context) => ColoredBox( key: const Key('box'), color: Theme.of(context).colorScheme.primary, ), ), ), ); Color shown() => tester.widget(find.byKey(const Key('box'))).color; await tester.pumpWidget(host(Colors.red)); final before = shown(); await tester.pumpWidget(host(Colors.blue)); await tester.pumpAndSettle(); expect(shown(), isNot(before)); }); }