|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:json_annotation/json_annotation.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | +import 'package:uuid/uuid.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('LocalAd', () { |
| 8 | + const testId = 'test-local-ad-id'; |
| 9 | + const testTitle = 'Test Ad Title'; |
| 10 | + const testSubtitle = 'Test Ad Subtitle'; |
| 11 | + const testImageUrl = 'http://example.com/local_ad.jpg'; |
| 12 | + const testTargetUrl = 'http://example.com/local_target'; |
| 13 | + const testAdType = AdType.banner; |
| 14 | + |
| 15 | + LocalAd createSubject({ |
| 16 | + String? id, |
| 17 | + String title = testTitle, |
| 18 | + String subtitle = testSubtitle, |
| 19 | + String imageUrl = testImageUrl, |
| 20 | + String targetUrl = testTargetUrl, |
| 21 | + AdType adType = testAdType, |
| 22 | + }) { |
| 23 | + return LocalAd( |
| 24 | + id: id ?? const Uuid().v4(), |
| 25 | + title: title, |
| 26 | + subtitle: subtitle, |
| 27 | + imageUrl: imageUrl, |
| 28 | + targetUrl: targetUrl, |
| 29 | + adType: adType, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + group('constructor', () { |
| 34 | + test('generates id when not provided', () { |
| 35 | + final localAd = createSubject(); |
| 36 | + expect(localAd.id, isA<String>()); |
| 37 | + expect(Uuid.isValidUUID(fromString: localAd.id), isTrue); |
| 38 | + }); |
| 39 | + |
| 40 | + test('uses provided id', () { |
| 41 | + final localAd = createSubject(id: testId); |
| 42 | + expect(localAd.id, testId); |
| 43 | + }); |
| 44 | + |
| 45 | + test('initializes all properties correctly', () { |
| 46 | + final localAd = createSubject(); |
| 47 | + expect(localAd.title, testTitle); |
| 48 | + expect(localAd.subtitle, testSubtitle); |
| 49 | + expect(localAd.imageUrl, testImageUrl); |
| 50 | + expect(localAd.targetUrl, testTargetUrl); |
| 51 | + expect(localAd.adType, testAdType); |
| 52 | + expect(localAd.type, 'localAd'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + group('copyWith', () { |
| 57 | + test('returns a new instance with updated fields', () { |
| 58 | + const newTitle = 'New Title'; |
| 59 | + const newAdType = AdType.video; |
| 60 | + |
| 61 | + final originalLocalAd = createSubject(); |
| 62 | + final updatedLocalAd = originalLocalAd.copyWith( |
| 63 | + title: newTitle, |
| 64 | + adType: newAdType, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(updatedLocalAd.id, originalLocalAd.id); |
| 68 | + expect(updatedLocalAd.title, newTitle); |
| 69 | + expect(updatedLocalAd.subtitle, originalLocalAd.subtitle); |
| 70 | + expect(updatedLocalAd.imageUrl, originalLocalAd.imageUrl); |
| 71 | + expect(updatedLocalAd.targetUrl, originalLocalAd.targetUrl); |
| 72 | + expect(updatedLocalAd.adType, newAdType); |
| 73 | + expect(updatedLocalAd.type, originalLocalAd.type); |
| 74 | + }); |
| 75 | + |
| 76 | + test('returns an identical copy if no updates provided', () { |
| 77 | + final originalLocalAd = createSubject(); |
| 78 | + final copiedLocalAd = originalLocalAd.copyWith(); |
| 79 | + expect(copiedLocalAd, originalLocalAd); |
| 80 | + expect(identical(copiedLocalAd, originalLocalAd), isFalse); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + group('toJson', () { |
| 85 | + test('serializes full LocalAd object to JSON', () { |
| 86 | + final localAd = createSubject(); |
| 87 | + final json = localAd.toJson(); |
| 88 | + |
| 89 | + expect(json, <String, dynamic>{ |
| 90 | + 'id': localAd.id, |
| 91 | + 'title': testTitle, |
| 92 | + 'subtitle': testSubtitle, |
| 93 | + 'imageUrl': testImageUrl, |
| 94 | + 'targetUrl': testTargetUrl, |
| 95 | + 'adType': 'banner', |
| 96 | + 'type': 'localAd', |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + group('fromJson', () { |
| 102 | + test('deserializes full JSON to LocalAd object', () { |
| 103 | + final json = <String, dynamic>{ |
| 104 | + 'id': testId, |
| 105 | + 'title': testTitle, |
| 106 | + 'subtitle': testSubtitle, |
| 107 | + 'imageUrl': testImageUrl, |
| 108 | + 'targetUrl': testTargetUrl, |
| 109 | + 'adType': 'banner', |
| 110 | + 'type': 'localAd', |
| 111 | + }; |
| 112 | + final localAd = LocalAd.fromJson(json); |
| 113 | + |
| 114 | + expect(localAd.id, testId); |
| 115 | + expect(localAd.title, testTitle); |
| 116 | + expect(localAd.subtitle, testSubtitle); |
| 117 | + expect(localAd.imageUrl, testImageUrl); |
| 118 | + expect(localAd.targetUrl, testTargetUrl); |
| 119 | + expect(localAd.adType, testAdType); |
| 120 | + expect(localAd.type, 'localAd'); |
| 121 | + }); |
| 122 | + |
| 123 | + test('deserializes JSON with unknown adType gracefully', () { |
| 124 | + final json = <String, dynamic>{ |
| 125 | + 'id': testId, |
| 126 | + 'title': testTitle, |
| 127 | + 'subtitle': testSubtitle, |
| 128 | + 'imageUrl': testImageUrl, |
| 129 | + 'targetUrl': testTargetUrl, |
| 130 | + 'adType': 'unknown_type', |
| 131 | + 'type': 'localAd', |
| 132 | + }; |
| 133 | + expect( |
| 134 | + () => LocalAd.fromJson(json), |
| 135 | + throwsA(isA<CheckedFromJsonException>()), |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + group('Equatable', () { |
| 141 | + test('instances with same properties are equal', () { |
| 142 | + final localAd1 = createSubject(id: '1'); |
| 143 | + final localAd2 = createSubject(id: '1'); |
| 144 | + expect(localAd1, localAd2); |
| 145 | + }); |
| 146 | + |
| 147 | + test('instances with different properties are not equal', () { |
| 148 | + final localAd1 = createSubject(id: '1'); |
| 149 | + final localAd2 = createSubject(id: '2'); |
| 150 | + expect(localAd1, isNot(equals(localAd2))); |
| 151 | + }); |
| 152 | + |
| 153 | + test('props list contains all relevant fields', () { |
| 154 | + final localAd = createSubject(); |
| 155 | + expect(localAd.props, [ |
| 156 | + localAd.id, |
| 157 | + localAd.title, |
| 158 | + localAd.subtitle, |
| 159 | + localAd.imageUrl, |
| 160 | + localAd.targetUrl, |
| 161 | + localAd.adType, |
| 162 | + localAd.type, |
| 163 | + ]); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | +} |
0 commit comments