Skip to content
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

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

Open
titantwentyone opened this issue Aug 12, 2021 · 3 comments

Comments

@titantwentyone
Copy link
Contributor

titantwentyone commented Aug 12, 2021

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
Copy link
Owner

Thanks, I will give that a look. 👍

@christophrumpel
Copy link
Owner

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?

@titantwentyone
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants