Files
platforms/apps/user_app/test/data/cart_test.dart
czl231 3ef33b531d 已完成用户APP首期功能开发
交付用户端首期页面、配套接口、后台资源及测试文档。用户APP构建、静态分析和三个管理后台构建通过;完整测试仍有2项失败,后端模型注释检查未通过,详见交付记录。
2026-09-13 00:57:32 +08:00

78 lines
2.6 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.
// 功能描述购物车公开标识、整数金额及登录失效契约回归版本1.0.0。
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:user_app/data/repositories/client_repository.dart';
import 'package:user_app/data/services/api_client.dart';
import 'package:user_app/domain/models/cart_item.dart';
void main() {
test('购物车迟到响应不跨账号继续加购或更新界面', () async {
var token = 'old';
final repo = ClientRepository(
ApiClient(
() => token,
client: MockClient((_) async {
token = 'new';
return http.Response('{"code":0,"details":{}}', 200);
}),
),
);
await expectLater(repo.cartItem('p1'), throwsA(isA<SessionExpiredException>()));
});
test('条件数量更新携带原版本,图片仅接受可信协议', () async {
final value = <String, Object?>{
'product_identity': 'p1',
'name': '商品',
'price_amount': 1999,
'stock_quantity': 4,
'quantity': 2,
'selected': false,
'available': true,
'revision': 'revision',
'image_url': 'javascript:alert(1)',
};
final repo = ClientRepository(
ApiClient(
() => 'test-token',
client: MockClient((request) async {
expect(request.headers['authorization'], 'test-token');
if (request.method == 'PUT') {
expect(jsonDecode(request.body), {
'quantity': 2,
'selected': false,
'revision': 'revision',
});
}
return http.Response(
jsonEncode({'code': 0, 'details': value}),
200,
headers: {'content-type': 'application/json; charset=utf-8'},
);
}),
),
);
final item = await repo.cartItem('p1');
expect(item.product.imageUrl, '');
expect(item.amount, 3998);
expect((await repo.setCartItem(item, quantity: 2, selected: false)).selected, isFalse);
expect(
() => CartItem.fromJson({...value, 'price_amount': 1.5}, (s) => s),
throwsFormatException,
);
});
test('购物车接口401清理会话', () async {
var expired = false;
final repo = ClientRepository(
ApiClient(
() => 'test',
onUnauthorized: (_) => expired = true,
client: MockClient((_) async => http.Response('{"code":1301}', 401)),
),
);
await expectLater(repo.cart(), throwsA(isA<SessionExpiredException>()));
expect(expired, isTrue);
});
}