From 677fbd04823623078165b95d8753c9f3a3cb4bd8 Mon Sep 17 00:00:00 2001 From: Gregory Conrad Date: Sun, 26 May 2024 16:57:01 -0400 Subject: [PATCH] test: add unit tests for capsule warm up extension (#187) Fixes #13 --- .../lib/src/widgets/capsule_warm_up.dart | 8 +- .../flutter_rearch/test/widgets_test.dart | 92 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/packages/flutter_rearch/lib/src/widgets/capsule_warm_up.dart b/packages/flutter_rearch/lib/src/widgets/capsule_warm_up.dart index 4f463b0..089bc03 100644 --- a/packages/flutter_rearch/lib/src/widgets/capsule_warm_up.dart +++ b/packages/flutter_rearch/lib/src/widgets/capsule_warm_up.dart @@ -1,9 +1,9 @@ part of '../widgets.dart'; -/// Provides [toWarmUpWidget], a mechanism to create a [Widget] from a [List] -/// of the current states of some "warm up" [Capsule]s. -extension CapsuleWarmUp on List> { - /// Creates a [Widget] from a [List] of the current states of +/// Provides [toWarmUpWidget], a mechanism to create a [Widget] from an +/// [Iterable] of the current states of some "warm up" [Capsule]s. +extension CapsuleWarmUp on Iterable> { + /// Creates a [Widget] from an [Iterable] of the current states of /// some "warm up" [Capsule]s. /// /// - [child] is returned when all of the current states are [AsyncData]. diff --git a/packages/flutter_rearch/test/widgets_test.dart b/packages/flutter_rearch/test/widgets_test.dart index 817013d..f64433e 100644 --- a/packages/flutter_rearch/test/widgets_test.dart +++ b/packages/flutter_rearch/test/widgets_test.dart @@ -3,6 +3,8 @@ import 'package:flutter_rearch/flutter_rearch.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:rearch/rearch.dart'; +import 'util.dart'; + void main() { testWidgets('RearchConsumer rebuilds when constructor params change (#163)', (tester) async { @@ -65,6 +67,96 @@ void main() { await tester.pump(); expect(builds, equals(3)); // count == 6, capsule used, rebuilt }); + + group('capsule warm up widget extension', () { + ValueWrapper<(AsyncValue, AsyncValue)> manager( + CapsuleHandle use, + ) => + use.data(const (AsyncLoading(None()), AsyncLoading(None()))); + AsyncValue c1(CapsuleHandle use) => use(manager).value.$1; + AsyncValue c2(CapsuleHandle use) => use(manager).value.$2; + + testWidgets('waits until all AsyncData', (tester) async { + final container = useContainer(); + await tester.pumpWidget( + CapsuleContainerProvider( + container: container, + child: MaterialApp( + home: Scaffold( + body: RearchBuilder( + builder: (context, use) { + return [c1, c2].map(use.call).toWarmUpWidget( + loading: const Text('loading'), + child: const Text('warmed up'), + errorBuilder: (_) => throw UnsupportedError( + 'should not occur in this test', + ), + ); + }, + ), + ), + ), + ), + ); + + expect(find.text('loading'), findsOneWidget); + expect(find.text('warmed up'), findsNothing); + + container.read(manager).value = + const (AsyncData(123), AsyncLoading(None())); + await tester.pumpAndSettle(); + expect(find.text('loading'), findsOneWidget); + expect(find.text('warmed up'), findsNothing); + + container.read(manager).value = const (AsyncData(123), AsyncData(false)); + await tester.pumpAndSettle(); + expect(find.text('loading'), findsNothing); + expect(find.text('warmed up'), findsOneWidget); + }); + + testWidgets('shows error on the first AsyncError', (tester) async { + final container = useContainer(); + await tester.pumpWidget( + CapsuleContainerProvider( + container: container, + child: MaterialApp( + home: Scaffold( + body: RearchBuilder( + builder: (context, use) { + return [c1, c2].map(use.call).toWarmUpWidget( + loading: const Text('loading'), + child: const Text('warmed up'), + errorBuilder: (errors) => + Text(errors.single.error as String), + ); + }, + ), + ), + ), + ), + ); + + expect(find.text('loading'), findsOneWidget); + expect(find.text('error'), findsNothing); + expect(find.text('warmed up'), findsNothing); + + container.read(manager).value = + const (AsyncData(123), AsyncLoading(None())); + await tester.pumpAndSettle(); + expect(find.text('loading'), findsOneWidget); + expect(find.text('error'), findsNothing); + expect(find.text('warmed up'), findsNothing); + + container.read(manager).value = ( + const AsyncData(123), + AsyncError('error', StackTrace.current, const None()), + ); + await tester.pumpAndSettle(); + expect(find.text('loading'), findsNothing); + expect(find.text('error'), findsOneWidget); + expect(find.text('warmed up'), findsNothing); + }); + }); } class ParentConsumer extends RearchConsumer {