fix(ui): fix broken test

This commit is contained in:
Efthymios Sarmpanis
2021-10-11 10:58:17 +03:00
parent 549f3f03d5
commit 9c4d33f1e6
@@ -1,6 +1,3 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@@ -26,14 +23,14 @@ void main() {
when(() => channel.on(any(), any(), any(), any())) when(() => channel.on(any(), any(), any(), any()))
.thenAnswer((_) => const Stream.empty()); .thenAnswer((_) => const Stream.empty());
channelClientState = MockChannelState(); channelClientState = MockChannelState();
when(() => channel.client).thenReturn(client);
when(() => channel.state).thenReturn(channelClientState); when(() => channel.state).thenReturn(channelClientState);
when(() => channelClientState.threadsStream) when(() => channelClientState.threadsStream)
.thenAnswer((_) => const Stream.empty()); .thenAnswer((_) => const Stream.empty());
when(() => channelClientState.messagesStream) when(() => channelClientState.messagesStream)
.thenAnswer((_) => const Stream.empty()); .thenAnswer((_) => const Stream.empty());
when(() => channelClientState.messages).thenReturn([]); when(() => channelClientState.messages)
when(() => channelClientState.isUpToDateStream) .thenReturn([Message(text: 'Hello World!')]);
.thenAnswer((_) => Stream.value(true));
when(() => channelClientState.isUpToDate).thenReturn(true); when(() => channelClientState.isUpToDate).thenReturn(true);
when(() => channelClientState.unreadCountStream) when(() => channelClientState.unreadCountStream)
.thenAnswer((_) => Stream.value(0)); .thenAnswer((_) => Stream.value(0));
@@ -69,58 +66,64 @@ void main() {
expect(find.byKey(emptyWidgetKey), findsOneWidget); expect(find.byKey(emptyWidgetKey), findsOneWidget);
}); });
testWidgets('renders empty message list view with custom background', testWidgets('renders a non empty message list view with custom background',
(tester) async { (tester) async {
when(() => channelClientState.messagesStream) final message = Message(
.thenAnswer((_) => Stream.value([Message(text: 'Hello world!')])); id: 'message1',
when(() => channelClientState.messages) text: 'Hello world!',
.thenReturn([Message(text: 'Hello world!')]); user: User(
id: 'user',
name: 'Test User',
),
);
const emptyWidgetKey = Key('empty_widget'); when(() => channelClientState.messagesStream).thenAnswer(
(_) => Stream.value([message]),
);
when(() => channelClientState.messages).thenReturn([message]);
await tester.pumpWidget( const nonEmptyWidgetKey = Key('non_empty_widget');
MaterialApp( await tester.runAsync(() async {
home: DefaultAssetBundle( await tester.pumpWidget(
bundle: _TestAssetBundle(), MaterialApp(
child: StreamChat( home: DefaultAssetBundle(
client: client, bundle: rootBundle,
streamChatThemeData: StreamChatThemeData.light().copyWith( child: StreamChat(
messageListViewTheme: const MessageListViewThemeData( client: client,
backgroundColor: Colors.grey, streamChatThemeData: StreamChatThemeData.light().copyWith(
backgroundImage: DecorationImage( messageListViewTheme: const MessageListViewThemeData(
image: AssetImage('assets/background.png'), backgroundColor: Colors.grey,
fit: BoxFit.cover, backgroundImage: DecorationImage(
image: AssetImage('images/placeholder.png'),
fit: BoxFit.none,
),
), ),
), ),
), child: StreamChannel(
child: StreamChannel( channel: channel,
channel: channel, child: const MessageListView(
child: MessageListView( key: nonEmptyWidgetKey,
emptyBuilder: (_) => Container(key: emptyWidgetKey), ),
), ),
), ),
), ),
), ),
), );
); await tester.pumpAndSettle();
await tester.pumpAndSettle(); });
bool findBackground(Widget widget) =>
widget is Container &&
widget.decoration is BoxDecoration &&
(widget.decoration! as BoxDecoration).image != null;
expect(find.byType(MessageListView), findsOneWidget); expect(find.byType(MessageListView), findsOneWidget);
expect(find.byKey(emptyWidgetKey), findsNothing); expect(find.byKey(nonEmptyWidgetKey), findsOneWidget);
expect(find.byType(DecorationImage, skipOffstage: false), findsOneWidget); expect(
find.byWidgetPredicate(
findBackground,
description: 'findBackground',
),
findsOneWidget);
}); });
} }
class _TestAssetBundle extends CachingAssetBundle {
@override
Future<ByteData> load(String key) async {
if (key == 'assets/background.png') {
return ByteData.view(
Uint8List.fromList(
utf8.encode('Background'),
).buffer,
);
}
return ByteData(0);
}
}