// Author: fruit.com.cn import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:fruitshorts/pages/discover/discover_logic.dart'; import 'package:fruitshorts/pages/discover/discover_types.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('should_report_query_only_for_non_whitespace_text', () { final logic = DiscoverLogic(); expect(logic.hasSearchQuery, isFalse); logic.search.text = ' '; expect(logic.hasSearchQuery, isFalse); logic.search.text = 'remote'; expect(logic.hasSearchQuery, isTrue); logic.dispose(); }); testWidgets('should_focus_search_only_for_new_request_on_active_page', ( tester, ) async { final logic = DiscoverLogic(); await tester.pumpWidget( MaterialApp( home: Scaffold( body: TextField( controller: logic.search, focusNode: logic.searchFocus, ), ), ), ); logic.syncSearch( active: false, request: 1, previousRequest: 0, isActive: () => false, ); await tester.pump(); expect(logic.searchFocus.hasFocus, isFalse); logic.syncSearch( active: true, request: 2, previousRequest: 1, isActive: () => true, ); await tester.pump(); await tester.pump(); expect(logic.searchFocus.hasFocus, isTrue); await tester.pumpWidget(const SizedBox.shrink()); logic.dispose(); }); testWidgets('should_forward_action_when_embedded_or_pop_when_standalone', ( tester, ) async { final logic = DiscoverLogic(); final forwarded = []; final popped = Completer(); late BuildContext pageContext; await tester.pumpWidget( MaterialApp( home: Builder( builder: (context) => TextButton( onPressed: () => Navigator.of(context) .push( MaterialPageRoute( 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, DiscoverPageAction.home, embedded: true, onAction: forwarded.add, ); expect(forwarded, [DiscoverPageAction.home]); logic.act(pageContext, DiscoverPageAction.mine, embedded: false); await tester.pumpAndSettle(); await expectLater(popped.future, completion(DiscoverPageAction.mine)); logic.dispose(); }); test('should_do_nothing_when_notifying_after_dispose', () { final logic = DiscoverLogic(); var notifications = 0; logic.addListener(() => notifications++); logic.dispose(); logic.notify(); expect(notifications, 0); }); }