forked from eatvio/eatvio_app
feat(post_creation): added multiple image posts creation and validation
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
import 'package:eatvio_app/features/blog_post_create/validation/blog_post_form_validator.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('BlogPostFormValidator.title', () {
|
||||
test('allows empty', () {
|
||||
expect(BlogPostFormValidator.title(''), isNull);
|
||||
expect(BlogPostFormValidator.title(' '), isNull);
|
||||
expect(BlogPostFormValidator.title(null), isNull);
|
||||
});
|
||||
|
||||
test('rejects shorter than 3 chars', () {
|
||||
expect(
|
||||
BlogPostFormValidator.title('ab'),
|
||||
BlogPostFormValidator.titleTooShort,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.title(' a '),
|
||||
BlogPostFormValidator.titleTooShort,
|
||||
);
|
||||
});
|
||||
|
||||
test('allows 3+ chars', () {
|
||||
expect(BlogPostFormValidator.title('abc'), isNull);
|
||||
expect(BlogPostFormValidator.title(' hello '), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('BlogPostFormValidator.description', () {
|
||||
test('allows empty and rejects short', () {
|
||||
expect(BlogPostFormValidator.description(''), isNull);
|
||||
expect(
|
||||
BlogPostFormValidator.description('xy'),
|
||||
BlogPostFormValidator.descriptionTooShort,
|
||||
);
|
||||
expect(BlogPostFormValidator.description('xyz'), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('BlogPostFormValidator.videoUrl', () {
|
||||
test('allows empty', () {
|
||||
expect(BlogPostFormValidator.videoUrl(''), isNull);
|
||||
expect(BlogPostFormValidator.videoUrl(' '), isNull);
|
||||
});
|
||||
|
||||
test('rejects unsupported hosts', () {
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://youtube.com/watch?v=abc'),
|
||||
BlogPostFormValidator.videoUrlUnsupportedHost,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://rutube.ru.evil.com/video/abc'),
|
||||
BlogPostFormValidator.videoUrlUnsupportedHost,
|
||||
);
|
||||
});
|
||||
|
||||
test('accepts rutube / vk / ok formats', () {
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://rutube.ru/video/abc123/'),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://www.rutube.ru/shorts/abc123'),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://vk.com/video-123_456'),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://vkvideo.ru/video123_456/'),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://ok.ru/video/12345'),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects invalid path formats for allowed hosts', () {
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://rutube.ru/channel/abc'),
|
||||
BlogPostFormValidator.videoUrlRutubeFormat,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://vk.com/feed'),
|
||||
BlogPostFormValidator.videoUrlVkFormat,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('https://ok.ru/profile/1'),
|
||||
BlogPostFormValidator.videoUrlOkFormat,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects malformed urls', () {
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('not-a-url'),
|
||||
BlogPostFormValidator.videoUrlInvalid,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.videoUrl('ftp://rutube.ru/video/abc'),
|
||||
BlogPostFormValidator.videoUrlInvalid,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('BlogPostFormValidator.contentRequiredMessage', () {
|
||||
test('requires at least one filled field', () {
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: '',
|
||||
description: '',
|
||||
videoUrl: '',
|
||||
imageCount: 0,
|
||||
),
|
||||
BlogPostFormValidator.contentRequired,
|
||||
);
|
||||
});
|
||||
|
||||
test('accepts any valid content source', () {
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: 'abc',
|
||||
description: '',
|
||||
videoUrl: '',
|
||||
imageCount: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: '',
|
||||
description: 'abc',
|
||||
videoUrl: '',
|
||||
imageCount: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: '',
|
||||
description: '',
|
||||
videoUrl: '',
|
||||
imageCount: 1,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: '',
|
||||
description: '',
|
||||
videoUrl: 'https://rutube.ru/video/abc123/',
|
||||
imageCount: 0,
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not count short title or invalid video as content', () {
|
||||
expect(
|
||||
BlogPostFormValidator.contentRequiredMessage(
|
||||
title: 'ab',
|
||||
description: '',
|
||||
videoUrl: 'https://youtube.com/watch?v=1',
|
||||
imageCount: 0,
|
||||
),
|
||||
BlogPostFormValidator.contentRequired,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('BlogPostFormValidator.imagePaths', () {
|
||||
test('allows jpg/jpeg/png', () {
|
||||
expect(
|
||||
BlogPostFormValidator.imagePaths(['a.jpg', 'b.JPEG', 'c.png']),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects other extensions', () {
|
||||
expect(
|
||||
BlogPostFormValidator.imagePaths(['a.webp']),
|
||||
BlogPostFormValidator.imageTypeInvalid,
|
||||
);
|
||||
expect(
|
||||
BlogPostFormValidator.imagePaths(['noext']),
|
||||
BlogPostFormValidator.imageTypeInvalid,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user