forked from eatvio/eatvio_app
feat(test): 70% tests coverage
This commit is contained in:
@@ -6,23 +6,58 @@ import 'package:eatvio_app/core/theme/theme_helper.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_popup.dart';
|
||||
import 'package:eatvio_app/generated/l10n.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
class _FakeAssetBundle extends CachingAssetBundle {
|
||||
_FakeAssetBundle(this.assets);
|
||||
final Map<String, String> assets;
|
||||
|
||||
@override
|
||||
Future<String> loadString(String key, {bool cache = true}) async {
|
||||
final value = assets[key];
|
||||
if (value == null) {
|
||||
throw FlutterError('Asset not found: $key');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<ByteData> load(String key) async {
|
||||
final value = assets[key];
|
||||
if (value == null) {
|
||||
throw FlutterError('Asset not found: $key');
|
||||
}
|
||||
final bytes = Uint8List.fromList(value.codeUnits);
|
||||
return ByteData.sublistView(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('CustomPopup smoke', () {
|
||||
setUpAll(() async {
|
||||
dotenv.loadFromString(envString: 'DEV_BASE_URL=http://localhost\nBASE_URL=http://localhost');
|
||||
});
|
||||
const fakeSvg = '''
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 10 10">
|
||||
<rect width="10" height="10" fill="red"/>
|
||||
</svg>
|
||||
''';
|
||||
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
});
|
||||
final fakeBundle = _FakeAssetBundle({
|
||||
'assets/icons/copy2.svg': fakeSvg,
|
||||
'assets/icons/share2.svg': fakeSvg,
|
||||
'assets/icons/edit_paint.svg': fakeSvg,
|
||||
'assets/icons/red_trash.svg': fakeSvg,
|
||||
'assets/icons/complain.svg': fakeSvg,
|
||||
});
|
||||
|
||||
Widget wrap(Widget child) {
|
||||
return MaterialApp(
|
||||
setUp(() async {
|
||||
await sl.reset();
|
||||
sl.registerLazySingleton<AppConfig>(() => AppConfig(buildType: AppBuildType.tests)..setBaseUrl('http://localhost'));
|
||||
});
|
||||
|
||||
Widget wrap(Widget child) {
|
||||
return DefaultAssetBundle(
|
||||
bundle: fakeBundle,
|
||||
child: MaterialApp(
|
||||
theme: ThemeHelper().themeData(),
|
||||
localizationsDelegates: const [
|
||||
S.delegate,
|
||||
@@ -31,87 +66,52 @@ void main() {
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: S.delegate.supportedLocales,
|
||||
home: Scaffold(body: child),
|
||||
);
|
||||
}
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('renders custom popup for author', (tester) async {
|
||||
await tester.pumpWidget(wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user')));
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
testWidgets('CustomPopup for author shows edit and delete actions for posts', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(
|
||||
const CustomPopup(
|
||||
isAuthor: true,
|
||||
complainedContentType: ComplainedContent.posts,
|
||||
path: 'posts/1',
|
||||
id: 1,
|
||||
userSlug: 'author',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
testWidgets('renders custom popup for non-author', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.posts)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
await tester.tap(find.byIcon(Icons.more_horiz_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
testWidgets('renders custom popup for non-author with recipe', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.recipe)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
expect(find.text('Скопировать ссылку'), findsOneWidget);
|
||||
expect(find.text('Поделиться'), findsOneWidget);
|
||||
expect(find.text('Редактировать'), findsOneWidget);
|
||||
expect(find.text('Удалить'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders custom popup for non-author with diet', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.diet)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
testWidgets('CustomPopup for non-author shows complain and block actions', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(
|
||||
const CustomPopup(
|
||||
isAuthor: false,
|
||||
complainedContentType: ComplainedContent.recipe,
|
||||
path: 'recipes/slug',
|
||||
slug: 'slug',
|
||||
userSlug: 'user-2',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
testWidgets('renders custom popup for non-author with plan', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: false, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.plan)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
await tester.tap(find.byIcon(Icons.more_horiz_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
testWidgets('renders custom popup for author with posts', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.posts)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders custom popup for author with recipe', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.recipe, slug: 'test-recipe')),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders custom popup for author with diet', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.diet, id: 1)),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('renders custom popup for author with plan', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
wrap(const CustomPopup(isAuthor: true, path: 'test/path', userSlug: 'test-user', complainedContentType: ComplainedContent.plan, slug: 'test-plan')),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.byType(CustomPopup), findsOneWidget);
|
||||
expect(find.byIcon(Icons.more_horiz_rounded), findsOneWidget);
|
||||
});
|
||||
expect(find.text('Скопировать ссылку'), findsOneWidget);
|
||||
expect(find.text('Поделиться'), findsOneWidget);
|
||||
expect(find.text('Пожаловаться'), findsOneWidget);
|
||||
expect(find.text('Заблокировать автора'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user