From af0356e5a15afbbd00ff2f5e78a8d142d59eb154 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 22 Jul 2021 19:47:54 +0530 Subject: [PATCH] test(localization): fix `sentAtText`, add remaining tests Signed-off-by: xsahil03x --- .../lib/src/localization/translations.dart | 13 +- .../test/src/default_translations_test.dart | 174 ++++++++++++++++++ .../lib/src/stream_chat_localizations_en.dart | 13 +- .../lib/src/stream_chat_localizations_fr.dart | 13 +- .../lib/src/stream_chat_localizations_hi.dart | 13 +- .../lib/src/stream_chat_localizations_it.dart | 17 +- .../test/translations_test.dart | 19 ++ 7 files changed, 235 insertions(+), 27 deletions(-) create mode 100644 packages/stream_chat_flutter/test/src/default_translations_test.dart diff --git a/packages/stream_chat_flutter/lib/src/localization/translations.dart b/packages/stream_chat_flutter/lib/src/localization/translations.dart index 17355b6f..d6e5add0 100644 --- a/packages/stream_chat_flutter/lib/src/localization/translations.dart +++ b/packages/stream_chat_flutter/lib/src/localization/translations.dart @@ -535,15 +535,18 @@ class DefaultTranslations implements Translations { String get photosLabel => 'Photos'; String _getDay(DateTime dateTime) { - final now = Jiffy(DateTime.now()); - final date = Jiffy(dateTime); + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = DateTime(now.year, now.month, now.day - 1); - if (date.isSame(now, Units.DAY)) { + final date = DateTime(dateTime.year, dateTime.month, dateTime.day); + + if (date == today) { return 'today'; - } else if (now.diff(date, Units.HOUR) < 24) { + } else if (date == yesterday) { return 'yesterday'; } else { - return 'on ${date.MMMd}'; + return 'on ${Jiffy(date).MMMd}'; } } diff --git a/packages/stream_chat_flutter/test/src/default_translations_test.dart b/packages/stream_chat_flutter/test/src/default_translations_test.dart new file mode 100644 index 00000000..60e69fab --- /dev/null +++ b/packages/stream_chat_flutter/test/src/default_translations_test.dart @@ -0,0 +1,174 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:stream_chat_flutter/stream_chat_flutter.dart'; + +void main() { + test('Default translations should exist', () { + final translations = DefaultTranslations.instance; + expect(translations.launchUrlError, isNotNull); + expect(translations.loadingUsersError, isNotNull); + expect(translations.noUsersLabel, isNotNull); + expect(translations.retryLabel, isNotNull); + expect(translations.userLastOnlineText, isNotNull); + expect(translations.userOnlineText, isNotNull); + expect(translations.userOnlineText, isNotNull); + // no users + expect(translations.userTypingText([]), isNotNull); + // single user + expect(translations.userTypingText([User(id: 'test-id')]), isNotNull); + // multiple users + expect( + translations.userTypingText([ + User(id: 'test-id-1'), + User(id: 'test-id-2'), + ]), + isNotNull, + ); + expect(translations.threadReplyLabel, isNotNull); + expect(translations.onlyVisibleToYouText, isNotNull); + expect(translations.threadReplyCountText(3), isNotNull); + expect( + translations.attachmentsUploadProgressText(remaining: 3, total: 10), + isNotNull, + ); + expect( + translations.pinnedByUserText( + pinnedBy: User(id: 'pinned-by-user-id'), + currentUser: OwnUser(id: 'current-user-id'), + ), + isNotNull, + ); + expect(translations.emptyMessagesText, isNotNull); + expect(translations.genericErrorText, isNotNull); + expect(translations.loadingMessagesError, isNotNull); + expect(translations.resultCountText(3), isNotNull); + expect(translations.messageDeletedText, isNotNull); + expect(translations.messageDeletedLabel, isNotNull); + expect(translations.messageReactionsLabel, isNotNull); + expect(translations.emptyChatMessagesText, isNotNull); + expect(translations.threadSeparatorText(3), isNotNull); + expect(translations.connectedLabel, isNotNull); + expect(translations.disconnectedLabel, isNotNull); + expect(translations.reconnectingLabel, isNotNull); + expect(translations.alsoSendAsDirectMessageLabel, isNotNull); + expect(translations.addACommentOrSendLabel, isNotNull); + expect(translations.searchGifLabel, isNotNull); + expect(translations.writeAMessageLabel, isNotNull); + expect(translations.instantCommandsLabel, isNotNull); + expect(translations.fileTooLargeAfterCompressionError, isNotNull); + expect(translations.fileTooLargeError, isNotNull); + expect(translations.emojiMatchingQueryText('sahil'), isNotNull); + expect(translations.addAFileLabel, isNotNull); + expect(translations.photoFromCameraLabel, isNotNull); + expect(translations.uploadAFileLabel, isNotNull); + expect(translations.uploadAPhotoLabel, isNotNull); + expect(translations.uploadAVideoLabel, isNotNull); + expect(translations.videoFromCameraLabel, isNotNull); + expect(translations.okLabel, isNotNull); + expect(translations.somethingWentWrongError, isNotNull); + expect(translations.addMoreFilesLabel, isNotNull); + expect(translations.enablePhotoAndVideoAccessMessage, isNotNull); + expect(translations.allowGalleryAccessMessage, isNotNull); + expect(translations.flagMessageLabel, isNotNull); + expect(translations.flagMessageQuestion, isNotNull); + expect(translations.flagLabel, isNotNull); + expect(translations.cancelLabel, isNotNull); + expect(translations.flagMessageSuccessfulLabel, isNotNull); + expect(translations.flagMessageSuccessfulText, isNotNull); + expect(translations.deleteLabel, isNotNull); + expect(translations.deleteMessageLabel, isNotNull); + expect(translations.deleteMessageQuestion, isNotNull); + expect(translations.operationCouldNotBeCompletedText, isNotNull); + expect(translations.replyLabel, isNotNull); + // pinned + expect(translations.togglePinUnpinText(pinned: true), isNotNull); + // un-pinned + expect(translations.togglePinUnpinText(pinned: false), isNotNull); + // delete-failed + expect( + translations.toggleDeleteRetryDeleteMessageText(isDeleteFailed: true), + isNotNull, + ); + // first-delete + expect( + translations.toggleDeleteRetryDeleteMessageText(isDeleteFailed: false), + isNotNull, + ); + expect(translations.copyMessageLabel, isNotNull); + expect(translations.editMessageLabel, isNotNull); + // resend-failed + expect( + translations.toggleResendOrResendEditedMessage(isUpdateFailed: true), + isNotNull, + ); + // first resend + expect( + translations.toggleResendOrResendEditedMessage(isUpdateFailed: false), + isNotNull, + ); + expect(translations.photosLabel, isNotNull); + // today + expect( + translations.sentAtText( + date: DateTime.now(), + time: DateTime.now(), + ), + isNotNull, + ); + // yesterday + expect( + translations.sentAtText( + date: DateTime.now().subtract(const Duration(days: 1)), + time: DateTime.now(), + ), + isNotNull, + ); + // any other day + expect( + translations.sentAtText( + date: DateTime.now().subtract(const Duration(days: 3)), + time: DateTime.now(), + ), + isNotNull, + ); + expect(translations.todayLabel, isNotNull); + expect(translations.yesterdayLabel, isNotNull); + expect(translations.channelIsMutedText, isNotNull); + expect(translations.noTitleText, isNotNull); + expect(translations.letsStartChattingLabel, isNotNull); + expect(translations.sendingFirstMessageLabel, isNotNull); + expect(translations.startAChatLabel, isNotNull); + expect(translations.loadingChannelsError, isNotNull); + expect(translations.deleteConversationLabel, isNotNull); + expect(translations.deleteConversationQuestion, isNotNull); + expect(translations.streamChatLabel, isNotNull); + expect(translations.searchingForNetworkText, isNotNull); + expect(translations.offlineLabel, isNotNull); + expect(translations.tryAgainLabel, isNotNull); + // 1 member + expect(translations.membersCountText(1), isNotNull); + // 3 members + expect(translations.membersCountText(3), isNotNull); + // 1 member + expect(translations.watchersCountText(1), isNotNull); + // 3 members + expect(translations.watchersCountText(3), isNotNull); + expect(translations.viewInfoLabel, isNotNull); + expect(translations.leaveGroupLabel, isNotNull); + expect(translations.leaveLabel, isNotNull); + expect(translations.leaveConversationLabel, isNotNull); + expect(translations.leaveConversationQuestion, isNotNull); + expect(translations.showInChatLabel, isNotNull); + expect(translations.saveImageLabel, isNotNull); + expect(translations.saveVideoLabel, isNotNull); + expect(translations.uploadErrorLabel, isNotNull); + expect(translations.giphyLabel, isNotNull); + expect(translations.shuffleLabel, isNotNull); + expect(translations.sendLabel, isNotNull); + expect(translations.withText, isNotNull); + expect(translations.inText, isNotNull); + expect(translations.youText, isNotNull); + expect(translations.ofText, isNotNull); + expect(translations.fileText, isNotNull); + expect(translations.replyToMessageLabel, isNotNull); + }); +} diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart index ad58b89a..5041d398 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart @@ -228,15 +228,18 @@ class StreamChatLocalizationsEn extends GlobalStreamChatLocalizations { String get photosLabel => 'Photos'; String _getDay(DateTime dateTime) { - final now = Jiffy(DateTime.now()); - final date = Jiffy(dateTime); + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = DateTime(now.year, now.month, now.day - 1); - if (date.isSame(now, Units.DAY)) { + final date = DateTime(dateTime.year, dateTime.month, dateTime.day); + + if (date == today) { return 'today'; - } else if (now.diff(date, Units.HOUR) < 24) { + } else if (date == yesterday) { return 'yesterday'; } else { - return 'on ${date.MMMd}'; + return 'on ${Jiffy(date).MMMd}'; } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart index ba8511a0..1bbfbc5a 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart @@ -232,15 +232,18 @@ class StreamChatLocalizationsFr extends GlobalStreamChatLocalizations { String get photosLabel => 'Photos'; String _getDay(DateTime dateTime) { - final now = Jiffy(DateTime.now()); - final date = Jiffy(dateTime); + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = DateTime(now.year, now.month, now.day - 1); - if (date.isSame(now, Units.DAY)) { + final date = DateTime(dateTime.year, dateTime.month, dateTime.day); + + if (date == today) { return "aujourd'hui"; - } else if (now.diff(date, Units.HOUR) < 24) { + } else if (date == yesterday) { return 'hier'; } else { - return 'le ${date.MMMd}'; + return 'le ${Jiffy(date).MMMd}'; } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart index 93a5ca30..850e481b 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart @@ -227,15 +227,18 @@ class StreamChatLocalizationsHi extends GlobalStreamChatLocalizations { String get photosLabel => 'तस्वीरें'; String _getDay(DateTime dateTime) { - final now = Jiffy(DateTime.now()); - final date = Jiffy(dateTime); + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = DateTime(now.year, now.month, now.day - 1); - if (date.isSame(now, Units.DAY)) { + final date = DateTime(dateTime.year, dateTime.month, dateTime.day); + + if (date == today) { return 'आज'; - } else if (now.diff(date, Units.HOUR) < 24) { + } else if (date == yesterday) { return 'कल'; } else { - return '${date.MMMd} को'; + return '${Jiffy(date).MMMd} को'; } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart index ce5faf55..8b2e1692 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart @@ -229,21 +229,24 @@ Il file è troppo grande per essere caricato. Il limite è di $limitInMB MB.'''; String get photosLabel => 'Foto'; String _getDay(DateTime dateTime) { - final now = Jiffy(DateTime.now()); - final date = Jiffy(dateTime); + final now = DateTime.now(); + final today = DateTime(now.year, now.month, now.day); + final yesterday = DateTime(now.year, now.month, now.day - 1); - if (date.isSame(now, Units.DAY)) { + final date = DateTime(dateTime.year, dateTime.month, dateTime.day); + + if (date == today) { return 'oggi'; - } else if (now.diff(date, Units.HOUR) < 24) { + } else if (date == yesterday) { return 'ieri'; } else { - return 'il ${date.MMMd}'; + return 'il ${Jiffy(date).MMMd}'; } } @override - String sentAtText({required DateTime date, required DateTime time}) => ''' -Inviato il ${_getDay(date)} alle ${Jiffy(time.toLocal()).format('HH:mm')}'''; + String sentAtText({required DateTime date, required DateTime time}) => + "Inviato ${_getDay(date)} alle ${Jiffy(time.toLocal()).format('HH:mm')}"; @override String get todayLabel => 'Oggi'; diff --git a/packages/stream_chat_localizations/test/translations_test.dart b/packages/stream_chat_localizations/test/translations_test.dart index 81acf58f..199557d8 100644 --- a/packages/stream_chat_localizations/test/translations_test.dart +++ b/packages/stream_chat_localizations/test/translations_test.dart @@ -114,6 +114,7 @@ void main() { isNotNull, ); expect(localizations.photosLabel, isNotNull); + // today expect( localizations.sentAtText( date: DateTime.now(), @@ -121,6 +122,22 @@ void main() { ), isNotNull, ); + // yesterday + expect( + localizations.sentAtText( + date: DateTime.now().subtract(const Duration(days: 1)), + time: DateTime.now(), + ), + isNotNull, + ); + // any other day + expect( + localizations.sentAtText( + date: DateTime.now().subtract(const Duration(days: 3)), + time: DateTime.now(), + ), + isNotNull, + ); expect(localizations.todayLabel, isNotNull); expect(localizations.yesterdayLabel, isNotNull); expect(localizations.channelIsMutedText, isNotNull); @@ -129,6 +146,7 @@ void main() { expect(localizations.sendingFirstMessageLabel, isNotNull); expect(localizations.startAChatLabel, isNotNull); expect(localizations.loadingChannelsError, isNotNull); + expect(localizations.deleteConversationLabel, isNotNull); expect(localizations.deleteConversationQuestion, isNotNull); expect(localizations.streamChatLabel, isNotNull); expect(localizations.searchingForNetworkText, isNotNull); @@ -148,6 +166,7 @@ void main() { expect(localizations.leaveConversationLabel, isNotNull); expect(localizations.leaveConversationQuestion, isNotNull); expect(localizations.showInChatLabel, isNotNull); + expect(localizations.saveImageLabel, isNotNull); expect(localizations.saveVideoLabel, isNotNull); expect(localizations.uploadErrorLabel, isNotNull); expect(localizations.giphyLabel, isNotNull);