add
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// 消息通知模块模型解析单测
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'package:video_app/services/notification_service.dart';
|
||||
|
||||
void main() {
|
||||
group('NotificationItem', () {
|
||||
test('fromJson 解析(服务端字段)', () {
|
||||
final n = NotificationItem.fromJson(const {
|
||||
'id': 701,
|
||||
'type': 1,
|
||||
'title': '视频审核通过',
|
||||
'content': '你发布的《海边日落》已通过审核',
|
||||
'biz_id': '88001',
|
||||
'is_read': false,
|
||||
'created_at': '2026-10-14 15:00:00',
|
||||
});
|
||||
expect(n.id, 701);
|
||||
expect(n.type, 1);
|
||||
expect(n.typeLabel, '审核');
|
||||
expect(n.isRead, isFalse);
|
||||
expect(n.videoId, 88001);
|
||||
});
|
||||
|
||||
test('缺省兜底 + 类型标签', () {
|
||||
final n = NotificationItem.fromJson(const {'id': 1, 'title': '系统'});
|
||||
expect(n.type, 3); // 缺省按公告
|
||||
expect(n.typeLabel, '公告');
|
||||
expect(n.bizId, '');
|
||||
expect(n.videoId, isNull);
|
||||
|
||||
// 未知类型
|
||||
final x = NotificationItem.fromJson(const {'id': 2, 'type': 99});
|
||||
expect(x.typeLabel, '通知');
|
||||
|
||||
// 非审核类型不解析 videoId
|
||||
final w = NotificationItem.fromJson(
|
||||
const {'id': 3, 'type': 2, 'biz_id': 'W20261014001'},
|
||||
);
|
||||
expect(w.videoId, isNull);
|
||||
});
|
||||
|
||||
test('copyWith 标记已读', () {
|
||||
const n = NotificationItem(id: 1, type: 4, title: '回复');
|
||||
final read = n.copyWith(isRead: true);
|
||||
expect(read.isRead, isTrue);
|
||||
expect(read.typeLabel, '回复');
|
||||
expect(n.isRead, isFalse); // 原对象不可变
|
||||
});
|
||||
});
|
||||
|
||||
group('NotificationsPage', () {
|
||||
test('分页解析', () {
|
||||
final p = NotificationsPage.fromJson(const {
|
||||
'total': 42,
|
||||
'list': [
|
||||
{'id': 1, 'type': 1, 'title': 'a', 'is_read': true},
|
||||
{'id': 2, 'type': 4, 'title': 'b', 'is_read': false},
|
||||
],
|
||||
});
|
||||
expect(p.total, 42);
|
||||
expect(p.items.length, 2);
|
||||
expect(p.items[0].isRead, isTrue);
|
||||
expect(p.items[1].typeLabel, '回复');
|
||||
});
|
||||
|
||||
test('空响应兜底', () {
|
||||
final p = NotificationsPage.fromJson(const {});
|
||||
expect(p.total, 0);
|
||||
expect(p.items, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user