|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:stream_chat_flutter/src/ai_assistant/stream_typewriter_builder.dart'; |
| 4 | +import 'package:stream_chat_flutter/src/ai_assistant/streaming_message_view.dart'; |
| 5 | +import 'package:stream_chat_flutter/src/misc/markdown_message.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('StreamingMessageView Tests', () { |
| 9 | + testWidgets( |
| 10 | + 'displays initial text', |
| 11 | + (WidgetTester tester) async { |
| 12 | + const testText = 'Hello, world!'; |
| 13 | + |
| 14 | + await tester.pumpWidget( |
| 15 | + const MaterialApp( |
| 16 | + home: Scaffold( |
| 17 | + body: StreamingMessageView(text: testText), |
| 18 | + ), |
| 19 | + ), |
| 20 | + ); |
| 21 | + |
| 22 | + expect(find.text(testText), findsOneWidget); |
| 23 | + }, |
| 24 | + ); |
| 25 | + |
| 26 | + testWidgets( |
| 27 | + 'updates text progressively like a typewriter', |
| 28 | + (WidgetTester tester) async { |
| 29 | + const testText = 'Hello, world!'; |
| 30 | + const typingSpeed = Duration(milliseconds: 20); |
| 31 | + |
| 32 | + await tester.pumpWidget( |
| 33 | + const MaterialApp( |
| 34 | + home: Scaffold( |
| 35 | + body: StreamingMessageView( |
| 36 | + text: testText, |
| 37 | + typingSpeed: typingSpeed, |
| 38 | + ), |
| 39 | + ), |
| 40 | + ), |
| 41 | + ); |
| 42 | + |
| 43 | + expect(find.text(testText), findsOneWidget); |
| 44 | + |
| 45 | + const updatedText = 'Hello, world! How are you?'; |
| 46 | + await tester.pumpWidget( |
| 47 | + const MaterialApp( |
| 48 | + home: Scaffold( |
| 49 | + body: StreamingMessageView( |
| 50 | + text: updatedText, |
| 51 | + typingSpeed: typingSpeed, |
| 52 | + ), |
| 53 | + ), |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + await tester.pump(typingSpeed * updatedText.length); |
| 58 | + |
| 59 | + expect(find.text(updatedText), findsOneWidget); |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + testWidgets( |
| 64 | + 'handles links correctly', |
| 65 | + (WidgetTester tester) async { |
| 66 | + const testText = '[Click me](https://example.com)'; |
| 67 | + const typingSpeed = Duration(milliseconds: 20); |
| 68 | + String? tappedLink; |
| 69 | + |
| 70 | + await tester.pumpWidget( |
| 71 | + MaterialApp( |
| 72 | + home: Scaffold( |
| 73 | + body: StreamingMessageView( |
| 74 | + text: testText, |
| 75 | + typingSpeed: typingSpeed, |
| 76 | + onTapLink: (String link, String? href, String title) { |
| 77 | + tappedLink = href; |
| 78 | + }, |
| 79 | + ), |
| 80 | + ), |
| 81 | + ), |
| 82 | + ); |
| 83 | + |
| 84 | + await tester.pump(typingSpeed * testText.length); |
| 85 | + |
| 86 | + final linkFinder = find.text('Click me'); |
| 87 | + expect(linkFinder, findsOneWidget); |
| 88 | + await tester.tap(linkFinder); |
| 89 | + |
| 90 | + expect(tappedLink, equals('https://example.com')); |
| 91 | + }, |
| 92 | + ); |
| 93 | + }); |
| 94 | +} |
0 commit comments