Store last 10 search queries in a new search_history_entries DB table (schema v27). Show them as chips below the search field when it is focused and empty; tapping a chip re-runs the search. Add a "Clear" button to wipe all history. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
202 lines
6.5 KiB
Dart
202 lines
6.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:sharedinbox/core/models/mailbox.dart';
|
|
import 'package:sharedinbox/di.dart';
|
|
|
|
import 'helpers.dart';
|
|
|
|
void main() {
|
|
group('SearchScreen', () {
|
|
testWidgets('shows placeholder hint text when empty', (tester) async {
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository(),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(FakeEmailRepository()),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Type 3+ characters to search'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('typing fewer than 3 characters does not trigger search', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository(),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(FakeEmailRepository()),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(find.byType(TextField), 'hi');
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
|
|
expect(find.text('Type 3+ characters to search'), findsOneWidget);
|
|
expect(find.text('No results'), findsNothing);
|
|
});
|
|
|
|
testWidgets('shows "No results" when search returns nothing', (
|
|
tester,
|
|
) async {
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository(),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(FakeEmailRepository()),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(find.byType(TextField), 'xyz');
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('No results'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('shows email results under "Messages" section', (
|
|
tester,
|
|
) async {
|
|
final email = testEmail(subject: 'Invoice Q3');
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository(),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(
|
|
FakeEmailRepository(searchResults: [email]),
|
|
),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(find.byType(TextField), 'inv');
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Messages'), findsOneWidget);
|
|
expect(find.text('Invoice Q3'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('shows folder results under "Folders" section', (
|
|
tester,
|
|
) async {
|
|
const archiveMailbox = Mailbox(
|
|
id: 'acc-1:Archive',
|
|
accountId: 'acc-1',
|
|
path: 'Archive',
|
|
name: 'Archive',
|
|
unreadCount: 0,
|
|
totalCount: 5,
|
|
);
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository([archiveMailbox]),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(FakeEmailRepository()),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(find.byType(TextField), 'arc');
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Folders'), findsOneWidget);
|
|
expect(find.text('Archive'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('tapping clear button resets results to placeholder', (
|
|
tester,
|
|
) async {
|
|
final email = testEmail(subject: 'Found email');
|
|
await tester.pumpWidget(
|
|
buildApp(
|
|
initialLocation: '/accounts/acc-1/search',
|
|
overrides: [
|
|
accountRepositoryProvider.overrideWithValue(
|
|
FakeAccountRepository([kTestAccount]),
|
|
),
|
|
mailboxRepositoryProvider.overrideWithValue(
|
|
FakeMailboxRepository(),
|
|
),
|
|
emailRepositoryProvider.overrideWithValue(
|
|
FakeEmailRepository(searchResults: [email]),
|
|
),
|
|
searchHistoryRepositoryProvider.overrideWithValue(
|
|
FakeSearchHistoryRepository(),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.enterText(find.byType(TextField), 'found');
|
|
await tester.pump(const Duration(milliseconds: 400));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Found email'), findsOneWidget);
|
|
|
|
await tester.tap(find.byIcon(Icons.clear));
|
|
await tester.pumpAndSettle();
|
|
|
|
// Results are gone; the recent-search chip for the prior query appears.
|
|
expect(find.text('Found email'), findsNothing);
|
|
expect(find.text('found'), findsOneWidget);
|
|
});
|
|
});
|
|
}
|