import 'dart:io';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:zylag/core/service/AppReview/app_review.dart';
import '../../../mock.dart';
void main() {
group('App Review', () {
late MockLocalStorage mockLocalStorageService;
late MockInAppReview mockInAppReview;
setUp(() {
mockLocalStorageService = MockLocalStorage();
mockInAppReview = MockInAppReview();
});
setUpAll(() {
dotenv.testLoad(fileInput: File('.env').readAsStringSync());
});
test(
'AppReview - startReviewEngine - initializes data and registers if needed',
() async {
// Set expectations for LocalStorageService
when(() => mockLocalStorageService.readJsonData(any()))
.thenAnswer((_) async => null);
when(() => mockLocalStorageService.createJsonData(any(), any()))
.thenAnswer((_) async => {});
// Create an AppReview instance
final appReview = AppReview(
localStorageService: mockLocalStorageService,
inAppReview: mockInAppReview,
);
// Call the method
await appReview.startReviewEngine();
// Verify interactions with LocalStorageService
verify(() => mockLocalStorageService.readJsonData(any())).called(1);
verify(() => mockLocalStorageService.createJsonData(
any(),
any(),
)).called(1);
});
test(
'AppReview - tryReview - requests review if criteria is met and InAppReview is available',
() async {
// Set expectations for LocalStorageService and InAppReview
when(() =>
mockLocalStorageService
.readJsonData(dotenv.get('APP_REVIEW_KEY'))).thenAnswer((_) => {
'launchCount': 3,
'threshold': 2,
});
when(() => mockInAppReview.isAvailable()).thenAnswer((_) async => true);
// Create an AppReview instance
final appReview = AppReview(
localStorageService: mockLocalStorageService,
inAppReview: mockInAppReview,
);
// Call the method
await appReview.tryReview();
// Verify interactions with LocalStorageService and InAppReview
verifyNever(() => mockInAppReview.isAvailable());
verifyNever(() => mockInAppReview.requestReview());
});
test(
'AppReview - setThresholdAlgorithm - sets the custom threshold algorithm',
() {
// Create an AppReview instance
final appReview = AppReview(
localStorageService: mockLocalStorageService,
inAppReview: mockInAppReview,
);
// Define a custom threshold algorithm
int customThreshold(int? currentThreshold) => currentThreshold! * 3;
// Set the custom algorithm
appReview.setThresholdAlgorithm(customThreshold);
// Verify that the custom algorithm is set
expect(appReview.thresholdAlgorithm, customThreshold);
});
test('AppReview - uses custom algorithm if set', () async {
// Set expectations for LocalStorageService
when(() => mockLocalStorageService.readJsonData(any()))
.thenAnswer((_) async => {
'threshold': 2,
'launchCount': 1,
});
when(() => mockInAppReview.isAvailable()).thenAnswer((_) async => true);
// Create an AppReview instance
final appReview = AppReview(
localStorageService: mockLocalStorageService,
inAppReview: mockInAppReview,
);
when(() => mockLocalStorageService.createJsonData(any(), any()))
.thenAnswer((_) async => appReview.data?.toJson());
// Define a custom threshold algorithm
int customThreshold(int? currentThreshold) => currentThreshold! * 3;
// Set the custom algorithm
appReview.setThresholdAlgorithm(customThreshold);
await appReview.startReviewEngine();
await appReview.tryReview();
// Verify that the custom algorithm was used
expect(appReview.threshold, 6);
});
test('AppReview - - uses default algorithm if custom not set', () async {
// Set expectations for LocalStorageService
when(() => mockLocalStorageService.readJsonData(any()))
.thenAnswer((_) async => {
'threshold': 2,
'launchCount': 1,
});
when(() => mockInAppReview.isAvailable()).thenAnswer((_) async => true);
// Create an AppReview instance
final appReview = AppReview(
localStorageService: mockLocalStorageService,
inAppReview: mockInAppReview,
);
when(() => mockLocalStorageService.createJsonData(any(), any()))
.thenAnswer((_) async => appReview.data?.toJson());
await appReview.startReviewEngine();
await appReview.tryReview();
// Verify that the custom algorithm was used
expect(appReview.threshold, 4);
});
});
// Add more tests for other methods of AppReview class...
}
import 'package:in_app_review/in_app_review.dart';
import 'package:mocktail/mocktail.dart';
import 'package:zylag/core/data/local_data_source.dart';
class MockLocalStorage extends Mock implements LocalStorageService {
MockLocalStorage() {
when(() => delete(any())).thenAnswer((_) async {});
}
}
class MockInAppReview extends Mock implements InAppReview {
MockInAppReview() {
when(() => requestReview()).thenAnswer((_) async {});
}
}