Files
pipelines/app/test/features/runs/actions/retry_test_support.dart
T

113 lines
2.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/actions/retry_action.dart';
typedef RetryHandler = Future<JsonMap> Function(String type, JsonMap fields);
/// A fake gateway that records every request and never owns a socket.
class FakeRetryGateway implements ApiGateway {
FakeRetryGateway(this.handler);
final RetryHandler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
Iterable<({String type, JsonMap fields})> get retries =>
calls.where((call) => call.type == 'retry_run');
}
JsonMap failedRun({
num id = 7,
bool executing = false,
String status = 'failed',
String title = 'Nightly build',
}) => {'id': id, 'title': title, 'status': status, 'executing': executing};
JsonMap retriedRun({num id = 8}) =>
failedRun(id: id, status: 'running', executing: true, title: 'Retry of 7');
/// Owns the run like a run-detail screen would, so the callback's returned
/// server detail is what the action renders next.
class RetryHost extends StatefulWidget {
const RetryHost({
super.key,
required this.gateway,
required this.initial,
required this.changed,
});
final FakeRetryGateway gateway;
final JsonMap initial;
final List<JsonMap> changed;
@override
State<RetryHost> createState() => _RetryHostState();
}
class _RetryHostState extends State<RetryHost> {
late JsonMap _run = widget.initial;
@override
Widget build(BuildContext context) {
return RetryRunAction(
run: _run,
gateway: widget.gateway,
onRunChanged: (run) {
widget.changed.add(run);
setState(() => _run = run);
},
);
}
}
Widget retryApp(Widget child, {required bool cupertino, double textScale = 1}) {
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
);
if (cupertino) {
return CupertinoApp(
builder: media,
home: CupertinoPageScaffold(child: Center(child: child)),
);
}
return MaterialApp(
builder: media,
home: Scaffold(body: Center(child: child)),
);
}
Future<void> pumpRetry(
WidgetTester tester, {
required FakeRetryGateway gateway,
required List<JsonMap> changed,
bool cupertino = false,
double textScale = 1,
JsonMap? run,
}) async {
await tester.pumpWidget(
retryApp(
RetryHost(
gateway: gateway,
initial: run ?? failedRun(),
changed: changed,
),
cupertino: cupertino,
textScale: textScale,
),
);
}