105 lines
2.9 KiB
Dart
105 lines
2.9 KiB
Dart
// Author: fruit.com.cn
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
|
import 'package:fruitshorts/core/data/remote/library_api.dart';
|
|
|
|
import '../support/fake_http_adapter.dart';
|
|
|
|
void main() {
|
|
test('should_request_favorites_with_page_and_parse_page_result', () async {
|
|
final adapter = FakeHttpAdapter(
|
|
(_) async => FakeHttpResponse(
|
|
jsonEncode({
|
|
'code': 0,
|
|
'message': 'ok',
|
|
'data': {
|
|
'total': '21',
|
|
'list': [
|
|
{'id': '9', 'title': '收藏剧', 'cover': '/files/9.png'},
|
|
],
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
final api = LibraryApi(
|
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
|
);
|
|
|
|
final page = await api.favorites(pageIndex: 2, pageSize: 10);
|
|
|
|
final request = adapter.requests.single;
|
|
expect(request.uri.path, '/app/favorites');
|
|
expect(request.queryParameters['pageIndex'], '2');
|
|
expect(request.queryParameters['pageSize'], '10');
|
|
expect(page.total, 21);
|
|
expect(page.list.single.id, '9');
|
|
expect(page.list.single.title, '收藏剧');
|
|
});
|
|
|
|
test('should_parse_favorites_when_server_returns_legacy_list', () async {
|
|
final adapter = FakeHttpAdapter(
|
|
(_) async => FakeHttpResponse(
|
|
jsonEncode({
|
|
'code': 0,
|
|
'message': 'ok',
|
|
'data': [
|
|
{'id': '5', 'title': '闪婚后', 'cover': '/files/5.png'},
|
|
{'id': '4', 'title': '另一部', 'cover': '/files/4.png'},
|
|
],
|
|
}),
|
|
),
|
|
);
|
|
final api = LibraryApi(
|
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
|
);
|
|
|
|
final page = await api.favorites();
|
|
|
|
expect(page.total, 2);
|
|
expect(page.list.map((drama) => drama.id), ['5', '4']);
|
|
expect(page.list.first.title, '闪婚后');
|
|
});
|
|
|
|
test('should_return_empty_favorites_when_data_is_null', () async {
|
|
final adapter = FakeHttpAdapter(
|
|
(_) async => FakeHttpResponse(
|
|
jsonEncode({'code': 0, 'message': 'ok', 'data': null}),
|
|
),
|
|
);
|
|
final api = LibraryApi(
|
|
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
|
);
|
|
|
|
final page = await api.favorites();
|
|
|
|
expect(page.total, 0);
|
|
expect(page.list, isEmpty);
|
|
});
|
|
|
|
test('should_throw_api_exception_when_favorites_business_code_failed', () async {
|
|
final adapter = FakeHttpAdapter(
|
|
(_) async => FakeHttpResponse(
|
|
jsonEncode({'code': 401, 'message': '未登录或登录已过期', 'data': null}),
|
|
),
|
|
);
|
|
final api = LibraryApi(
|
|
ApiClient(
|
|
baseUrl: 'http://127.0.0.1:8081',
|
|
adapter: adapter,
|
|
// 无刷新回调时,401 直接抛业务异常,避免重试逻辑。
|
|
tokenRefresher: null,
|
|
),
|
|
);
|
|
|
|
await expectLater(
|
|
() => api.favorites(),
|
|
throwsA(
|
|
isA<ApiException>().having((e) => e.code, 'code', 401),
|
|
),
|
|
);
|
|
});
|
|
}
|