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/templates/screen.dart'; import 'package:pipelines_app/features/templates/ui/template_create_wording.dart'; import '../data/template_test_support.dart' show source; import 'template_gallery_test_support.dart'; Future openRelease(WidgetTester tester) async { await tester.tap(find.byKey(templateRowKey('release'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(templateCreateActionKey)); await tester.pumpAndSettle(); } void main() { testWidgets( 'Cancel sends nothing; Create validates and submits the verbatim source ' 'once through the existing prompt', (tester) async { final gateway = GalleryGateway((type, fields) async { if (type == 'list_templates') return templatesReply(galleryTemplates); return acceptedCreate(type, fields); }); final created = []; await pumpGallery(tester, gateway, onCreated: created.add); await openRelease(tester); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Create from Release workflow'), findsOneWidget); await tester.enterText(find.byKey(templateCreateNameKey), 'Café'); await tester.pump(); await tester.tap(find.byKey(templateCreateCancelKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(gateway.callsOf('validate'), hasLength(2)); expect(gateway.callsOf('submit'), isEmpty); expect(created, isEmpty); await tester.tap(find.byKey(templateCreateActionKey)); await tester.pumpAndSettle(); expect(find.byKey(templateCreateNameKey), findsOneWidget); await tester.enterText(find.byKey(templateCreateNameKey), ' Café '); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.callsOf('validate'), hasLength(3)); expect(gateway.callsOf('validate').last.fields, {'source': source}); expect(gateway.callsOf('submit').single.fields, { 'name': 'Café', 'source': source, 'create_only': true, }); expect(gateway.callsOf('submit').single.fields.keys, [ 'name', 'source', 'create_only', ]); expect(created, [ {'id': 7, 'name': 'Created release'}, ]); expect( find.text('Created "Created release". It is in the Pipelines list.'), findsOneWidget, ); expect(find.text('Close'), findsOneWidget); await tester.tap(find.byKey(templateCreateCancelKey)); await tester.pumpAndSettle(); expect(find.byKey(templateDetailKey), findsOneWidget); expect(created, hasLength(1)); expect(gateway.callsOf('submit'), hasLength(1)); }, ); testWidgets('the callback is optional and Cupertino uses its native prompt', ( tester, ) async { final gateway = GalleryGateway((type, fields) async { if (type == 'list_templates') return templatesReply(galleryTemplates); return acceptedCreate(type, fields); }); await pumpGallery(tester, gateway, cupertino: true); await openRelease(tester); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); await tester.enterText(find.byKey(templateCreateNameKey), 'Cupertino'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.callsOf('validate'), hasLength(3)); expect(gateway.callsOf('submit'), hasLength(1)); expect( find.text('Created "Created release". It is in the Pipelines list.'), findsOneWidget, ); expect(find.byKey(templateCreateCancelKey), findsOneWidget); }); }