Skip to content

Commit 60b16e2

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 60b16e2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/widgets/user_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import 'dart:io';
2+
13
import 'package:checks/checks.dart';
24
import 'package:flutter/cupertino.dart';
35
import 'package:flutter/material.dart';
46
import 'package:flutter/rendering.dart';
57
import 'package:flutter_test/flutter_test.dart';
68
import 'package:zulip/model/store.dart';
79
import 'package:zulip/widgets/content.dart';
10+
import 'package:zulip/widgets/icons.dart';
811
import 'package:zulip/widgets/store.dart';
912
import 'package:zulip/widgets/user.dart';
1013

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

1729
void main() {
1830
TestZulipBinding.ensureInitialized();
@@ -73,6 +85,30 @@ void main() {
7385
debugNetworkImageHttpClientProvider = null;
7486
});
7587

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

0 commit comments

Comments
 (0)