|
| 1 | +import 'package:bb_mobile/core/themes/app_theme.dart'; |
| 2 | +import 'package:bb_mobile/core/widgets/cards/backup_option_card.dart'; |
| 3 | +import 'package:bb_mobile/core/widgets/cards/tag_card.dart'; |
| 4 | +import 'package:bb_mobile/generated/l10n/localization.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + Future<void> pumpCard( |
| 10 | + WidgetTester tester, { |
| 11 | + List<String> tags = const [], |
| 12 | + double width = 400, |
| 13 | + }) async { |
| 14 | + await tester.pumpWidget( |
| 15 | + MaterialApp( |
| 16 | + theme: AppTheme.themeData(AppThemeType.light), |
| 17 | + localizationsDelegates: AppLocalizations.localizationsDelegates, |
| 18 | + supportedLocales: AppLocalizations.supportedLocales, |
| 19 | + home: Scaffold( |
| 20 | + body: Center( |
| 21 | + child: SizedBox( |
| 22 | + width: width, |
| 23 | + child: BackupOptionCard( |
| 24 | + icon: const Icon(Icons.lock), |
| 25 | + title: 'Encrypted vault', |
| 26 | + description: 'Stored on a server, unlocked with your PIN.', |
| 27 | + tags: tags, |
| 28 | + onTap: () {}, |
| 29 | + ), |
| 30 | + ), |
| 31 | + ), |
| 32 | + ), |
| 33 | + ), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + testWidgets('renders every tag it is given', (tester) async { |
| 38 | + await pumpCard(tester, tags: ['Easy and simple (1 minute)', 'Uses Tor']); |
| 39 | + |
| 40 | + expect(find.byType(OptionsTag), findsNWidgets(2)); |
| 41 | + expect(find.text('Easy and simple (1 minute)'), findsOneWidget); |
| 42 | + expect(find.text('Uses Tor'), findsOneWidget); |
| 43 | + }); |
| 44 | + |
| 45 | + testWidgets('renders a single tag', (tester) async { |
| 46 | + await pumpCard(tester, tags: ['Takes 10 minutes']); |
| 47 | + |
| 48 | + expect(find.byType(OptionsTag), findsOneWidget); |
| 49 | + expect(find.text('Takes 10 minutes'), findsOneWidget); |
| 50 | + }); |
| 51 | + |
| 52 | + testWidgets('renders no tag widget at all when given none', (tester) async { |
| 53 | + await pumpCard(tester); |
| 54 | + |
| 55 | + expect(find.byType(OptionsTag), findsNothing); |
| 56 | + // The rest of the card is unaffected. |
| 57 | + expect(find.text('Encrypted vault'), findsOneWidget); |
| 58 | + }); |
| 59 | + |
| 60 | + testWidgets('lays two tags side by side when there is room', (tester) async { |
| 61 | + await pumpCard( |
| 62 | + tester, |
| 63 | + tags: ['Easy and simple (1 minute)', 'Uses Tor'], |
| 64 | + width: 600, |
| 65 | + ); |
| 66 | + |
| 67 | + final first = tester.getTopLeft(find.byType(OptionsTag).at(0)); |
| 68 | + final second = tester.getTopLeft(find.byType(OptionsTag).at(1)); |
| 69 | + expect(second.dy, first.dy); |
| 70 | + expect(second.dx, greaterThan(first.dx)); |
| 71 | + }); |
| 72 | + |
| 73 | + testWidgets('wraps the second tag onto its own row when the card is narrow', ( |
| 74 | + tester, |
| 75 | + ) async { |
| 76 | + await pumpCard( |
| 77 | + tester, |
| 78 | + tags: ['Easy and simple (1 minute)', 'Uses Tor'], |
| 79 | + width: 260, |
| 80 | + ); |
| 81 | + |
| 82 | + final first = tester.getTopLeft(find.byType(OptionsTag).at(0)); |
| 83 | + final second = tester.getTopLeft(find.byType(OptionsTag).at(1)); |
| 84 | + expect(second.dy, greaterThan(first.dy)); |
| 85 | + // Wrapping, not overflowing: no layout exception and both are still there. |
| 86 | + expect(find.byType(OptionsTag), findsNWidgets(2)); |
| 87 | + }); |
| 88 | +} |
0 commit comments