Skip to content

Commit 97f92e7

Browse files
tests: Add test for avatar error placeholder.
This adds a new test case to verify the behavior of the AvatarImage widget when the avatar URL fails to load. The test uses HttpOverrides with a MockHttpClient to reliably simulate a network error. It then asserts that the expected placeholder widget (identified by its icon, ZulipIcons.person) is rendered, ensuring the error handling is working correctly.
1 parent 1a37418 commit 97f92e7

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

test/widgets/user_test.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import 'dart:io';
2+
13
import 'package:checks/checks.dart';
24
import 'package:flutter/cupertino.dart';
35
import 'package:flutter/material.dart';
4-
import 'package:flutter/rendering.dart';
56
import 'package:flutter_test/flutter_test.dart';
67
import 'package:zulip/model/store.dart';
78
import 'package:zulip/widgets/content.dart';
9+
import 'package:zulip/widgets/icons.dart';
810
import 'package:zulip/widgets/store.dart';
911
import 'package:zulip/widgets/user.dart';
1012

@@ -13,6 +15,15 @@ import '../model/binding.dart';
1315
import '../model/test_store.dart';
1416
import '../stdlib_checks.dart';
1517
import '../test_images.dart';
18+
import 'test_app.dart';
19+
20+
class MockHttpClient extends Fake implements HttpClient {
21+
@override
22+
Future<HttpClientRequest> getUrl(Uri url) async {
23+
throw const SocketException('test error');
24+
}
25+
}
26+
1627

1728
void main() {
1829
TestZulipBinding.ensureInitialized();
@@ -73,6 +84,30 @@ void main() {
7384
debugNetworkImageHttpClientProvider = null;
7485
});
7586

87+
testWidgets('shows placeholder when image URL gives error', (WidgetTester tester) async {
88+
await HttpOverrides.runZoned(() async {
89+
addTearDown(testBinding.reset);
90+
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
91+
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
92+
final user = eg.user(avatarUrl: 'https://zulip.com/avatar.png');
93+
await store.addUser(user);
94+
95+
await tester.pumpWidget(
96+
TestZulipApp(
97+
accountId: eg.selfAccount.id,
98+
child: AvatarImage(userId: user.userId, size: 32),
99+
),
100+
);
101+
await tester.pump(); // Image provider is created
102+
await tester.pump(); // Image fails to load
103+
expect(find.byIcon(ZulipIcons.person), findsOneWidget);
104+
105+
expect(find.byType(DecoratedBox), findsOneWidget);
106+
107+
}, createHttpClient: (context) => MockHttpClient());
108+
});
109+
110+
76111
testWidgets('smoke with invalid URL', (tester) async {
77112
const avatarUrl = '::not a URL::';
78113
check(await actualUrl(tester, avatarUrl)).isNull();

0 commit comments

Comments
 (0)