Tkko / Flutter_Pinput

Flutter package to create Pin code input text field with every pixel customization possibility 🎨 with beautiful animations, iOS autofill, Android autofill
https://pub.dev/packages/pinput
MIT License
777 stars 179 forks source link

onSubmitted test fail #138

Closed alegos27 closed 1 year ago

alegos27 commented 1 year ago

Describe the bug During test seems onSubmitted callback does't work

To Reproduce

testWidgets(
      'Pinput should call the onSubmitted callback when pin input is submitted',
      (WidgetTester tester) async {
        final pinController = TextEditingController();
        String? submittedValue;

        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: Pinput(
                controller: pinController,
                onSubmitted: (value) {
                  submittedValue = value;
                },
                length: 6,
              ),
            ),
          ),
        );

        const pinValue = '123456';
        await tester.enterText(find.byType(Pinput), pinValue);
        await tester.testTextInput.receiveAction(TextInputAction.done);
        await tester.pumpAndSettle();

        expect(pinController.text, equals(pinValue));
        expect(submittedValue, equals(pinValue));
      },
    );

Pinput version: 2.2.31

Result of: flutter doctor --verbose

[βœ“] Flutter (Channel stable, 3.10.2, on macOS 13.3.1 22E261 darwin-arm64, locale en-IT) β€’ Flutter version 3.10.2 on channel stable at /Users/alessiogoslino/development/flutter β€’ Upstream repository https://github.com/flutter/flutter.git β€’ Framework revision 9cd3d0d9ff (5 days ago), 2023-05-23 20:57:28 -0700 β€’ Engine revision 90fa3ae28f β€’ Dart version 3.0.2 β€’ DevTools version 2.23.1 [βœ“] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4) β€’ Android SDK at /Users/alessiogoslino/Library/Android/sdk β€’ Platform android-33, build-tools 33.0.0-rc4 β€’ Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java β€’ Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) β€’ All Android licenses accepted. [βœ“] Xcode - develop for iOS and macOS (Xcode 14.2) β€’ Xcode at /Applications/Xcode.app/Contents/Developer β€’ Build 14C18 β€’ CocoaPods version 1.12.1 [βœ“] Android Studio (version 2022.1) β€’ Android Studio at /Applications/Android Studio.app/Contents β€’ Flutter plugin can be installed from: πŸ”¨ https://plugins.jetbrains.com/plugin/9212-flutter β€’ Dart plugin can be installed from: πŸ”¨ https://plugins.jetbrains.com/plugin/6351-dart β€’ Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) [βœ“] VS Code (version 1.78.2) β€’ VS Code at /Applications/Visual Studio Code.app/Contents β€’ Flutter extension version 3.64.0 [βœ“] VS Code (version 1.61.1) β€’ VS Code at /Users/alessiogoslino/Creative Cloud Files/DOWNLOAD01/Visual Studio Code.app/Contents β€’ Flutter extension version 3.64.0 [βœ“] Connected device (3 available) β€’ iPhone A (mobile) β€’ 72f626bbeae9dcfe39118556c9a336436a6bb905 β€’ ios β€’ iOS 16.3.1 20D67 β€’ iPhone 14 (mobile) β€’ 8B1A7A9B-DF9F-47E5-9100-321E6FAD7AF6 β€’ ios β€’ com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator) β€’ iPhone 14 Pro (mobile) β€’ FFB17208-8744-43C4-BF7E-354FF91F9539 β€’ ios β€’ com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator) [βœ“] Network resources β€’ All expected network resources are available. β€’ No issues found!
Tkko commented 1 year ago

Hey @alegos27, you are entering the 123456 and it's length is the same as Pinput's length, what happens is that when you call await tester.testTextInput.receiveAction(TextInputAction.done); the keyboard is already closed by Pinput so the onSumbitted callback is never called.

This test case works fine.

testWidgets(
      'Pinput should call the onSubmitted callback when pin input is submitted',
      (WidgetTester tester) async {
        final pinController = TextEditingController();
        String? submittedValue;

        await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              body: Pinput(
                controller: pinController,
                onSubmitted: (value) {
                  submittedValue = value;
                },
                length: 6,
              ),
            ),
          ),
        );

        const pinValue = '12345';
        await tester.enterText(find.byType(Pinput), pinValue);
        await tester.testTextInput.receiveAction(TextInputAction.done);
        await tester.pumpAndSettle();

        expect(pinController.text, equals(pinValue));
        expect(submittedValue, equals(pinValue));
      },
    );

Note that if you need a callback which is fired when pin is fully entered you should use onCompleted property