fix(home): 修复 feed 更新触发的 build 期 setState
This commit is contained in:
1 parent
862b7e92dd
commit
d227ac69fb
6 files changed
+174
-10
No files matched your search
@@ -1,5 +1,6 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fruitshorts/core/data/models/drama.dart';
|
||||
import 'package:fruitshorts/pages/discover/discover_types.dart';
|
||||
@@ -121,6 +122,40 @@ void main() {
|
||||
logic.dispose();
|
||||
});
|
||||
|
||||
testWidgets('should_defer_drama_notification_when_changed_during_build', (
|
||||
tester,
|
||||
) async {
|
||||
final logic = HomeLogic(dramas);
|
||||
var notifications = 0;
|
||||
var immediateNotifications = -1;
|
||||
var changed = false;
|
||||
logic.addListener(() => notifications++);
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: ListenableBuilder(
|
||||
listenable: logic,
|
||||
builder: (context, _) {
|
||||
if (!changed) {
|
||||
changed = true;
|
||||
logic.onDramaChanged(dramas[1]);
|
||||
immediateNotifications = notifications;
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(immediateNotifications, 0);
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(logic.currentDrama.id, 'city');
|
||||
await tester.pump();
|
||||
expect(notifications, 1);
|
||||
expect(tester.takeException(), isNull);
|
||||
logic.dispose();
|
||||
});
|
||||
|
||||
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
|
||||
final logic = HomeLogic(dramas);
|
||||
|
||||
|
||||
@@ -236,6 +236,7 @@ void main() {
|
||||
logic.dispose();
|
||||
await _drain(logic);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
group('history writes', () {
|
||||
@@ -618,6 +619,68 @@ void main() {
|
||||
logic.dispose();
|
||||
await _drain(logic);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'should_defer_feed_update_side_effects_when_called_during_build',
|
||||
(tester) async {
|
||||
final factory = _RecordingFactory();
|
||||
final initial = [_drama('a'), _drama('b'), _drama('c')];
|
||||
final latest = [_drama('d'), _drama('c'), _drama('a')];
|
||||
final logic = _logic(FakeWatchHistory());
|
||||
logic.attach(
|
||||
PlayerPage(
|
||||
drama: initial.first,
|
||||
initialIndex: 0,
|
||||
homeMode: true,
|
||||
feedDramas: initial,
|
||||
),
|
||||
isMounted: () => true,
|
||||
);
|
||||
await tester.runAsync(() async {
|
||||
logic.start(
|
||||
factory: factory.create,
|
||||
coordinator: PlaybackCoordinator(),
|
||||
);
|
||||
await _drain(logic);
|
||||
});
|
||||
expect(logic.currentDramaId, 'a');
|
||||
expect(logic.session.active, isNotNull);
|
||||
var updateScheduled = false;
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: ListenableBuilder(
|
||||
listenable: logic,
|
||||
builder: (context, _) {
|
||||
if (!updateScheduled) {
|
||||
updateScheduled = true;
|
||||
logic.updateFeed(latest, keepDramaId: 'a');
|
||||
}
|
||||
return Scaffold(
|
||||
body: PageView.builder(
|
||||
controller: logic.pages,
|
||||
itemCount: logic.visibleIndices.length,
|
||||
itemBuilder: (_, index) =>
|
||||
ColoredBox(color: Colors.black, key: ValueKey(index)),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(logic.pages.hasClients, isTrue);
|
||||
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(logic.queue.entryAt(2).drama.id, 'a');
|
||||
expect(logic.pages.page, closeTo(2, 0.01));
|
||||
logic.dispose();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('lifecycle', () {
|
||||
|
||||
Reference in new issue
Block a user