Skip to content

Commit

Permalink
TF-2859 Unit test add language when reply calendar invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
tddang-linagora authored and hoangdat committed May 21, 2024
1 parent 2e68e7d commit b207880
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 31 deletions.
168 changes: 168 additions & 0 deletions model/test/extensions/session_extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/account/account.dart';
import 'package:jmap_dart_client/jmap/core/capability/calendar_event_capability.dart';
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
import 'package:jmap_dart_client/jmap/core/capability/empty_capability.dart';
import 'package:jmap_dart_client/jmap/core/id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:model/extensions/session_extension.dart';

void main() {
final accountId = AccountId(Id('123abc'));

group('validate calendar event capability test:', () {
final account = Account(AccountName('name'), true, true, {});

test(
'should return isAvailable is false '
'and calendarEventCapability is null '
'when there is no such capability',
() {
// arrange
final session = Session(
{},
{accountId: account},
{}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final result = session.validateCalendarEventCapability(accountId);

// assert
expect(result.isAvailable, false);
expect(result.calendarEventCapability, null);
});

test(
'should return isAvailable is true '
'and calendarEventCapability is not null '
'when calendar event capability is available',
() {
// arrange
final calendarEventCapability = CalendarEventCapability();
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: calendarEventCapability},
{accountId: account},
{}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final result = session.validateCalendarEventCapability(AccountId(Id('123abc')));

// assert
expect(result.isAvailable, true);
expect(result.calendarEventCapability, calendarEventCapability);
});
});

group('get language for calendar event test:', () {
test(
'should return null '
'when calendar event capability is not supported',
() {
// arrange
const currentLocale = Locale('en', 'US');
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: EmptyCapability()},
{}, {}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final calendarEventLanguage = session.getLanguageForCalendarEvent(
currentLocale,
accountId);

// assert
expect(calendarEventLanguage, null);
});

test(
'should return null '
'when calendar event capability supportes no language',
() {
// arrange
final calendarEventCapability = CalendarEventCapability();
const currentLocale = Locale('en', 'US');
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: calendarEventCapability},
{}, {}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final calendarEventLanguage = session.getLanguageForCalendarEvent(
currentLocale,
accountId);

// assert
expect(calendarEventLanguage, null);
});

test(
'should return current locale language '
'when calendar event capability supports current locale',
() {
// arrange
final calendarEventCapability = CalendarEventCapability(
replySupportedLanguage: ['en', 'fr'],
);
const currentLocale = Locale('en', 'US');
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: calendarEventCapability},
{}, {}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final calendarEventLanguage = session.getLanguageForCalendarEvent(
currentLocale,
accountId);

// assert
expect(calendarEventLanguage, currentLocale.languageCode);
});

test(
'should return English language '
'when calendar event capability doesn\'t support current locale, '
'but supports English',
() {
// arrange
final calendarEventCapability = CalendarEventCapability(
replySupportedLanguage: ['en'],
);
const currentLocale = Locale('fr', 'FR');
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: calendarEventCapability},
{}, {}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final calendarEventLanguage = session.getLanguageForCalendarEvent(
currentLocale,
accountId);

// assert
expect(calendarEventLanguage, 'en');
});

test(
'should return first supported language '
'when calendar event capability doesn\'t support both English and current locale',
() {
// arrange
final calendarEventCapability = CalendarEventCapability(
replySupportedLanguage: ['vi'],
);
const currentLocale = Locale('fr', 'FR');
final session = Session(
{CapabilityIdentifier.jamesCalendarEvent: calendarEventCapability},
{}, {}, UserName(''), Uri(), Uri(), Uri(), Uri(), State(''));

// act
final calendarEventLanguage = session.getLanguageForCalendarEvent(
currentLocale,
accountId);

// assert
expect(calendarEventLanguage, 'vi');
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void main() {
final calendarEventRepository = CalendarEventRepositoryImpl(calendarEventDataSource);
final accountId = AccountId(Id('123'));
final blobId = Id('blobId');
const language = 'en';

group('calendar event repository test:', () {
final calendarEventAcceptResponseresponse = CalendarEventAcceptResponse(
Expand All @@ -31,24 +32,24 @@ void main() {

test('should return response when data source return response', () async {
// arrange
when(calendarEventNetworkDataSource.acceptEventInvitation(any, any))
when(calendarEventNetworkDataSource.acceptEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventAcceptResponseresponse);

// act
final response = await calendarEventRepository.acceptEventInvitation(accountId, {blobId});
final response = await calendarEventRepository.acceptEventInvitation(accountId, {blobId}, language);

// assert
expect(response, calendarEventAcceptResponseresponse);
});

test('should throw exception when data source throw exception', () {
// arrange
when(calendarEventNetworkDataSource.acceptEventInvitation(any, any))
when(calendarEventNetworkDataSource.acceptEventInvitation(any, any, any))
.thenThrow(NotAcceptableCalendarEventException());

// assert
expect(
() => calendarEventRepository.acceptEventInvitation(accountId, {blobId}),
() => calendarEventRepository.acceptEventInvitation(accountId, {blobId}, language),
throwsA(isA<NotAcceptableCalendarEventException>()));
});
});
Expand All @@ -61,24 +62,24 @@ void main() {

test('should return response when data source return response', () async {
// arrange
when(calendarEventNetworkDataSource.maybeEventInvitation(any, any))
when(calendarEventNetworkDataSource.maybeEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventMaybeResponse);

// act
final response = await calendarEventRepository.maybeEventInvitation(accountId, {blobId});
final response = await calendarEventRepository.maybeEventInvitation(accountId, {blobId}, language);

// assert
expect(response, calendarEventMaybeResponse);
});

test('should throw exception when data source throw exception', () {
// arrange
when(calendarEventNetworkDataSource.maybeEventInvitation(any, any))
when(calendarEventNetworkDataSource.maybeEventInvitation(any, any, any))
.thenThrow(NotMaybeableCalendarEventException());

// assert
expect(
() => calendarEventRepository.maybeEventInvitation(accountId, {blobId}),
() => calendarEventRepository.maybeEventInvitation(accountId, {blobId}, language),
throwsA(isA<NotMaybeableCalendarEventException>()));
});
});
Expand All @@ -91,24 +92,24 @@ void main() {

test('should return response when data source return response', () async {
// arrange
when(calendarEventNetworkDataSource.rejectEventInvitation(any, any))
when(calendarEventNetworkDataSource.rejectEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventRejectResponseresponse);

// act
final response = await calendarEventRepository.rejectEventInvitation(accountId, {blobId});
final response = await calendarEventRepository.rejectEventInvitation(accountId, {blobId}, language);

// assert
expect(response, calendarEventRejectResponseresponse);
});

test('should throw exception when data source throw exception', () {
// arrange
when(calendarEventNetworkDataSource.rejectEventInvitation(any, any))
when(calendarEventNetworkDataSource.rejectEventInvitation(any, any, any))
.thenThrow(NotRejectableCalendarEventException());

// assert
expect(
() => calendarEventRepository.rejectEventInvitation(accountId, {blobId}),
() => calendarEventRepository.rejectEventInvitation(accountId, {blobId}, language),
throwsA(isA<NotRejectableCalendarEventException>()));
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void main() {
final accountId = AccountId(Id('123'));
final blobId = Id('123321');
final emailId = EmailId(Id('abcde'));
const language = 'en';

group('calendar event accept interactor test:', () {
test('should emit CalendarEventAccepted when repo return data', () {
Expand All @@ -29,12 +30,12 @@ void main() {
accountId,
null,
accepted: [EventId(blobId.value)]);
when(calendarEventRepository.acceptEventInvitation(any, any))
when(calendarEventRepository.acceptEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventAcceptResponse);

// assert
expect(
acceptCalendarEventInteractor.execute(accountId, {blobId}, emailId),
acceptCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventAccepting()),
Right(CalendarEventAccepted(calendarEventAcceptResponse, emailId))]));
Expand All @@ -43,11 +44,11 @@ void main() {
test('should emit CalendarEventAcceptFailure when repo throw exception', () {
// arrange
final exception = NotAcceptableCalendarEventException();
when(calendarEventRepository.acceptEventInvitation(any, any)).thenThrow(exception);
when(calendarEventRepository.acceptEventInvitation(any, any, any)).thenThrow(exception);

// assert
expect(
acceptCalendarEventInteractor.execute(accountId, {blobId}, emailId),
acceptCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventAccepting()),
Left(CalendarEventAcceptFailure(exception: exception))]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main() {
final accountId = AccountId(Id('123'));
final blobId = Id('123321');
final emailId = EmailId(Id('abcde'));
const language = 'en';

group('calendar event reject interactor test:', () {
test('should emit CalendarEventRejected when repo return data', () {
Expand All @@ -26,12 +27,12 @@ void main() {
accountId,
null,
rejected: [EventId(blobId.value)]);
when(calendarEventRepository.rejectEventInvitation(any, any))
when(calendarEventRepository.rejectEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventRejectResponse);

// assert
expect(
rejectCalendarEventInteractor.execute(accountId, {blobId}, emailId),
rejectCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventRejecting()),
Right(CalendarEventRejected(calendarEventRejectResponse, emailId))]));
Expand All @@ -40,11 +41,11 @@ void main() {
test('should emit CalendarEventRejectFailure when repo throw exception', () {
// arrange
final exception = NotRejectableCalendarEventException();
when(calendarEventRepository.rejectEventInvitation(any, any)).thenThrow(exception);
when(calendarEventRepository.rejectEventInvitation(any, any, any)).thenThrow(exception);

// assert
expect(
rejectCalendarEventInteractor.execute(accountId, {blobId}, emailId),
rejectCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventRejecting()),
Left(CalendarEventRejectFailure(exception: exception))]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main() {
final accountId = AccountId(Id('123'));
final blobId = Id('123321');
final emailId = EmailId(Id('abcde'));
const language = 'en';

group('calendar event maybe interactor should emit expected states', () {
test('when repo return data', () {
Expand All @@ -26,12 +27,12 @@ void main() {
accountId,
null,
maybe: [EventId(blobId.value)]);
when(calendarEventRepository.maybeEventInvitation(any, any))
when(calendarEventRepository.maybeEventInvitation(any, any, any))
.thenAnswer((_) async => calendarEventMaybeResponse);

// assert
expect(
maybeCalendarEventInteractor.execute(accountId, {blobId}, emailId),
maybeCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventMaybeReplying()),
Right(CalendarEventMaybeSuccess(calendarEventMaybeResponse, emailId))]));
Expand All @@ -40,11 +41,11 @@ void main() {
test('when repo throw exception', () {
// arrange
final exception = NotMaybeableCalendarEventException();
when(calendarEventRepository.maybeEventInvitation(any, any)).thenThrow(exception);
when(calendarEventRepository.maybeEventInvitation(any, any, any)).thenThrow(exception);

// assert
expect(
maybeCalendarEventInteractor.execute(accountId, {blobId}, emailId),
maybeCalendarEventInteractor.execute(accountId, {blobId}, emailId, language),
emitsInOrder([
Right(CalendarEventMaybeReplying()),
Left(CalendarEventMaybeFailure(exception: exception))]));
Expand Down
Loading

0 comments on commit b207880

Please sign in to comment.