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

253 lines
6.4 KiB
Dart

import 'package:eatvio_app/features/profile/models/sub_model.dart';
import 'package:eatvio_app/core/models/pagination_model.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('SubPaginationModel', () {
test('creates SubPaginationModel with empty list', () {
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: null,
lastPage: 1,
links: [],
path: '/api/subs',
perPage: 10,
to: null,
total: 0,
);
const model = SubPaginationModel(
data: [],
links: links,
meta: meta,
);
expect(model.data, isEmpty);
expect(model.meta.total, 0);
});
test('creates SubPaginationModel with subs', () {
final subs = [
SubModel(
slug: 'user1',
name: 'User One',
city: 'New York',
birthdate: DateTime(1990, 1, 1),
isEntitySubscribedByMe: true,
profilePhotoUrl: 'photo1.jpg',
roles: ['consultant'],
),
SubModel(
slug: 'user2',
name: 'User Two',
city: 'London',
birthdate: DateTime(1995, 5, 15),
isEntitySubscribedByMe: false,
profilePhotoUrl: 'photo2.jpg',
roles: ['user'],
),
];
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: 1,
lastPage: 1,
links: [],
path: '/api/subs',
perPage: 10,
to: 2,
total: 2,
);
final model = SubPaginationModel(
data: subs,
links: links,
meta: meta,
);
expect(model.data.length, 2);
expect(model.data[0].slug, 'user1');
expect(model.data[0].name, 'User One');
expect(model.data[1].slug, 'user2');
expect(model.data[1].name, 'User Two');
});
test('creates SubPaginationModel from JSON', () {
final json = {
'data': [
{
'slug': 'user1',
'name': 'User One',
'city': 'New York',
'birthdate': '1990-01-01T00:00:00Z',
'is_entity_subscribed_by_me': true,
'profile_photo_url': 'photo1.jpg',
'roles': ['consultant'],
},
],
'links': {
'first': 'http://example.com?page=1',
'last': 'http://example.com?page=1',
'prev': null,
'next': null,
},
'meta': {
'current_page': 1,
'from': 1,
'last_page': 1,
'links': [],
'path': '/api/subs',
'per_page': 10,
'to': 1,
'total': 1,
},
};
final model = SubPaginationModel.fromJson(json);
expect(model.data.length, 1);
expect(model.data[0].slug, 'user1');
expect(model.data[0].name, 'User One');
});
});
group('SubModel', () {
test('creates SubModel with all parameters', () {
final sub = SubModel(
slug: 'user1',
name: 'User One',
city: 'New York',
birthdate: DateTime(1990, 1, 1),
isEntitySubscribedByMe: true,
profilePhotoUrl: 'photo1.jpg',
roles: ['consultant', 'nutritionist'],
);
expect(sub.slug, 'user1');
expect(sub.name, 'User One');
expect(sub.city, 'New York');
expect(sub.birthdate, DateTime(1990, 1, 1));
expect(sub.isEntitySubscribedByMe, true);
expect(sub.profilePhotoUrl, 'photo1.jpg');
expect(sub.roles.length, 2);
expect(sub.roles, contains('consultant'));
expect(sub.roles, contains('nutritionist'));
});
test('creates SubModel with empty roles', () {
final sub = SubModel(
slug: 'user2',
name: 'User Two',
city: 'London',
birthdate: DateTime(1995, 5, 15),
isEntitySubscribedByMe: false,
profilePhotoUrl: 'photo2.jpg',
roles: [],
);
expect(sub.roles, isEmpty);
});
test('creates SubModel from JSON', () {
final json = {
'slug': 'user1',
'name': 'User One',
'city': 'New York',
'birthdate': '1990-01-01T00:00:00Z',
'is_entity_subscribed_by_me': true,
'profile_photo_url': 'photo1.jpg',
'roles': ['consultant'],
};
final sub = SubModel.fromJson(json);
expect(sub.slug, 'user1');
expect(sub.name, 'User One');
expect(sub.isEntitySubscribedByMe, true);
});
test('toJson serializes SubModel correctly', () {
final sub = SubModel(
slug: 'user1',
name: 'User One',
city: 'New York',
birthdate: DateTime(1990, 1, 1),
isEntitySubscribedByMe: true,
profilePhotoUrl: 'photo1.jpg',
roles: ['consultant'],
);
final json = sub.toJson();
expect(json['slug'], 'user1');
expect(json['name'], 'User One');
expect(json['city'], 'New York');
expect(json['is_entity_subscribed_by_me'], isTrue);
expect(json['profile_photo_url'], 'photo1.jpg');
expect(json['roles'], ['consultant']);
expect(json.containsKey('birthdate'), isTrue);
});
});
group('SubPaginationModel', () {
test('toJson serializes SubPaginationModel correctly', () {
final subs = [
SubModel(
slug: 'user1',
name: 'User One',
city: 'New York',
birthdate: DateTime(1990, 1, 1),
isEntitySubscribedByMe: true,
profilePhotoUrl: 'photo1.jpg',
roles: ['consultant'],
),
];
const links = Links(
first: 'http://example.com?page=1',
last: 'http://example.com?page=1',
prev: null,
next: null,
);
const meta = Meta(
currentPage: 1,
from: 1,
lastPage: 1,
links: [],
path: '/api/subs',
perPage: 10,
to: 1,
total: 1,
);
final model = SubPaginationModel(
data: subs,
links: links,
meta: meta,
);
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);
});
});
}