Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onSubmitted test fail #138

Closed
alegos27 opened this issue May 29, 2023 · 1 comment
Closed

onSubmitted test fail #138

alegos27 opened this issue May 29, 2023 · 1 comment
Assignees

Comments

@alegos27
Copy link

alegos27 commented May 29, 2023

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

To Reproduce

  • Run this test:
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));
      },
    );
  • This is the result of test:

    The following TestFailure was thrown running a test:
    Expected: '123456'
    Actual:
    Which: not an <Instance of 'String'>

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
Copy link
Owner

Tkko commented Aug 2, 2023

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

@Tkko Tkko closed this as completed Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants