passsy / spot

Chainable powerful Flutter widget selector API, screenshots and assertions for awesome widget tests.
https://pub.dev/packages/spot
Apache License 2.0
62 stars 1 forks source link

Add act.enterTtext() #51

Closed rehlma closed 6 months ago

rehlma commented 6 months ago

Add the tester.enterText() functionality to Spot

testWidgets('enter text in text form field', (tester) async {
    await tester.pumpWidget(
      MaterialApp(home: Material(child: Form(child: TextFormField()))),
    );
    await act.enterText(spot<TextFormField>(), 'hello');
    spotText('hello').existsOnce();
});
passsy commented 6 months ago

I tried it, here's an example test for a login form.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:f93_forming_forms/main.dart';
import 'package:spot/spot.dart';

final textFieldLabelText = widgetProp<TextField, String?>(
  'labelText',
  (textField) => textField.decoration?.labelText,
);

void main() {
  group('Part 1', () {
    testWidgets('enter username password', (tester) async {
      await tester.pumpWidget(FormingFormsKata());

      final email = spot<TextField>()
          .whereWidgetProp(textFieldLabelText, (label) => label == 'Email')
        ..existsOnce();
      await act.enterText(email, 'pascal@phntm.xyz');

      final password = spot<TextField>()
          .whereWidgetProp(textFieldLabelText, (label) => label == 'Password')
        ..existsOnce();
      await act.enterText(password, 'asdfasdf');

      await act.tap(spotText('Accept AGBs'));
      await tester.pump();

      // wait for async matchers and debounce
      await tester.pump(Duration(milliseconds: 250));
      await tester.pump(Duration(seconds: 2));

      await act.tap(spotText('Sign in'));
      await tester.pump();
      await tester.pump(Duration(seconds: 1));

      spotText('Success').existsOnce();
    });
  });
}