82 lines
2.8 KiB
Dart
82 lines
2.8 KiB
Dart
// 搜索结果分页解析 + 搜索页默认态渲染测试
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:video_app/core/storage/search_history_store.dart';
|
|
import 'package:video_app/screens/search/search_screen.dart';
|
|
import 'package:video_app/services/search_service.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
group('SearchPage.fromJson', () {
|
|
test('total 与视频列表解析', () {
|
|
final page = SearchPage.fromJson(const {
|
|
'total': 236,
|
|
'list': [
|
|
{
|
|
'video_id': 88001,
|
|
'title': '海边日落',
|
|
'cover_url': 'https://cdn/cover.jpg',
|
|
'video_url': 'https://cdn/index.m3u8',
|
|
'author': {'user_id': 100023, 'nickname': '小明', 'avatar_url': ''},
|
|
'view_count': 12034,
|
|
'like_count': 456,
|
|
'comment_count': 78,
|
|
'favorite_count': 90,
|
|
'tags': ['日落'],
|
|
},
|
|
],
|
|
});
|
|
expect(page.total, 236);
|
|
expect(page.items, hasLength(1));
|
|
expect(page.items.first.videoId, 88001);
|
|
expect(page.items.first.author.nickname, '小明');
|
|
});
|
|
|
|
test('空结果兜底', () {
|
|
final page = SearchPage.fromJson(const {'total': 0, 'list': []});
|
|
expect(page.total, 0);
|
|
expect(page.items, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('SearchScreen 默认态', () {
|
|
testWidgets('展示搜索历史与热搜榜区域', (tester) async {
|
|
SharedPreferences.setMockInitialValues(const {
|
|
'search_history': ['日落', '美食'],
|
|
});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
SearchHistoryStore.initForTest(prefs);
|
|
|
|
await tester.pumpWidget(
|
|
const ProviderScope(child: MaterialApp(home: SearchScreen())),
|
|
);
|
|
await tester.pump();
|
|
|
|
expect(find.text('搜索历史'), findsOneWidget);
|
|
expect(find.text('日落'), findsOneWidget);
|
|
expect(find.text('美食'), findsOneWidget);
|
|
expect(find.text('热搜榜'), findsOneWidget);
|
|
// 热搜接口测试环境不可用,静默失败显示空态
|
|
expect(find.text('暂无热搜'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('无历史时不显示历史区域', (tester) async {
|
|
SharedPreferences.setMockInitialValues(const {});
|
|
final prefs = await SharedPreferences.getInstance();
|
|
SearchHistoryStore.initForTest(prefs);
|
|
|
|
await tester.pumpWidget(
|
|
const ProviderScope(child: MaterialApp(home: SearchScreen())),
|
|
);
|
|
await tester.pump();
|
|
|
|
expect(find.text('搜索历史'), findsNothing);
|
|
expect(find.text('热搜榜'), findsOneWidget);
|
|
});
|
|
});
|
|
}
|