feat(post_creation): added multiple image posts creation and validation

This commit is contained in:
misha
2026-08-04 17:47:43 +03:00
parent 3210f38d4a
commit 04de956769
21 changed files with 2791 additions and 982 deletions

View File

@@ -21,10 +21,16 @@ void main() {
setUp(() {
blogPostRepository = FixtureBlogPostRepository();
blogPostCreateCubit = BlogPostCreateCubit(blogPostRepository: blogPostRepository);
blogPostCreateCubit = BlogPostCreateCubit(
blogPostRepository: blogPostRepository,
);
feedProbe = FeedReloadProbe();
fullCreateFixture = FixtureReader.readAsMap('blog_post_create/create_request_full.json');
updateFixture = FixtureReader.readAsMap('blog_post_create/update_request.json');
fullCreateFixture = FixtureReader.readAsMap(
'blog_post_create/create_request_full.json',
);
updateFixture = FixtureReader.readAsMap(
'blog_post_create/update_request.json',
);
});
tearDown(() {
@@ -41,11 +47,14 @@ void main() {
);
}
testWidgets('should complete full blog post creation flow', (WidgetTester tester) async {
testWidgets('should complete full blog post creation flow', (
WidgetTester tester,
) async {
// Arrange
Widget fullFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
Widget
fullFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state is BlogPostCreateStateLoaded) {
if (state.isSuccess) {
feedProbe.loadFeedBlogPostList();
}
},
@@ -82,7 +91,11 @@ void main() {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.add_photo_alternate, size: 48, color: Colors.grey),
Icon(
Icons.add_photo_alternate,
size: 48,
color: Colors.grey,
),
SizedBox(height: 8),
Text('Добавить изображение'),
],
@@ -98,7 +111,6 @@ void main() {
title: fullCreateFixture['title'] as String?,
videoUrl: fullCreateFixture['video_url'] as String?,
description: fullCreateFixture['description_md'] as String?,
image: null,
);
},
child: const Text('Опубликовать'),
@@ -111,27 +123,46 @@ void main() {
await tester.pumpWidget(createTestWidget(fullFlowWidget));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenTitleTextField), fullCreateFixture['title'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenContentTextField), fullCreateFixture['description_md'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField), fullCreateFixture['video_url'] as String);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenTitleTextField),
fullCreateFixture['title'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenContentTextField),
fullCreateFixture['description_md'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField),
fullCreateFixture['video_url'] as String,
);
await tester.pump();
await tester.tap(find.byKey(AppKeys.blogPostCreateScreenCreateButton));
await tester.pump();
// Assert
expect(blogPostRepository.createPayloads.single.toJson(), fullCreateFixture);
expect(
blogPostRepository.createPayloads.single.toJson(),
fullCreateFixture,
);
expect(feedProbe.loadRequests, 1);
});
testWidgets('should handle blog post update flow', (WidgetTester tester) async {
testWidgets('should handle blog post update flow', (
WidgetTester tester,
) async {
// Arrange
final existingBlogPost = FeedBlogPostModel(
id: 1,
state: 'published',
image: 'original_image.jpg',
description: 'Original Description',
author: const Author(authorSlug: 'test-author', authorName: 'Test Author', authorProfileIcon: 'avatar.jpg', authorIsMe: true),
author: const Author(
authorSlug: 'test-author',
authorName: 'Test Author',
authorProfileIcon: 'avatar.jpg',
authorIsMe: true,
),
videoUrl: 'https://youtube.com/watch?v=original',
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
@@ -142,9 +173,10 @@ void main() {
wasLikedByMe: false,
);
Widget updateFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
Widget
updateFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state is BlogPostCreateStateLoaded) {
if (state.isSuccess) {
feedProbe.loadFeedBlogPostList();
}
},
@@ -161,13 +193,17 @@ void main() {
decoration: const InputDecoration(labelText: 'Описание'),
maxLines: 6,
minLines: 4,
controller: TextEditingController(text: existingBlogPost.description),
controller: TextEditingController(
text: existingBlogPost.description,
),
),
const SizedBox(height: 16),
TextFormField(
key: AppKeys.blogPostCreateScreenVideoLinkTextField,
decoration: const InputDecoration(labelText: 'Ссылка на видео'),
controller: TextEditingController(text: existingBlogPost.videoUrl),
controller: TextEditingController(
text: existingBlogPost.videoUrl,
),
),
const SizedBox(height: 16),
ElevatedButton(
@@ -178,8 +214,9 @@ void main() {
title: updateFixture['title'] as String?,
videoUrl: updateFixture['video_url'] as String?,
description: updateFixture['description_md'] as String?,
image: null,
oldImageUrl: updateFixture['old_image_url'] as String?,
existingMediaIds: List<int>.from(
updateFixture['existing_media_ids'] as List,
),
);
},
child: const Text('Обновить'),
@@ -192,9 +229,18 @@ void main() {
await tester.pumpWidget(createTestWidget(updateFlowWidget));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenTitleTextField), updateFixture['title'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenContentTextField), updateFixture['description_md'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField), updateFixture['video_url'] as String);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenTitleTextField),
updateFixture['title'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenContentTextField),
updateFixture['description_md'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField),
updateFixture['video_url'] as String,
);
await tester.pump();
await tester.tap(find.byKey(AppKeys.blogPostCreateScreenCreateButton));
@@ -205,7 +251,9 @@ void main() {
expect(feedProbe.loadRequests, 1);
});
testWidgets('should handle blog post deletion flow', (WidgetTester tester) async {
testWidgets('should handle blog post deletion flow', (
WidgetTester tester,
) async {
// Arrange
Widget deleteFlowWidget = Column(
children: [
@@ -230,35 +278,44 @@ void main() {
await tester.pump();
// Assert
final deleteFixture = FixtureReader.readAsMap('blog_post_create/delete_request.json');
final deleteFixture = FixtureReader.readAsMap(
'blog_post_create/delete_request.json',
);
expect(blogPostRepository.deleteCalls.single, deleteFixture['id']);
expect(feedProbe.loadRequests, 1);
});
testWidgets('should handle error flow during blog post creation', (WidgetTester tester) async {
testWidgets('should handle error flow during blog post creation', (
WidgetTester tester,
) async {
// Arrange
blogPostRepository.createError = Exception('Network error');
Widget errorFlowWidget = BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state is BlogPostCreateStateError) {
return Column(
children: [
Text('Error: ${state.errorMessage}'),
const SizedBox(height: 16),
ElevatedButton(
key: const Key('retryButton'),
onPressed: () async {
await blogPostCreateCubit.createBlogPost(title: 'Retry Title', videoUrl: null, description: 'Retry Description', image: null);
},
child: const Text('Повторить'),
),
],
);
}
return const Text('No Error');
},
);
Widget errorFlowWidget =
BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state.errorMessage != null) {
return Column(
children: [
Text('Error: ${state.errorMessage}'),
const SizedBox(height: 16),
ElevatedButton(
key: const Key('retryButton'),
onPressed: () async {
await blogPostCreateCubit.createBlogPost(
title: 'Retry Title',
videoUrl: null,
description: 'Retry Description',
);
},
child: const Text('Повторить'),
),
],
);
}
return const Text('No Error');
},
);
// Act
await tester.pumpWidget(createTestWidget(errorFlowWidget));
@@ -269,7 +326,6 @@ void main() {
title: fullCreateFixture['title'] as String?,
videoUrl: null,
description: fullCreateFixture['description_md'] as String?,
image: null,
);
await tester.pump();
@@ -278,18 +334,27 @@ void main() {
expect(find.byKey(const Key('retryButton')), findsOneWidget);
});
testWidgets('should handle loading state during blog post creation', (WidgetTester tester) async {
testWidgets('should handle loading state during blog post creation', (
WidgetTester tester,
) async {
// Arrange
blogPostRepository.createDelay = const Duration(milliseconds: 50);
Widget loadingFlowWidget = BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state is BlogPostCreateStateLoading) {
return const Column(children: [CircularProgressIndicator(), SizedBox(height: 16), Text('Создание блог поста...')]);
}
return const Text('Not Loading');
},
);
Widget loadingFlowWidget =
BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state.isLoading) {
return const Column(
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text('Создание блог поста...'),
],
);
}
return const Text('Not Loading');
},
);
// Act
await tester.pumpWidget(createTestWidget(loadingFlowWidget));
@@ -300,7 +365,6 @@ void main() {
title: fullCreateFixture['title'] as String?,
videoUrl: null,
description: fullCreateFixture['description_md'] as String?,
image: null,
);
await tester.pump();
await tester.pump(); // Need to pump twice to get the loading state
@@ -314,29 +378,32 @@ void main() {
await tester.pumpAndSettle();
});
testWidgets('should handle success state after blog post creation', (WidgetTester tester) async {
testWidgets('should handle success state after blog post creation', (
WidgetTester tester,
) async {
// Arrange
Widget successFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state is BlogPostCreateStateLoaded) {
feedProbe.loadFeedBlogPostList();
}
},
child: BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state is BlogPostCreateStateLoaded) {
return const Column(
children: [
Icon(Icons.check_circle, color: Colors.green, size: 48),
SizedBox(height: 16),
Text('Блог пост успешно создан!'),
],
);
}
return const Text('Not Loaded');
},
),
);
Widget successFlowWidget =
BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state.isSuccess) {
feedProbe.loadFeedBlogPostList();
}
},
child: BlocBuilder<BlogPostCreateCubit, BlogPostCreateState>(
builder: (context, state) {
if (state.isSuccess) {
return const Column(
children: [
Icon(Icons.check_circle, color: Colors.green, size: 48),
SizedBox(height: 16),
Text('Блог пост успешно создан!'),
],
);
}
return const Text('Not Loaded');
},
),
);
// Act
await tester.pumpWidget(createTestWidget(successFlowWidget));
@@ -347,7 +414,6 @@ void main() {
title: fullCreateFixture['title'] as String?,
videoUrl: null,
description: fullCreateFixture['description_md'] as String?,
image: null,
);
await tester.pump();
@@ -357,28 +423,43 @@ void main() {
expect(feedProbe.loadRequests, 1);
});
testWidgets('should handle form validation flow', (WidgetTester tester) async {
testWidgets('should handle form validation flow', (
WidgetTester tester,
) async {
// Arrange
Widget validationFlowWidget = Column(
children: [
TextFormField(
key: AppKeys.blogPostCreateScreenTitleTextField,
decoration: const InputDecoration(labelText: 'Заголовок', errorText: 'Заголовок обязателен'),
decoration: const InputDecoration(
labelText: 'Заголовок',
errorText: 'Заголовок обязателен',
),
),
const SizedBox(height: 16),
TextFormField(
key: AppKeys.blogPostCreateScreenContentTextField,
decoration: const InputDecoration(labelText: 'Описание', errorText: 'Описание обязательно'),
decoration: const InputDecoration(
labelText: 'Описание',
errorText: 'Описание обязательно',
),
maxLines: 6,
minLines: 4,
),
const SizedBox(height: 16),
TextFormField(
key: AppKeys.blogPostCreateScreenVideoLinkTextField,
decoration: const InputDecoration(labelText: 'Ссылка на видео', errorText: 'Введите корректную ссылку'),
decoration: const InputDecoration(
labelText: 'Ссылка на видео',
errorText: 'Введите корректную ссылку',
),
),
const SizedBox(height: 16),
ElevatedButton(key: AppKeys.blogPostCreateScreenCreateButton, onPressed: () {}, child: const Text('Опубликовать')),
ElevatedButton(
key: AppKeys.blogPostCreateScreenCreateButton,
onPressed: () {},
child: const Text('Опубликовать'),
),
],
);
@@ -415,7 +496,11 @@ void main() {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.add_photo_alternate, size: 48, color: Colors.grey),
Icon(
Icons.add_photo_alternate,
size: 48,
color: Colors.grey,
),
SizedBox(height: 8),
Text('Добавить изображение'),
],
@@ -431,7 +516,6 @@ void main() {
title: 'Test Title',
videoUrl: null,
description: 'Test Description',
image: null, // В реальном тесте здесь был бы XFile
);
},
child: const Text('Опубликовать'),
@@ -450,90 +534,122 @@ void main() {
expect(imageUploadInitiated, isTrue);
});
testWidgets('should handle complete blog post creation with all features', (WidgetTester tester) async {
testWidgets('should handle complete blog post creation with all features', (
WidgetTester tester,
) async {
// Arrange
Widget completeFlowWidget = BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state is BlogPostCreateStateLoaded) {
feedProbe.loadFeedBlogPostList();
}
},
child: Column(
children: [
const Text('Заголовок'),
const SizedBox(height: 8),
TextFormField(
key: AppKeys.blogPostCreateScreenTitleTextField,
decoration: const InputDecoration(hintText: 'Введите заголовок поста'),
),
const SizedBox(height: 16),
const Text('Описание'),
const SizedBox(height: 8),
TextFormField(key: AppKeys.blogPostCreateScreenContentTextField, decoration: const InputDecoration(), maxLines: 6, minLines: 4),
const SizedBox(height: 16),
const Text('Изображение поста'),
const SizedBox(height: 8),
GestureDetector(
key: AppKeys.imageWidget,
onTap: () {},
child: Container(
height: 200,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.add_photo_alternate, size: 48, color: Colors.grey),
SizedBox(height: 8),
Text('Добавить изображение'),
],
Widget completeFlowWidget =
BlocListener<BlogPostCreateCubit, BlogPostCreateState>(
listener: (context, state) {
if (state.isSuccess) {
feedProbe.loadFeedBlogPostList();
}
},
child: Column(
children: [
const Text('Заголовок'),
const SizedBox(height: 8),
TextFormField(
key: AppKeys.blogPostCreateScreenTitleTextField,
decoration: const InputDecoration(
hintText: 'Введите заголовок поста',
),
),
),
const SizedBox(height: 16),
const Text('Описание'),
const SizedBox(height: 8),
TextFormField(
key: AppKeys.blogPostCreateScreenContentTextField,
decoration: const InputDecoration(),
maxLines: 6,
minLines: 4,
),
const SizedBox(height: 16),
const Text('Изображение поста'),
const SizedBox(height: 8),
GestureDetector(
key: AppKeys.imageWidget,
onTap: () {},
child: Container(
height: 200,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.add_photo_alternate,
size: 48,
color: Colors.grey,
),
SizedBox(height: 8),
Text('Добавить изображение'),
],
),
),
),
),
const SizedBox(height: 16),
const Text('Ссылка на видео'),
const SizedBox(height: 8),
TextFormField(
key: AppKeys.blogPostCreateScreenVideoLinkTextField,
decoration: const InputDecoration(),
),
const SizedBox(height: 32),
ElevatedButton(
key: AppKeys.blogPostCreateScreenCreateButton,
onPressed: () async {
await blogPostCreateCubit.createBlogPost(
title: fullCreateFixture['title'] as String?,
videoUrl: fullCreateFixture['video_url'] as String?,
description:
fullCreateFixture['description_md'] as String?,
);
},
child: const Text('Опубликовать'),
),
],
),
const SizedBox(height: 16),
const Text('Ссылка на видео'),
const SizedBox(height: 8),
TextFormField(key: AppKeys.blogPostCreateScreenVideoLinkTextField, decoration: const InputDecoration()),
const SizedBox(height: 32),
ElevatedButton(
key: AppKeys.blogPostCreateScreenCreateButton,
onPressed: () async {
await blogPostCreateCubit.createBlogPost(
title: fullCreateFixture['title'] as String?,
videoUrl: fullCreateFixture['video_url'] as String?,
description: fullCreateFixture['description_md'] as String?,
image: null,
);
},
child: const Text('Опубликовать'),
),
],
),
);
);
// Act
await tester.pumpWidget(createTestWidget(completeFlowWidget));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenTitleTextField), fullCreateFixture['title'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenContentTextField), fullCreateFixture['description_md'] as String);
await tester.enterText(find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField), fullCreateFixture['video_url'] as String);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenTitleTextField),
fullCreateFixture['title'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenContentTextField),
fullCreateFixture['description_md'] as String,
);
await tester.enterText(
find.byKey(AppKeys.blogPostCreateScreenVideoLinkTextField),
fullCreateFixture['video_url'] as String,
);
await tester.pump();
// Scroll to make sure the button is visible
await tester.drag(find.byType(SingleChildScrollView), const Offset(0, -200));
await tester.drag(
find.byType(SingleChildScrollView),
const Offset(0, -200),
);
await tester.pump();
await tester.tap(find.byKey(AppKeys.blogPostCreateScreenCreateButton));
await tester.pump();
// Assert
expect(blogPostRepository.createPayloads.single.toJson(), fullCreateFixture);
expect(
blogPostRepository.createPayloads.single.toJson(),
fullCreateFixture,
);
expect(feedProbe.loadRequests, 1);
});
});