|
| 1 | +import 'package:ht_shared/src/models/feed/ad.dart'; |
| 2 | +import 'package:ht_shared/src/models/content_type.dart'; |
| 3 | +import 'package:ht_shared/src/models/feed/ad_placement.dart'; |
| 4 | +import 'package:ht_shared/src/models/feed/feed_item_action.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | +import 'package:uuid/uuid.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('Ad', () { |
| 10 | + const testId = 'test-ad-id'; |
| 11 | + const testImageUrl = 'http://example.com/ad.jpg'; |
| 12 | + const testTargetUrl = 'http://example.com/target'; |
| 13 | + const testAdType = AdType.banner; |
| 14 | + const testPlacement = AdPlacement.feedInlineStandardBanner; |
| 15 | + const defaultAction = OpenExternalUrl(url: 'http://default.com'); |
| 16 | + |
| 17 | + Ad createSubject({ |
| 18 | + String? id, |
| 19 | + String imageUrl = testImageUrl, |
| 20 | + String targetUrl = testTargetUrl, |
| 21 | + AdType adType = testAdType, |
| 22 | + AdPlacement? placement = testPlacement, |
| 23 | + FeedItemAction action = defaultAction, |
| 24 | + }) { |
| 25 | + return Ad( |
| 26 | + id: id, |
| 27 | + imageUrl: imageUrl, |
| 28 | + targetUrl: targetUrl, |
| 29 | + adType: adType, |
| 30 | + placement: placement, |
| 31 | + action: action, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + group('constructor', () { |
| 36 | + test('generates id when not provided', () { |
| 37 | + final ad = createSubject(id: null); |
| 38 | + expect(ad.id, isA<String>()); |
| 39 | + expect(Uuid.isValidUUID(fromString: ad.id), isTrue); |
| 40 | + }); |
| 41 | + |
| 42 | + test('uses provided id', () { |
| 43 | + final ad = createSubject(id: testId); |
| 44 | + expect(ad.id, testId); |
| 45 | + }); |
| 46 | + |
| 47 | + test('initializes all properties correctly', () { |
| 48 | + final ad = createSubject(); |
| 49 | + expect(ad.imageUrl, testImageUrl); |
| 50 | + expect(ad.targetUrl, testTargetUrl); |
| 51 | + expect(ad.adType, testAdType); |
| 52 | + expect(ad.placement, testPlacement); |
| 53 | + expect(ad.action, defaultAction); |
| 54 | + expect(ad.type, 'ad'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + group('copyWith', () { |
| 59 | + test('returns a new instance with updated fields', () { |
| 60 | + const newImageUrl = 'http://new.com/ad.png'; |
| 61 | + const newAdType = AdType.video; |
| 62 | + const newAction = OpenInternalContent( |
| 63 | + contentId: 'new-content', |
| 64 | + contentType: ContentType.headline, |
| 65 | + ); |
| 66 | + |
| 67 | + final originalAd = createSubject(); |
| 68 | + final updatedAd = originalAd.copyWith( |
| 69 | + imageUrl: newImageUrl, |
| 70 | + adType: newAdType, |
| 71 | + action: newAction, |
| 72 | + ); |
| 73 | + |
| 74 | + expect(updatedAd.id, originalAd.id); |
| 75 | + expect(updatedAd.imageUrl, newImageUrl); |
| 76 | + expect(updatedAd.targetUrl, originalAd.targetUrl); |
| 77 | + expect(updatedAd.adType, newAdType); |
| 78 | + expect(updatedAd.placement, originalAd.placement); |
| 79 | + expect(updatedAd.action, newAction); |
| 80 | + expect(updatedAd.type, originalAd.type); |
| 81 | + }); |
| 82 | + |
| 83 | + test('returns an identical copy if no updates provided', () { |
| 84 | + final originalAd = createSubject(); |
| 85 | + final copiedAd = originalAd.copyWith(); |
| 86 | + expect(copiedAd, originalAd); |
| 87 | + expect(identical(copiedAd, originalAd), isFalse); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + group('toJson', () { |
| 92 | + test('serializes full Ad object to JSON', () { |
| 93 | + final ad = createSubject(); |
| 94 | + final json = ad.toJson(); |
| 95 | + |
| 96 | + expect(json, <String, dynamic>{ |
| 97 | + 'id': ad.id, |
| 98 | + 'imageUrl': testImageUrl, |
| 99 | + 'targetUrl': testTargetUrl, |
| 100 | + 'adType': testAdType.name, |
| 101 | + 'placement': testPlacement.name, |
| 102 | + 'type': 'ad', |
| 103 | + 'action': defaultAction.toJson(), |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + test('omits null optional fields from JSON', () { |
| 108 | + final ad = createSubject(placement: null); |
| 109 | + final json = ad.toJson(); |
| 110 | + |
| 111 | + expect(json.containsKey('placement'), isFalse); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + group('fromJson', () { |
| 116 | + test('deserializes full JSON to Ad object', () { |
| 117 | + final json = <String, dynamic>{ |
| 118 | + 'id': testId, |
| 119 | + 'imageUrl': testImageUrl, |
| 120 | + 'targetUrl': testTargetUrl, |
| 121 | + 'adType': testAdType.name, |
| 122 | + 'placement': testPlacement.name, |
| 123 | + 'type': 'ad', |
| 124 | + 'action': defaultAction.toJson(), |
| 125 | + }; |
| 126 | + final ad = Ad.fromJson(json); |
| 127 | + |
| 128 | + expect(ad.id, testId); |
| 129 | + expect(ad.imageUrl, testImageUrl); |
| 130 | + expect(ad.targetUrl, testTargetUrl); |
| 131 | + expect(ad.adType, testAdType); |
| 132 | + expect(ad.placement, testPlacement); |
| 133 | + expect(ad.action, defaultAction); |
| 134 | + expect(ad.type, 'ad'); |
| 135 | + }); |
| 136 | + |
| 137 | + test('deserializes JSON with missing optional fields', () { |
| 138 | + final json = <String, dynamic>{ |
| 139 | + 'id': testId, |
| 140 | + 'imageUrl': testImageUrl, |
| 141 | + 'targetUrl': testTargetUrl, |
| 142 | + 'adType': testAdType.name, |
| 143 | + 'type': 'ad', |
| 144 | + 'action': defaultAction.toJson(), |
| 145 | + }; |
| 146 | + final ad = Ad.fromJson(json); |
| 147 | + |
| 148 | + expect(ad.placement, isNull); |
| 149 | + }); |
| 150 | + |
| 151 | + test('deserializes JSON with unknown adType gracefully', () { |
| 152 | + final json = <String, dynamic>{ |
| 153 | + 'id': testId, |
| 154 | + 'imageUrl': testImageUrl, |
| 155 | + 'targetUrl': testTargetUrl, |
| 156 | + 'adType': 'unknown_type', |
| 157 | + 'type': 'ad', |
| 158 | + 'action': defaultAction.toJson(), |
| 159 | + }; |
| 160 | + final ad = Ad.fromJson(json); |
| 161 | + expect(ad.adType, isNull); // Should be null for unknown enum value |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + group('Equatable', () { |
| 166 | + test('instances with same properties are equal', () { |
| 167 | + final ad1 = createSubject(id: '1'); |
| 168 | + final ad2 = createSubject(id: '1'); |
| 169 | + expect(ad1, ad2); |
| 170 | + }); |
| 171 | + |
| 172 | + test('instances with different properties are not equal', () { |
| 173 | + final ad1 = createSubject(id: '1'); |
| 174 | + final ad2 = createSubject(id: '2'); |
| 175 | + expect(ad1, isNot(equals(ad2))); |
| 176 | + }); |
| 177 | + |
| 178 | + test('props list contains all relevant fields', () { |
| 179 | + final ad = createSubject(); |
| 180 | + expect(ad.props, [ |
| 181 | + ad.id, |
| 182 | + ad.imageUrl, |
| 183 | + ad.targetUrl, |
| 184 | + ad.adType, |
| 185 | + ad.placement, |
| 186 | + ad.type, |
| 187 | + ad.action, |
| 188 | + ]); |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | +} |
0 commit comments