|
| 1 | +import 'package:edge_functions_example/functions_repository.dart'; |
| 2 | +import 'package:edge_functions_example/main.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_test/flutter_test.dart'; |
| 5 | +import 'package:integration_test/integration_test.dart'; |
| 6 | +import 'package:supabase_flutter/supabase_flutter.dart'; |
| 7 | + |
| 8 | +const supabaseUrl = String.fromEnvironment('SUPABASE_URL'); |
| 9 | +const supabasePublishableKey = String.fromEnvironment( |
| 10 | + 'SUPABASE_PUBLISHABLE_KEY', |
| 11 | +); |
| 12 | + |
| 13 | +/// End-to-end tests that drive the Edge Functions example against the local |
| 14 | +/// stack. |
| 15 | +/// |
| 16 | +/// The first test exercises the core flow through the repository (a JSON |
| 17 | +/// greeting over POST and GET, a plain-text transform, and a validation error), |
| 18 | +/// asserting on what each function returns. The second drives the app widgets to |
| 19 | +/// confirm the greeting card is wired to the function. Edge Functions are |
| 20 | +/// stateless, so there is nothing to clean up between runs. |
| 21 | +void main() { |
| 22 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 23 | + |
| 24 | + late FunctionsRepository repository; |
| 25 | + |
| 26 | + setUpAll(() async { |
| 27 | + await Supabase.initialize( |
| 28 | + url: supabaseUrl, |
| 29 | + publishableKey: supabasePublishableKey, |
| 30 | + ); |
| 31 | + repository = FunctionsRepository(Supabase.instance.client); |
| 32 | + }); |
| 33 | + |
| 34 | + tearDownAll(() async { |
| 35 | + await Supabase.instance.dispose(); |
| 36 | + }); |
| 37 | + |
| 38 | + testWidgets('invokes the example functions through the repository', ( |
| 39 | + tester, |
| 40 | + ) async { |
| 41 | + // POST with a JSON body: the function greets and echoes how it was called. |
| 42 | + final posted = await repository.greet(name: 'Ada', excited: true); |
| 43 | + expect(posted.message, 'Hello, Ada!!!'); |
| 44 | + expect(posted.method, 'POST'); |
| 45 | + expect(posted.source, 'flutter-app'); |
| 46 | + |
| 47 | + // GET with a query parameter reaches the same function a different way. |
| 48 | + final fetched = await repository.greetViaQuery('Grace'); |
| 49 | + expect(fetched.message, 'Hello, Grace.'); |
| 50 | + expect(fetched.method, 'GET'); |
| 51 | + |
| 52 | + // A plain-text response comes back as a String. |
| 53 | + final shouted = await repository.shout('edge functions'); |
| 54 | + expect(shouted, 'EDGE FUNCTIONS'); |
| 55 | + |
| 56 | + // A JSON response decodes into the model. |
| 57 | + final count = await repository.countWords('one two three'); |
| 58 | + expect(count.words, 3); |
| 59 | + expect(count.characters, 13); |
| 60 | + |
| 61 | + // An empty input makes the function respond with a 400, which surfaces as a |
| 62 | + // FunctionException carrying the JSON error body. |
| 63 | + await expectLater( |
| 64 | + repository.countWords(''), |
| 65 | + throwsA( |
| 66 | + isA<FunctionException>() |
| 67 | + .having((error) => error.status, 'status', 400) |
| 68 | + .having( |
| 69 | + (error) => (error.details as Map)['error'], |
| 70 | + 'details.error', |
| 71 | + isA<String>(), |
| 72 | + ), |
| 73 | + ), |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + testWidgets('greets through the UI', (tester) async { |
| 78 | + await tester.pumpWidget(const EdgeFunctionsExampleApp()); |
| 79 | + await tester.pumpAndSettle(); |
| 80 | + |
| 81 | + // Tapping "Greet (POST)" invokes the function and shows its message. |
| 82 | + await tester.tap(find.widgetWithText(FilledButton, 'Greet (POST)')); |
| 83 | + await _pumpUntil(tester, find.textContaining('Hello, Ada')); |
| 84 | + expect(find.textContaining('via POST'), findsOneWidget); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +/// Pumps frames until [finder] matches at least one widget or [timeout] elapses. |
| 89 | +/// |
| 90 | +/// Invoking a function goes over the network, so the UI can't be settled with |
| 91 | +/// `pumpAndSettle`; this polls the widget tree instead. |
| 92 | +Future<void> _pumpUntil( |
| 93 | + WidgetTester tester, |
| 94 | + Finder finder, { |
| 95 | + Duration timeout = const Duration(seconds: 20), |
| 96 | +}) async { |
| 97 | + final deadline = DateTime.now().add(timeout); |
| 98 | + while (DateTime.now().isBefore(deadline)) { |
| 99 | + await tester.pump(const Duration(milliseconds: 100)); |
| 100 | + if (finder.evaluate().isNotEmpty) return; |
| 101 | + } |
| 102 | + fail('Timed out waiting for: $finder'); |
| 103 | +} |
0 commit comments