Skip to content

Commit

Permalink
chore: Add tests for converting event types
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Jan 14, 2025
1 parent 83f993a commit e2b3f3d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
15 changes: 1 addition & 14 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,7 @@ class Event extends MatrixEvent {

@override
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
if (stateKey != null) data['state_key'] = stateKey;
if (prevContent?.isNotEmpty == true) {
data['prev_content'] = prevContent;
}
data['content'] = content;
data['type'] = type;
data['event_id'] = eventId;
data['room_id'] = roomId;
data['sender'] = senderId;
data['origin_server_ts'] = originServerTs.millisecondsSinceEpoch;
if (unsigned?.isNotEmpty == true) {
data['unsigned'] = unsigned;
}
final data = super.toJson();
if (originalSource != null) {
data['original_source'] = originalSource?.toJson();
}
Expand Down
33 changes: 33 additions & 0 deletions test/event_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2830,5 +2830,38 @@ void main() {
expect(event.onlyEmotes, false);
expect(event.numberEmotes, 2);
});

test('Check conversion between types', () {
final matrixEvent = MatrixEvent.fromJson(
{
'content': {
'body': 'filename.jpg',
'info': {
'h': 398,
'mimetype': 'image/jpeg',
'size': 31037,
'w': 394,
},
'msgtype': 'm.image',
'url': 'mxc://example.org/JWEIFJgwEIhweiWJE',
},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': room.id,
'sender': '@example:example.org',
'type': 'm.room.message',
'unsigned': {'age': 1234},
'redacts': 'abcd',
'prev_content': <String, Object?>{
'foo': 'bar',
},
},
);
final event = Event.fromMatrixEvent(matrixEvent, room);
expect(
event.toJson()..remove('status'),
matrixEvent.toJson(),
);
});
});
}

0 comments on commit e2b3f3d

Please sign in to comment.