diff --git a/packages/stream_chat/test/fixtures/user.json b/packages/stream_chat/test/fixtures/user.json index b22c8553..146a4242 100644 --- a/packages/stream_chat/test/fixtures/user.json +++ b/packages/stream_chat/test/fixtures/user.json @@ -2,5 +2,16 @@ "id": "bbb19d9a-ee50-45bc-84e5-0584e79d0c9e", "role": "test-role", "name": "John", + "image": "https://getstream.io/random_png/?id=cool-shadow-7&name=Cool+shadow", + "extraDataStringTest": "Extra data test", + "extraDataIntTest": 1, + "extraDataDoubleTest": 1.1, + "extraDataBoolTest": true, + "banned": true, + "online": true, + "teams": ["team-1", "team-2"], + "created_at": "2021-08-03 12:39:21.817646", + "updated_at": "2021-08-04 12:39:21.817646", + "last_active" : "2021-08-05 12:39:21.817646", "language": "en" } \ No newline at end of file diff --git a/packages/stream_chat/test/src/core/models/own_user_test.dart b/packages/stream_chat/test/src/core/models/own_user_test.dart index 7aef859d..e1d12ada 100644 --- a/packages/stream_chat/test/src/core/models/own_user_test.dart +++ b/packages/stream_chat/test/src/core/models/own_user_test.dart @@ -1,10 +1,23 @@ +import 'package:mocktail/mocktail.dart'; import 'package:stream_chat/src/core/models/own_user.dart'; import 'package:stream_chat/src/core/models/user.dart'; +import 'package:stream_chat/stream_chat.dart'; import 'package:test/test.dart'; import '../../utils.dart'; +class MockMute extends Mock implements Mute {} + +class MockDevice extends Mock implements Device {} + void main() { + final devices = [MockDevice(), MockDevice()]; + final mutes = [MockMute(), MockMute()]; + final channelMutes = [MockMute()]; + final createdAt = DateTime.parse('2021-05-03 12:39:21.817646'); + final updatedAt = DateTime.parse('2021-04-03 12:39:21.817646'); + final lastActive = DateTime.parse('2021-03-03 12:39:21.817646'); + group('src/models/own_user', () { test('should parse json correctly', () { final ownUser = OwnUser.fromJson(jsonFixture('own_user.json')); @@ -22,6 +35,7 @@ void main() { expect(ownUser.channelMutes.length, 1); expect(ownUser.totalUnreadCount, 0); expect(ownUser.unreadChannels, 0); + expect(ownUser.image, 'https://placehold.jp/150x150.png'); expect(ownUser.extraData['image'], 'https://placehold.jp/150x150.png'); expect(ownUser.extraData['name'], 'Proud darkness'); expect(ownUser.extraData['username'], 'Rioland'); @@ -40,6 +54,7 @@ void main() { expect(ownUser.online, user.online); expect(ownUser.banned, user.banned); expect(ownUser.extraData, user.extraData); + expect(ownUser.image, user.image); }); test('copyWith', () { @@ -49,35 +64,116 @@ void main() { expect(newUser.id, user.id); expect(newUser.role, user.role); expect(newUser.name, user.name); + expect(newUser.devices, user.devices); + expect(newUser.mutes, user.mutes); + expect(newUser.totalUnreadCount, user.totalUnreadCount); + expect(newUser.channelMutes, user.channelMutes); + expect(newUser.createdAt, user.createdAt); + expect(newUser.updatedAt, user.updatedAt); + expect(newUser.lastActive, user.lastActive); + expect(newUser.online, user.online); + expect(newUser.extraData, user.extraData); + expect(newUser.banned, user.banned); + expect(newUser.teams, user.teams); + expect(newUser.language, user.language); + expect(newUser.image, user.image); newUser = user.copyWith( id: 'test', role: 'test', + image: 'https://getstream.io/image-new', extraData: { 'name': 'test', }, + devices: devices, + mutes: mutes, + totalUnreadCount: 10, + unreadChannels: 5, + channelMutes: channelMutes, + createdAt: createdAt, + updatedAt: updatedAt, + lastActive: lastActive, + online: true, + banned: true, + teams: ['team1', 'team2'], + language: 'fr', ); expect(newUser.id, 'test'); expect(newUser.role, 'test'); expect(newUser.name, 'test'); + expect(newUser.image, 'https://getstream.io/image-new'); + expect( + newUser.extraData, + { + 'name': 'test', + 'image': 'https://getstream.io/image-new', + }, + reason: 'Should get image from user.image', + ); + expect(newUser.devices, devices); + expect(newUser.mutes, mutes); + expect(newUser.totalUnreadCount, 10); + expect(newUser.unreadChannels, 5); + expect(newUser.channelMutes, channelMutes); + expect(newUser.createdAt, createdAt); + expect(newUser.updatedAt, updatedAt); + expect(newUser.createdAt, createdAt); + expect(newUser.online, true); + expect(newUser.banned, true); + expect(newUser.teams, ['team1', 'team2']); + expect(newUser.language, 'fr'); }); test('merge', () { final user = OwnUser.fromJson(jsonFixture('own_user.json')); - final newUser = user.merge(OwnUser( - id: 'test', - role: 'test', - extraData: const { - 'name': 'test', - }, - banned: true, - )); + final newUser = user.merge( + OwnUser( + id: 'test', + role: 'test', + extraData: const { + 'name': 'test', + }, + image: 'https://getstream.io/image-new', + devices: devices, + mutes: mutes, + totalUnreadCount: 10, + unreadChannels: 5, + channelMutes: channelMutes, + createdAt: createdAt, + updatedAt: updatedAt, + lastActive: lastActive, + online: true, + banned: true, + teams: const ['team1', 'team2'], + language: 'fr', + ), + ); expect(newUser.id, 'test'); expect(newUser.role, 'test'); expect(newUser.name, 'test'); + expect(newUser.image, 'https://getstream.io/image-new'); + expect( + newUser.extraData, + { + 'name': 'test', + 'image': 'https://getstream.io/image-new', + }, + reason: 'Should get image from user.image', + ); + expect(newUser.devices, devices); + expect(newUser.mutes, mutes); + expect(newUser.totalUnreadCount, 10); + expect(newUser.unreadChannels, 5); + expect(newUser.channelMutes, channelMutes); + expect(newUser.createdAt, createdAt); + expect(newUser.updatedAt, updatedAt); + expect(newUser.createdAt, createdAt); + expect(newUser.online, true); expect(newUser.banned, true); + expect(newUser.teams, ['team1', 'team2']); + expect(newUser.language, 'fr'); }); }); } diff --git a/packages/stream_chat/test/src/core/models/user_test.dart b/packages/stream_chat/test/src/core/models/user_test.dart index ce488cab..48306017 100644 --- a/packages/stream_chat/test/src/core/models/user_test.dart +++ b/packages/stream_chat/test/src/core/models/user_test.dart @@ -4,21 +4,73 @@ import 'package:test/test.dart'; import '../../utils.dart'; void main() { + const id = 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'; + const role = 'test-role'; + const name = 'John'; + const image = + 'https://getstream.io/random_png/?id=cool-shadow-7&name=Cool+shadow'; + const extraDataStringTest = 'Extra data test'; + const extraDataIntTest = 1; + const extraDataDoubleTest = 1.1; + const extraDataBoolTest = true; + const online = true; + const banned = true; + const teams = ['team-1', 'team-2']; + const createdAtString = '2021-08-03 12:39:21.817646'; + const updatedAtString = '2021-08-04 12:39:21.817646'; + const lastActiveString = '2021-08-05 12:39:21.817646'; + group('src/models/user', () { test('should parse json correctly', () { final user = User.fromJson(jsonFixture('user.json')); - expect(user.id, 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e'); - expect(user.name, 'John'); + expect(user.id, id); + expect(user.role, role); + expect(user.name, name); + expect(user.image, image); + expect(user.extraData['image'], image); + expect(user.extraData['extraDataStringTest'], extraDataStringTest); + expect(user.extraData['extraDataIntTest'], extraDataIntTest); + expect(user.extraData['extraDataDoubleTest'], extraDataDoubleTest); + expect(user.extraData['extraDataBoolTest'], extraDataBoolTest); + expect(user.online, online); + expect(user.banned, banned); + expect(user.teams, teams); + expect(user.createdAt, DateTime.parse(createdAtString)); + expect(user.updatedAt, DateTime.parse(updatedAtString)); + expect(user.lastActive, DateTime.parse(lastActiveString)); + expect(user.language, 'en'); }); test('should serialize to json correctly', () { final user = User( - id: 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e', - role: 'abc', + id: id, + role: role, + image: image, + extraData: const { + 'name': name, + 'extraDataStringTest': extraDataStringTest, + 'extraDataIntTest': extraDataIntTest, + 'extraDataDoubleTest': extraDataDoubleTest, + 'extraDataBoolTest': extraDataBoolTest, + }, + createdAt: DateTime.parse(createdAtString), + updatedAt: DateTime.parse(updatedAtString), + lastActive: DateTime.parse(lastActiveString), + banned: online, + online: banned, + teams: const ['team-1', 'team-2'], + language: 'fr', ); expect(user.toJson(), { - 'id': 'bbb19d9a-ee50-45bc-84e5-0584e79d0c9e', + 'id': id, + 'name': name, + 'image': image, + 'extraDataStringTest': extraDataStringTest, + 'extraDataIntTest': extraDataIntTest, + 'extraDataDoubleTest': extraDataDoubleTest, + 'extraDataBoolTest': extraDataBoolTest, + 'language': 'fr', }); }); @@ -29,6 +81,14 @@ void main() { expect(newUser.id, user.id); expect(newUser.role, user.role); expect(newUser.name, user.name); + expect(newUser.image, user.image); + expect(newUser.online, user.online); + expect(newUser.banned, user.banned); + expect(newUser.teams, user.teams); + expect(newUser.createdAt, user.createdAt); + expect(newUser.updatedAt, user.updatedAt); + expect(newUser.lastActive, user.lastActive); + expect(newUser.language, user.language); newUser = user.copyWith( id: 'test', @@ -36,11 +96,94 @@ void main() { extraData: { 'name': 'test', }, + image: 'https://stream.io/new-image', + online: false, + banned: false, + teams: ['new-team1', 'new-team2'], + createdAt: DateTime.parse('2021-05-03 12:39:21.817646'), + updatedAt: DateTime.parse('2021-05-04 12:39:21.817646'), + lastActive: DateTime.parse('2021-05-06 12:39:21.817646'), + language: 'it', ); expect(newUser.id, 'test'); expect(newUser.role, 'test'); expect(newUser.name, 'test'); + expect(newUser.image, 'https://stream.io/new-image'); + expect(newUser.extraData['image'], 'https://stream.io/new-image'); + expect(newUser.online, false); + expect(newUser.banned, false); + expect(newUser.teams, ['new-team1', 'new-team2']); + expect(newUser.createdAt, DateTime.parse('2021-05-03 12:39:21.817646')); + expect(newUser.updatedAt, DateTime.parse('2021-05-04 12:39:21.817646')); + expect(newUser.lastActive, DateTime.parse('2021-05-06 12:39:21.817646')); + expect(newUser.language, 'it'); + }); + + test('image property and extraData manipulation', () { + final user = User(id: id, image: image); + + expect(user.image, image); + expect(user.extraData['image'], image); + expect(user.toJson(), {'id': id, 'image': image}); + expect(User.fromJson(user.toJson()).toJson(), {'id': id, 'image': image}); + + const imageURLOne = 'https://stream.io/image-one'; + var newUser = user.copyWith( + extraData: {'image': imageURLOne}, + ); + + expect(newUser.extraData['image'], imageURLOne); + expect(newUser.image, imageURLOne); + + const imageURLTwo = 'https://stream.io/image-two'; + newUser = user.copyWith( + image: imageURLTwo, + ); + + expect(newUser.extraData['image'], imageURLTwo); + expect(newUser.image, imageURLTwo); + + const imageURLThree = 'https://stream.io/image-three'; + newUser = user.copyWith( + image: imageURLThree, + extraData: {'image': imageURLThree}, + ); + + expect(newUser.extraData['image'], imageURLThree); + expect(newUser.image, imageURLThree); + }); + + test('default values, constructor', () { + final user = User(id: id); + + expect(user.id, id); + expect(user.role, null); + expect(user.name, id, reason: 'if a name is not supplied, default to id'); + expect(user.image, null); + expect(user.extraData, const {}); + expect(user.online, false); + expect(user.banned, false); + expect(user.teams, []); + expect(user.lastActive, null); + expect(user.createdAt, isNotNull); + expect(user.updatedAt, isNotNull); + }); + + test('default values, parse json', () { + final user = User.fromJson(const {'id': id}); + + expect(user.id, id); + expect(user.role, null); + expect(user.name, id, reason: 'if a name is not supplied, default to id'); + expect(user.image, null); + expect(user.extraData, const {}); + expect(user.online, false); + expect(user.banned, false); + expect(user.teams, []); + expect(user.lastActive, null); + expect(user.createdAt, isNotNull); + expect(user.updatedAt, isNotNull); }); }); }