ux icon indicating copy to clipboard operation
ux copied to clipboard

[Autocomplete] Testing an autocomplete

Open HiroKX opened this issue 2 years ago • 12 comments

Hello !

I'm currently testing an application that uses symfony/ux-autocomplete in a form and i'm facing an issue where i can't test my form because there is no possible values.

Do you have an idea of what i can do to test this field ?

PHPUnit : 9.6 Symfony : 6.4 UX-Autocomplete : 2.13.2

HiroKX avatar Dec 13 '23 15:12 HiroKX

Hey,

https://github.com/symfony/symfony/pull/47642 should help if someone will care :D

norkunas avatar Dec 13 '23 16:12 norkunas

With Crawler you might need to add the <option value="..."> element first, e.g. using ChoiceFormField::addChoice($node) because there is a special check that validates the value option before setting it, simulating what happens in the browser via Javascript.

yceruto avatar Dec 13 '23 18:12 yceruto

@yceruto I've tried this but there is still a problem - it's impossible to add choice to single select element.

https://github.com/symfony/symfony/blob/0d9562ff6cdda11c71f0eb2bce0076f0d3a8ea9f/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php#L146-L148

so it's simulating a wrong behavior?

norkunas avatar Dec 14 '23 10:12 norkunas

I've been through the same issue here and didn't find any solutions at that time...

HiroKX avatar Dec 14 '23 11:12 HiroKX

I found that ChoiceFormField has disableValidation method but it is internal. Tried some hacks with Reflection to populate the option to it's option list and then setting value, but still form populates an error that value should not be blank while I explicitly set it..

norkunas avatar Dec 14 '23 11:12 norkunas

You do want a full application test ? Or an unit -- integration ? Can you share a bit of code ? There are as many ways to use those classes than there are developers..

smnandre avatar Dec 14 '23 11:12 smnandre

For sure an functional test

norkunas avatar Dec 14 '23 11:12 norkunas

It's really surprising that ChoiceFormField::addChoice() only work for multiple and radio choices... I don't get why this limitation and also why marked as internal when it's public and useful. I will check.

yceruto avatar Dec 14 '23 13:12 yceruto

If someone was inclined to create a mini reproducer that could help motivate people to digg into this with you

smnandre avatar Dec 14 '23 13:12 smnandre

Hey guys!

I'm facing an issue with this test where I try to create a user from a form:

    public function testNew(): void
    {
        $this->client->loginUser($this->userRepository->findOneBy(['username' => 'admin']));
        $originalNumObjectsInRepository = $this->userRepository->count([]);
        $this->client->request('GET', $this->generateNewFormUrl());
        $this->client->submitForm('Create', [
            'User[username]' => 'Testing',
            'User[email]' => '[email protected]',
            'User[client][autocomplete]' => '1', //This field is the one facing the issue
            'User[password][first]' => 'test',
            'User[password][second]' => 'test',
        ]);
        self::assertResponseStatusCodeSame(302);
        self::assertSame($originalNumObjectsInRepository + 1, $this->userRepository->count([]));
   }

And I'm getting this error: 1) App\Tests\CrudController\UserCrudControllerTest::testNew InvalidArgumentException: Input "User[client][autocomplete]" cannot take "1" as a value (possible values: ""). Even if I'm passing an object or an id...

I'd like to add an option in my autocomplete field (maybe with ChoiceFormField::addChoice()) but I'm facing the LogicException...

HiroKX avatar Dec 14 '23 13:12 HiroKX

As temporal, ugly workaround, you can add the new options this way (this should be done before submitting the form):

\Closure::bind(function () {
    // add here the new options e.g.
    $this->options[] = ['value' => '1']; 
}, $field, ChoiceFormField::class)();

Being $field an instance of ChoiceFormField.

yceruto avatar Dec 14 '23 16:12 yceruto

I also have the same issue. As a workaround, Instead of using submitForm(), I do POST requests and it works.

See https://github.com/symfony/ux/blob/2.x/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php for an example with zenstruck/browser.

simondaigre avatar Jan 17 '24 20:01 simondaigre