mnemo_cards/mnemo_cards_common/test/user_dto_telegram_test.dart

122 lines
3.6 KiB
Dart
Raw Normal View History

2025-12-19 23:38:51 +00:00
import 'package:test/test.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
void main() {
group('UserDto telegram field', () {
test('should serialize and deserialize with telegram', () {
final userDto = UserDto(
id: 'test-id',
name: 'Test User',
email: 'test@example.com',
telegram: '@testuser',
admin: false,
packs: ['pack1', 'pack2'],
purchases: ['purchase1'],
);
final json = userDto.toJson();
expect(json['telegram'], equals('@testuser'));
expect(json['email'], equals('test@example.com'));
final deserialized = UserDto.fromJson(json);
expect(deserialized.telegram, equals('@testuser'));
expect(deserialized.email, equals('test@example.com'));
expect(deserialized.name, equals('Test User'));
});
test('should handle null telegram', () {
final userDto = UserDto(
id: 'test-id',
name: 'Test User',
email: 'test@example.com',
telegram: null,
admin: false,
);
final json = userDto.toJson();
expect(json['telegram'], isNull);
final deserialized = UserDto.fromJson(json);
expect(deserialized.telegram, isNull);
expect(deserialized.email, equals('test@example.com'));
});
test('should handle missing telegram in JSON', () {
final json = {
'id': 'test-id',
'name': 'Test User',
'email': 'test@example.com',
'admin': false,
'packs': <String>[],
'purchases': <String>[],
};
final userDto = UserDto.fromJson(json);
expect(userDto.telegram, isNull);
expect(userDto.email, equals('test@example.com'));
});
test('should support telegram without email for Telegram-only users', () {
final userDto = UserDto(
id: 'test-id',
name: 'Telegram User',
email: null,
telegram: '@telegram_only',
admin: false,
);
final json = userDto.toJson();
expect(json['telegram'], equals('@telegram_only'));
expect(json['email'], isNull);
final deserialized = UserDto.fromJson(json);
expect(deserialized.telegram, equals('@telegram_only'));
expect(deserialized.email, isNull);
});
test('should work with copyWith for telegram', () {
final userDto = UserDto(
id: 'test-id',
name: 'Test User',
email: 'test@example.com',
telegram: '@oldusername',
admin: false,
);
final updated = userDto.copyWith(telegram: '@newusername');
expect(updated.telegram, equals('@newusername'));
expect(updated.email, equals('test@example.com'));
expect(userDto.telegram, equals('@oldusername')); // Original unchanged
});
test('should handle both email and telegram contact methods', () {
final userDto = UserDto(
id: 'test-id',
name: 'Test User',
email: 'user@example.com',
telegram: '@testuser',
admin: true,
packs: ['pack1'],
purchases: ['purchase1'],
);
expect(userDto.email, equals('user@example.com'));
expect(userDto.telegram, equals('@testuser'));
expect(userDto.admin, isTrue);
final json = userDto.toJson();
final deserialized = UserDto.fromJson(json);
expect(deserialized.email, equals('user@example.com'));
expect(deserialized.telegram, equals('@testuser'));
expect(deserialized.admin, isTrue);
});
test('empty UserDto should have null telegram', () {
final empty = UserDto.empty;
expect(empty.telegram, isNull);
expect(empty.email, isNull);
});
});
}