Files
ReelShort/test/mine/mine_logic_test.dart
T

118 lines
3.3 KiB
Dart

// Author: fruit.com.cn
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/watch_progress.dart';
import 'package:fruitshorts/pages/mine/mine_logic.dart';
import 'package:fruitshorts/pages/mine/mine_types.dart';
import '../support/fake_watch_history.dart';
const _record = WatchProgress(
dramaId: 'signal',
episodeId: 'signal-1',
position: Duration(seconds: 7),
duration: Duration(minutes: 2),
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('should_load_recent_watches_when_constructed', () async {
final history = FakeWatchHistory()..recentRecords = const [_record];
final logic = MineLogic(history: history);
await pumpEventQueue();
expect(logic.recent.single.dramaId, 'signal');
expect(logic.watchRecordTotal, 1);
expect(logic.recentLoading, isFalse);
expect(logic.recentError, isNull);
logic.dispose();
});
test('should_expose_error_when_recent_request_fails', () async {
final history = FakeWatchHistory()..failReads = true;
final logic = MineLogic(history: history);
await pumpEventQueue();
expect(logic.recent, isEmpty);
expect(logic.recentError, 'progressError');
logic.dispose();
});
test('should_refresh_recent_only_when_page_becomes_active', () async {
final history = FakeWatchHistory()..recentRecords = const [_record];
final logic = MineLogic(history: history);
await pumpEventQueue();
history.recentRecords = const [];
logic.syncPage(active: true, wasActive: true);
await pumpEventQueue();
expect(logic.recent, hasLength(1));
logic.syncPage(active: true, wasActive: false);
await pumpEventQueue();
expect(logic.recent, isEmpty);
logic.dispose();
});
testWidgets('should_forward_action_when_embedded_or_pop_when_standalone', (
tester,
) async {
final logic = MineLogic(history: FakeWatchHistory());
final forwarded = <MyPageAction>[];
final popped = Completer<MyPageAction?>();
late BuildContext pageContext;
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) => TextButton(
onPressed: () => Navigator.of(context)
.push<MyPageAction>(
MaterialPageRoute<MyPageAction>(
builder: (inner) {
pageContext = inner;
return const SizedBox.shrink();
},
),
)
.then(popped.complete),
child: const Text('open'),
),
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
logic.act(
pageContext,
MyPageAction.recommend,
embedded: true,
onAction: forwarded.add,
);
expect(forwarded, [MyPageAction.recommend]);
logic.act(pageContext, MyPageAction.resume, embedded: false);
await tester.pumpAndSettle();
await expectLater(popped.future, completion(MyPageAction.resume));
logic.dispose();
});
test('should_do_nothing_when_notifying_after_dispose', () {
final logic = MineLogic(history: FakeWatchHistory());
var notifications = 0;
logic.addListener(() => notifications++);
logic.dispose();
logic.notify();
expect(notifications, 0);
});
}