Skip to content

[LiveComponent] Add a new helper to interact with forms in functional tests #1992

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

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

yoye
Copy link
Contributor

@yoye yoye commented Jul 17, 2024

Q A
Bug fix? no
New feature? yes
License MIT

Add a new method in TestLiveComponent to interact with live components that contains forms. Submitted data must be passed as an array like you'll do with KernelBrowser.

$component = $this->createLiveComponent(name: Component::class);
$component->submitForm(['foo' => ['bar' => 'baz']]); // The method will change this values in ['foo.bar' => 'baz'] as expected by the component.

@carsonbot carsonbot added Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed labels Jul 17, 2024
@yoye yoye changed the title [LiveComponent] Add a new [LiveComponent] Add a new helper to interact with forms in functional tests Jul 17, 2024
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is beautiful! I thought adding this would be much more complex!

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Jul 18, 2024
@kbond kbond requested a review from smnandre July 18, 2024 13:04
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Reviewed Has been reviewed by a maintainer labels Jul 18, 2024
@@ -3659,7 +3659,7 @@ uses Symfony's test client to render and make requests to your components::

// call live action with file uploads
$testComponent
->save('processUpload', files: ['file' => new UploadedFile(...)]);
->call('processUpload', files: ['file' => new UploadedFile(...)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted!

@@ -3672,6 +3672,10 @@ uses Symfony's test client to render and make requests to your components::
->set('count', 99)
;

// Submit form data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should create an entire block "Test LiveComponent with Forms" (or something like that), wdyt ?

If "no", i think some comment to precise what is "form" and "save" here could avoid some missunderstandings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Test helper is only one block, I didn't want to change that. But I understand that my array is a bit confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep as it is..

Who knows, mabe if you add other nice helpers... we will be forced to add a dedicated block ? :))


foreach ($values as $key => $value) {
if (\is_array($value)) {
$result += $this->flattenFormValues($value, $prefix.$key.'.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference array_is_list / map ? (geniune question)

Does this work with fields accepting multiple values ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how to use array_is_list here. With array_map I can't handle keys.

Yes, I was a little undecided to not add a test for that method. At first I've thinked to add a public static method in Symfony\UX\LiveComponent\Util\LiveFormUtility which could be easily tested. But I don't know if it make sence as it will be created only for test purpose.

But this will work.

    public function testFlatten(): void
    {
        $data = [
            'foo' => ['bar' => 'baz'],
            'collection' => ['one', 'two', 'three'],
            'collection_with_sub' => [
                'sub_one' => ['child_one', 'child_two'],
                'sub_two' => ['child_three', 'key' => 'child_four'],
            ],
        ];

        $this->assertSame([
            'foo.bar' => 'baz',
            'collection.0' => 'one',
            'collection.1' => 'two',
            'collection.2' => 'three',
            'collection_with_sub.sub_one.0' => 'child_one',
            'collection_with_sub.sub_one.1' => 'child_two',
            'collection_with_sub.sub_two.0' => 'child_three',
            'collection_with_sub.sub_two.key' => 'child_four',
        ], TestLiveComponent::flattenFormValues($data));
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for me then, thank you for taking time to answer :)

@smnandre
Copy link
Member

I let some questions about details, but overall very happy to see this PR: thank you very much @yoye !

And i second @kbond, I thought it would be much more complex so .. 👏

Copy link
Member

@smnandre smnandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Jul 19, 2024
@kbond kbond force-pushed the functional-test-submit-form branch from 71be401 to b23eaee Compare July 24, 2024 18:19
@kbond
Copy link
Member

kbond commented Jul 24, 2024

So great, thanks @yoye!

@kbond kbond merged commit 2b35ecb into symfony:2.x Jul 24, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature LiveComponent Status: Reviewed Has been reviewed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants