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

[2.x] Add ability to perform toMatchSnapshot on Livewire components #18

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
"providers": [
"Pest\\Livewire\\PestServiceProvider"
]
}
},
"scripts": {
"lint": "pint",
"test:lint": "pint --test",
Expand Down
34 changes: 34 additions & 0 deletions src/PestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Pest\Livewire;

use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
use Laravel\Dusk\Console\DuskCommand;
use Livewire\Features\SupportTesting\Testable;
use Pest\Laravel\Commands\PestDatasetCommand;
use Pest\Laravel\Commands\PestDuskCommand;
use Pest\Laravel\Commands\PestTestCommand;
use Pest\Plugins\Snapshot;

final class PestServiceProvider extends ServiceProvider
{
/**
* Register Artisan Commands.
*/
public function register(): void
{
// Livewire v3.x
if(class_exists(Testable::class)) {
Snapshot::intercept(Testable::class, function (Testable $component): string {
return $component->html(stripInitialData: true);
});
}

Snapshot::macro('livewire.wire-key', function (string $value) {
return preg_replace('/wire:id="[0-9a-zA-Z]*"/', 'wire:id="..."', $value);
});
}
}