Skip to content

Commit fecc0a3

Browse files
committed
test: sync
1 parent 3db96be commit fecc0a3

File tree

3 files changed

+178
-11
lines changed

3 files changed

+178
-11
lines changed

test/src/models/config/ad_config_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ void main() {
1212
adConfigFixture.platformAdIdentifiers,
1313
isA<Map<AdPlatformType, AdPlatformIdentifiers>>(),
1414
);
15-
expect(adConfigFixture.localAdsCatalog, isA<Map<String, LocalAd>>());
1615
expect(adConfigFixture.feedAdConfiguration, isA<FeedAdConfiguration>());
1716
expect(
1817
adConfigFixture.articleAdConfiguration,

test/src/models/core/feed_item_test.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ void main() {
88
final mockSource = sourcesFixturesData.first;
99
final mockCountry = countriesFixturesData.first;
1010

11-
const mockAd = Ad(
12-
id: 'ad-1',
13-
imageUrl: 'http://example.com/ad.jpg',
14-
targetUrl: 'http://example.com/ad-target',
11+
const mockLocalAd = LocalAd(
12+
id: 'local-ad-1',
13+
title: 'Test Local Ad',
14+
subtitle: 'This is a test local ad.',
15+
imageUrl: 'http://example.com/local_ad.jpg',
16+
targetUrl: 'http://example.com/local_ad_target',
1517
adType: AdType.banner,
16-
placement: AdPlacement.feedInlineStandardBanner,
1718
);
1819

1920
const mockCallToAction = CallToActionItem(
@@ -69,10 +70,10 @@ void main() {
6970
});
7071

7172
test('dispatches to Ad.fromJson', () {
72-
final json = mockAd.toJson();
73+
final json = mockLocalAd.toJson();
7374
final feedItem = FeedItem.fromJson(json);
74-
expect(feedItem, isA<Ad>());
75-
expect(feedItem, equals(mockAd));
75+
expect(feedItem, isA<LocalAd>());
76+
expect(feedItem, equals(mockLocalAd));
7677
});
7778

7879
test('dispatches to CallToAction.fromJson', () {
@@ -196,8 +197,8 @@ void main() {
196197
});
197198

198199
test('serializes Ad correctly', () {
199-
final json = mockAd.toJson();
200-
final deserialized = FeedItem.fromJson(json) as Ad;
200+
final json = mockLocalAd.toJson();
201+
final deserialized = FeedItem.fromJson(json) as LocalAd;
201202
expect(deserialized.toJson(), equals(json));
202203
});
203204

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)