Files
wechat_kit/test/wechat_kit_test.dart
2021-07-31 12:51:34 +08:00

130 lines
4.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pedantic/pedantic.dart';
import 'package:wechat_kit/wechat_kit.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const MethodChannel channel = MethodChannel('v7lin.github.io/wechat_kit');
setUp(() {
channel.setMockMethodCallHandler((MethodCall call) async {
switch (call.method) {
case 'registerApp':
return null;
case 'isInstalled':
return true;
case 'isSupportApi':
return true;
case 'openWechat':
return true;
case 'auth':
unawaited(channel.binaryMessenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(MethodCall(
'onAuthResp',
json.decode(
'{"country":"CN","code":null,"errorCode":-2,"state":null,"lang":"zh_CN","errorMsg":null}'))),
(ByteData? data) {
print('mock ${channel.name} ${call.method}');
},
));
return null;
case 'startQrauth':
case 'stopQrauth':
throw PlatformException(code: '0', message: '懒得去mock');
case 'openUrl':
case 'openRankList':
throw PlatformException(code: '0', message: '懒得去mock');
case 'shareText':
case 'shareImage':
case 'shareEmoji':
case 'shareMusic':
case 'shareVideo':
case 'shareWebpage':
case 'shareMiniProgram':
unawaited(channel.binaryMessenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(MethodCall('onShareMsgResp',
json.decode('{"errorCode":0,"errorMsg":null}'))),
(ByteData? data) {
print('mock ${channel.name} ${call.method}');
},
));
return null;
case 'subscribeMsg':
case 'launchMiniProgram':
throw PlatformException(code: '0', message: '懒得去mock');
case 'pay':
unawaited(channel.binaryMessenger.handlePlatformMessage(
channel.name,
channel.codec.encodeMethodCall(MethodCall(
'onPayResp',
json.decode(
'{"errorCode":-2,"returnKey":"","errorMsg":null}'))),
(ByteData? data) {
print('mock ${channel.name} ${call.method}');
},
));
return null;
}
throw PlatformException(code: '0', message: '想啥呢升级插件不想升级Mock');
});
});
tearDown(() {
channel.setMockMethodCallHandler(null);
});
test('isInstalled', () async {
expect(await Wechat.instance.isInstalled(), true);
});
test('isSupportApi', () async {
expect(await Wechat.instance.isSupportApi(), true);
});
test('auth', () async {
final StreamSubscription<BaseResp> sub =
Wechat.instance.respStream().listen((BaseResp resp) {
expect(resp.runtimeType, AuthResp);
expect(resp.errorCode, BaseResp.ERRORCODE_USERCANCEL);
});
await Wechat.instance.auth(scope: <String>[WechatScope.SNSAPI_USERINFO]);
await sub.cancel();
});
test('share', () async {
final StreamSubscription<BaseResp> sub =
Wechat.instance.respStream().listen((BaseResp resp) {
expect(resp.runtimeType, ShareMsgResp);
expect(resp.errorCode, BaseResp.ERRORCODE_SUCCESS);
});
await Wechat.instance
.shareText(scene: WechatScene.SESSION, text: 'share text');
await sub.cancel();
});
test('pay', () async {
final StreamSubscription<BaseResp> sub =
Wechat.instance.respStream().listen((BaseResp resp) {
expect(resp.runtimeType, PayResp);
expect(resp.errorCode, BaseResp.ERRORCODE_USERCANCEL);
});
await Wechat.instance.pay(
appId: 'mock',
partnerId: 'mock',
prepayId: 'mock',
package: 'mock',
nonceStr: 'mock',
timeStamp: 'mock',
sign: 'mock',
);
await sub.cancel();
});
}