Skip to content

Commit d4ed0f0

Browse files
committed
fix(models): improve copyWith methods for AppReview, Engagement, and Report
- Update copyWith methods to use null-aware assignment for most fields - Add missing fields to copyWith methods in Engagement - Reorder parameters in Report.copyWith to match field order - Update related tests to use ValueWrapper for wrapped fields
1 parent 8bae6eb commit d4ed0f0

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

lib/src/models/user_generated_content/app_review.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,25 @@ class AppReview extends Equatable {
106106

107107
/// Creates a copy of this [AppReview] with updated values.
108108
AppReview copyWith({
109+
String? id,
110+
String? userId,
109111
AppReviewFeedback? feedback,
112+
DateTime? createdAt,
110113
DateTime? updatedAt,
111114
bool? wasStoreReviewRequested,
112-
String? feedbackDetails,
115+
ValueWrapper<String?>? feedbackDetails,
113116
}) {
114117
return AppReview(
115-
id: id,
116-
userId: userId,
118+
id: id ?? this.id,
119+
userId: userId ?? this.userId,
117120
feedback: feedback ?? this.feedback,
118-
createdAt: createdAt,
121+
createdAt: createdAt ?? this.createdAt,
119122
updatedAt: updatedAt ?? this.updatedAt,
120123
wasStoreReviewRequested:
121124
wasStoreReviewRequested ?? this.wasStoreReviewRequested,
122-
feedbackDetails: feedbackDetails ?? this.feedbackDetails,
125+
feedbackDetails: feedbackDetails != null
126+
? feedbackDetails.value
127+
: this.feedbackDetails,
123128
);
124129
}
125130
}

lib/src/models/user_generated_content/engagement.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:core/core.dart';
12
import 'package:core/src/enums/engageable_type.dart';
23
import 'package:core/src/models/user_generated_content/comment.dart';
34
import 'package:core/src/models/user_generated_content/reaction.dart';
@@ -74,15 +75,23 @@ class Engagement extends Equatable {
7475
];
7576

7677
/// Creates a copy of this [Engagement] with updated values.
77-
Engagement copyWith({Reaction? reaction, Comment? comment}) {
78+
Engagement copyWith({
79+
String? id,
80+
String? userId,
81+
String? entityId,
82+
EngageableType? entityType,
83+
Reaction? reaction,
84+
ValueWrapper<Comment?>? comment,
85+
DateTime? createdAt,
86+
}) {
7887
return Engagement(
79-
id: id,
80-
userId: userId,
81-
entityId: entityId,
82-
entityType: entityType,
88+
id: id ?? this.id,
89+
userId: userId ?? this.userId,
90+
entityId: entityId ?? this.entityId,
91+
entityType: entityType ?? this.entityType,
8392
reaction: reaction ?? this.reaction,
84-
comment: comment ?? this.comment,
85-
createdAt: createdAt,
93+
comment: comment != null ? comment.value : this.comment,
94+
createdAt: createdAt ?? this.createdAt,
8695
updatedAt: DateTime.now(),
8796
);
8897
}

lib/src/models/user_generated_content/report.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ class Report extends Equatable {
105105
reporterUserId: reporterUserId ?? this.reporterUserId,
106106
entityType: entityType ?? this.entityType,
107107
entityId: entityId ?? this.entityId,
108-
reason: reason ?? this.reason,
109108
status: status ?? this.status,
110109
additionalComments: additionalComments != null
111110
? additionalComments.value
112111
: this.additionalComments,
113112
createdAt: createdAt ?? this.createdAt,
113+
reason: reason ?? this.reason,
114114
);
115115
}
116116
}

test/src/models/user_generated_content/app_review_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void main() {
6363

6464
final updatedReview = negativeReviewWithReason.copyWith(
6565
updatedAt: newTimestamp,
66-
feedbackDetails: newDetails,
66+
feedbackDetails: const ValueWrapper(newDetails),
6767
);
6868

6969
expect(updatedReview.updatedAt, newTimestamp);
@@ -74,6 +74,15 @@ void main() {
7474
expect(updatedReview.createdAt, negativeReviewWithReason.createdAt);
7575
});
7676

77+
test('allows setting feedbackDetails to null', () {
78+
final updatedReview = negativeReviewWithReason.copyWith(
79+
feedbackDetails: const ValueWrapper(null),
80+
);
81+
82+
expect(updatedReview.feedbackDetails, isNull);
83+
expect(updatedReview.id, negativeReviewWithReason.id);
84+
});
85+
7786
test('correctly uses initialFeedback in copyWith', () {
7887
final updatedReview = positiveReview.copyWith(
7988
feedback: AppReviewFeedback.negative,

test/src/models/user_generated_content/engagement_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ void main() {
6868
);
6969
});
7070

71+
test('returns a new instance with an updated comment', () {
72+
final newComment = Comment(
73+
language: languagesFixturesData.first,
74+
content: 'This is a new comment.',
75+
status: ModerationStatus.resolved,
76+
);
77+
78+
final updatedEngagement = engagementFixture.copyWith(
79+
comment: ValueWrapper(newComment),
80+
);
81+
82+
expect(updatedEngagement.comment, newComment);
83+
expect(updatedEngagement.id, engagementFixture.id);
84+
});
85+
86+
test('returns a new instance with a null comment', () {
87+
// Start with a fixture that has a comment.
88+
final updatedEngagement = engagementFixture.copyWith(
89+
comment: const ValueWrapper(null),
90+
);
91+
92+
expect(updatedEngagement.comment, isNull);
93+
// Verify other fields remain unchanged
94+
expect(updatedEngagement.id, engagementFixture.id);
95+
});
96+
7197
test(
7298
'returns a new instance with a new timestamp if no updates provided',
7399
() {

0 commit comments

Comments
 (0)