Professional Documents
Culture Documents
SK Methodchannel Apis Test - Dart
SK Methodchannel Apis Test - Dart
SK Methodchannel Apis Test - Dart
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:in_app_purchase/src/channel.dart';
import 'package:in_app_purchase/store_kit_wrappers.dart';
import 'sk_test_stub_objects.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() {
SystemChannels.platform
.setMockMethodCallHandler(fakeIOSPlatform.onMethodCall);
});
setUp(() {});
group('sk_request_maker', () {
test('get products method channel', () async {
SkProductResponseWrapper productResponseWrapper =
await SKRequestMaker().startProductRequest(['xxx']);
expect(
productResponseWrapper.products,
isNotEmpty,
);
expect(
productResponseWrapper.products.first.priceLocale.currencySymbol,
'\$',
);
expect(
productResponseWrapper.products.first.priceLocale.currencySymbol,
isNot('A'),
);
expect(
productResponseWrapper.products.first.priceLocale.currencyCode,
'USD',
);
expect(
productResponseWrapper.invalidProductIdentifiers,
isNotEmpty,
);
expect(
fakeIOSPlatform.startProductRequestParam,
['xxx'],
);
});
group('sk_receipt_manager', () {
test('should get receipt (faking it by returning a `receipt data` string)',
() async {
String receiptData = await SKReceiptManager.retrieveReceiptData();
expect(receiptData, 'receipt data');
});
});
group('sk_payment_queue', () {
test('canMakePayment should return true', () async {
expect(await SKPaymentQueueWrapper.canMakePayments(), true);
});
test(
'throws if observer is not set for payment queue before adding payment',
() async {
expect(SKPaymentQueueWrapper().addPayment(dummyPayment),
throwsAssertionError);
});
class FakeIOSPlatform {
FakeIOSPlatform() {
channel.setMockMethodCallHandler(onMethodCall);
getProductRequestFailTest = false;
}
// get product request
List startProductRequestParam;
bool getProductRequestFailTest;
// payment queue
List<SKPaymentWrapper> payments = [];
List<String> transactionsFinished = [];
String applicationNameHasTransactionRestored;
void paymentQueueRestoreCompletedTransactionsFinished() {}
bool shouldAddStorePayment(
{SKPaymentWrapper payment, SKProductWrapper product}) {
return true;
}
}