christophrumpel / missing-livewire-assertions

Adding the missing Livewire assertions to your Laravel project
MIT License
136 stars 25 forks source link

assertContainsLivewireComponent fails when components are not in main Livewire namespace #9

Open titantwentyone opened 3 years ago

titantwentyone commented 3 years ago

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!

christophrumpel commented 3 years ago

Thanks, I will give that a look. 👍

christophrumpel commented 2 years ago

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

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

titantwentyone commented 1 year ago

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.