missing-livewire-assertions icon indicating copy to clipboard operation
missing-livewire-assertions copied to clipboard

assertContainsLivewireComponent fails when components are not in main Livewire namespace

Open titantwentyone opened this issue 3 years ago • 3 comments

When testing against a component which is in a subdirectory of the main Livewire namespace, assertContainsLivewireComponent fails

namespace App\Http\Livewire\Components;

use Livewire\Component;

class MyComponent extends Component
{
   //...
}

test:

Livewire::test(MyParentComponent::class)
    ->assertContainsLivewireComponent(MyComponent::class);

Error comes back:

matches PCRE pattern "/@livewire\('my-component'|<livewire\:my-component/".

The following is okay however:

Livewire::test(MyParentComponent::class)
    ->assertContainsLivewireComponent('components.my-component);

Tried to look at a fix. Since assertContainsLivewireComponent uses basename() to strip out the class, I used the following (possibly ugly) fix:

        return function (string $componentNeedleClass) {
            $componentNeedle = Str::of($componentNeedleClass)
                ->remove('App\Http\Livewire\\')
                ->explode('\\')
                ->map(function($item)
                {
                    return Str::kebab($item);
                })
                ->implode('.');
             ...

I wanted to provide a pull request but, while this works me, your tests fail. I guess this is down to the fact that the test components are not in App\Http\Livewire. I'm no Livewire aficionado so I wasn't sure how to resolve this! Happy to help if I can however!

titantwentyone avatar Aug 12 '21 12:08 titantwentyone

Thanks, I will give that a look. 👍

christophrumpel avatar Aug 19 '21 07:08 christophrumpel

Hey @titantwentyone, sorry for the late reply. Can you tell if this is still an issue? Just tried it locally with:

  • parent component in App\Http\Livewire\ and child component in App\Http\Livewire\Components
  • and both in 'App\Http\Livewire\Components``

In my tests, both situations are working. Can you maybe check again?

christophrumpel avatar Nov 08 '22 08:11 christophrumpel

Hi @christophrumpel. My turn to apologise for the delay in responding. I've just upgraded a few apps to Laravel10 and Livewire/Filament v3. This assertion didn't come up much but it's useful when it did. I've provided a repo to demonstrate what I'm seeing here. Tests in AssertionsTest using Pest.

Think it's a case of ensuring the namespace of the child component is taken into account.

I've added a quick fix for this using assertContainsLivewireComponent2 which you'll see in CustomLivewireAssertionsMixin. If you're happy, I can PR.

titantwentyone avatar Oct 08 '23 11:10 titantwentyone