Skip to content

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

@titantwentyone

Description

@titantwentyone

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions