Files
eatvio_app/test/features/feed/models/plan_pagination_models_test.dart
2026-01-14 18:47:11 +03:00

357 lines
12 KiB
Dart

import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/models/plan_pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('PlanType', () {
test('hasAtLeastOneType returns true when fit is true', () {
const planType = PlanType(fit: true, slim: null, behavior: null, wellness: null);
expect(planType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when slim is true', () {
const planType = PlanType(fit: null, slim: true, behavior: null, wellness: null);
expect(planType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when behavior is true', () {
const planType = PlanType(fit: null, slim: null, behavior: true, wellness: null);
expect(planType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns true when wellness is true', () {
const planType = PlanType(fit: null, slim: null, behavior: null, wellness: true);
expect(planType.hasAtLeastOneType(), isTrue);
});
test('hasAtLeastOneType returns false when all types are false', () {
const planType = PlanType(fit: false, slim: false, behavior: false, wellness: false);
expect(planType.hasAtLeastOneType(), isFalse);
});
test('hasAtLeastOneType returns false when all types are null', () {
const planType = PlanType(fit: null, slim: null, behavior: null, wellness: null);
expect(planType.hasAtLeastOneType(), isFalse);
});
test('fromJson parses all fields correctly', () {
final json = {'fit': true, 'slim': false, 'behavior': true, 'wellness': false};
final model = PlanType.fromJson(json);
expect(model.fit, isTrue);
expect(model.slim, isFalse);
expect(model.behavior, isTrue);
expect(model.wellness, isFalse);
});
test('toJson serializes all fields correctly', () {
const model = PlanType(fit: true, slim: false, behavior: true, wellness: false);
final json = model.toJson();
expect(json['fit'], isTrue);
expect(json['slim'], isFalse);
expect(json['behavior'], isTrue);
expect(json['wellness'], isFalse);
});
});
group('PacketOptions', () {
test('empty factory creates instance with default values', () {
final options = PacketOptions.empty();
expect(options.items, isEmpty);
expect(options.price, 1200.0);
expect(options.short, '');
expect(options.description, '');
expect(options.hasPersonalDiet, isFalse);
});
test('fromJson parses all fields correctly', () {
final json = {
'items': ['item1', 'item2'],
'price': 1500,
'short': 'Short description',
'description_md': 'Full description',
'has_personal_diet': true,
};
final model = PacketOptions.fromJson(json);
expect(model.items, ['item1', 'item2']);
expect(model.price, 1500);
expect(model.short, 'Short description');
expect(model.description, 'Full description');
expect(model.hasPersonalDiet, isTrue);
});
test('fromJson parses items from Map', () {
final json = {
'items': {'key1': 'item1', 'key2': 'item2'},
'price': 1000,
'short': 'Short',
'description_md': 'Description',
'has_personal_diet': false,
};
final model = PacketOptions.fromJson(json);
expect(model.items.length, 2);
expect(model.items, contains('item1'));
expect(model.items, contains('item2'));
});
test('fromJson parses has_personal_diet as string "1"', () {
final json = {'items': [], 'price': 1000, 'short': 'Short', 'description_md': 'Description', 'has_personal_diet': '1'};
final model = PacketOptions.fromJson(json);
expect(model.hasPersonalDiet, isTrue);
});
test('fromJson parses has_personal_diet as string "0"', () {
final json = {'items': [], 'price': 1000, 'short': 'Short', 'description_md': 'Description', 'has_personal_diet': '0'};
final model = PacketOptions.fromJson(json);
expect(model.hasPersonalDiet, isFalse);
});
test('toJson serializes all fields correctly', () {
const model = PacketOptions(items: ['item1', 'item2'], price: 1500, short: 'Short', description: 'Description', hasPersonalDiet: true);
final json = model.toJson();
expect(json['items'], ['item1', 'item2']);
expect(json['price'], 1500);
expect(json['short'], 'Short');
expect(json['description_md'], 'Description');
expect(json['has_personal_diet'], isTrue);
});
});
group('PlanModel', () {
test('fromJson parses all fields correctly', () {
final json = {
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 30,
'state': 'active',
'type': {'fit': true, 'slim': false, 'behavior': false, 'wellness': false},
'description_md': 'Test description',
'packets_count': 3,
'packet1_options': {
'items': ['item1'],
'price': 1000,
'short': 'Short',
'description_md': 'Description',
'has_personal_diet': true,
},
'author': {'author_slug': 'author-slug', 'author_name': 'Author Name', 'author_profile_icon': 'https://example.com/icon.jpg', 'author_is_me': false},
'total_likes': 100,
'total_views': 1000,
'total_comments': 50,
'is_favorite': true,
'min_price_of_options': 1000.0,
'max_price_of_options': 3000.0,
'was_liked_by_me': true,
};
final model = PlanModel.fromJson(json);
expect(model.id, 1);
expect(model.slug, 'test-plan');
expect(model.name, 'Test Plan');
expect(model.duration, 30);
expect(model.state, 'active');
expect(model.type.fit, isTrue);
expect(model.packet1Options, isNotNull);
expect(model.packet1Options!.items, ['item1']);
expect(model.author.authorSlug, 'author-slug');
expect(model.totalLikes, 100);
expect(model.isFavorite, isTrue);
});
test('fromJson handles null packet options', () {
final json = {
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 30,
'state': 'active',
'type': {'fit': true, 'slim': false, 'behavior': false, 'wellness': false},
'description_md': 'Test description',
'packets_count': 3,
'packet1_options': null,
'packet2_options': null,
'packet3_options': null,
'author': {'author_slug': 'author-slug', 'author_name': 'Author Name', 'author_profile_icon': 'https://example.com/icon.jpg', 'author_is_me': false},
'total_likes': 100,
'total_views': 1000,
'total_comments': 50,
'is_favorite': true,
'min_price_of_options': 1000.0,
'max_price_of_options': 3000.0,
'was_liked_by_me': true,
};
final model = PlanModel.fromJson(json);
expect(model.packet1Options, isNull);
expect(model.packet2Options, isNull);
expect(model.packet3Options, isNull);
});
test('fromJson handles type as List (returns default PlanType)', () {
final json = {
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 30,
'state': 'active',
'type': [],
'description_md': 'Test description',
'packets_count': 3,
'author': {'author_slug': 'author-slug', 'author_name': 'Author Name', 'author_profile_icon': 'https://example.com/icon.jpg', 'author_is_me': false},
'total_likes': 100,
'total_views': 1000,
'total_comments': 50,
'is_favorite': true,
'min_price_of_options': 1000.0,
'max_price_of_options': 3000.0,
'was_liked_by_me': true,
};
final model = PlanModel.fromJson(json);
expect(model.type.fit, isFalse);
expect(model.type.slim, isFalse);
expect(model.type.behavior, isFalse);
expect(model.type.wellness, isFalse);
});
test('toJson serializes PlanModel correctly', () {
const model = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 30,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
packet1Options: null,
packet2Options: null,
packet3Options: null,
author: Author(authorSlug: 'author-slug', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
totalLikes: 100,
totalViews: 1000,
totalComments: 50,
isFavorite: true,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 3000.0,
wasLikedByMe: true,
);
final json = model.toJson();
expect(json['id'], 1);
expect(json['slug'], 'test-plan');
expect(json['name'], 'Test Plan');
expect(json['duration'], 30);
expect(json['state'], 'active');
expect(json['total_likes'], 100);
expect(json['is_favorite'], isTrue);
});
});
group('PlanPaginationModel', () {
test('fromJson parses all fields correctly', () {
final json = {
'data': [
{
'id': 1,
'slug': 'test-plan',
'name': 'Test Plan',
'image': 'https://example.com/image.jpg',
'duration': 30,
'state': 'active',
'type': {'fit': true, 'slim': false, 'behavior': false, 'wellness': false},
'description_md': 'Test description',
'packets_count': 3,
'author': {
'author_slug': 'author-slug',
'author_name': 'Author Name',
'author_profile_icon': 'https://example.com/icon.jpg',
'author_is_me': false,
},
'total_likes': 100,
'total_views': 1000,
'total_comments': 50,
'is_favorite': true,
'min_price_of_options': 1000.0,
'max_price_of_options': 3000.0,
'was_liked_by_me': true,
},
],
'links': {'first': 'https://example.com/first', 'last': 'https://example.com/last', 'prev': null, 'next': 'https://example.com/next'},
'meta': {'current_page': 1, 'last_page': 10, 'per_page': 20, 'total': 200, 'from': null, 'to': null, 'links': []},
};
final model = PlanPaginationModel.fromJson(json);
expect(model.data.length, 1);
expect(model.data.first.id, 1);
expect(model.data.first.name, 'Test Plan');
expect(model.links.first, 'https://example.com/first');
expect(model.meta.currentPage, 1);
expect(model.meta.lastPage, 10);
});
test('toJson serializes PlanPaginationModel correctly', () {
const planModel = PlanModel(
id: 1,
slug: 'test-plan',
name: 'Test Plan',
image: 'https://example.com/image.jpg',
duration: 30,
state: 'active',
type: PlanType(fit: true, slim: false, behavior: false, wellness: false),
details: 'Test description',
packetsCount: 3,
packet1Options: null,
packet2Options: null,
packet3Options: null,
author: Author(authorSlug: 'author-slug', authorName: 'Author Name', authorProfileIcon: 'https://example.com/icon.jpg', authorIsMe: false),
totalLikes: 100,
totalViews: 1000,
totalComments: 50,
isFavorite: true,
minPriceOfOptions: 1000.0,
maxPriceOfOptions: 3000.0,
wasLikedByMe: true,
);
const model = PlanPaginationModel(
data: [planModel],
links: Links(first: 'https://example.com/first', last: 'https://example.com/last', prev: null, next: 'https://example.com/next'),
meta: Meta(currentPage: 1, lastPage: 10, perPage: 20, total: 200, from: null, to: null, links: []),
);
final json = model.toJson();
expect(json.containsKey('data'), isTrue);
expect((json['data'] as List).length, 1);
expect(json.containsKey('links'), isTrue);
expect(json.containsKey('meta'), isTrue);
});
});
}