ralphjsmit / pest-plugin-livewire
A plugin for Pest to test Livewire forms.
Installs: 23 590
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.0
- illuminate/support: ^8.76|^9.0|^10.0
- livewire/livewire: ^2.8
- pestphp/pest: ^1.17|^2.0
- pestphp/pest-plugin: ^1.0|^2.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.0|^8.0
README
This package provides a convenient way to test your Livewire forms and speed up your workflow. It adds helpers that make repetitive tasks faster, like testing whether properties are required or not.
Important
This package works only with Livewire V2 and up to Laravel 10. Therefore, it will not be updated to support Livewire V3 and/or Laravel 11 or higher. The repository will be archived.
Contents
- Expectations
- Functions
Expectations
expect(...)->toHaveRequiredProperties()
Test whether Livewire properties are required. Consider the following dummy Livewire component:
class TestRequiredPropertiesComponent extends Component { public string $email_Req = ''; public string $email = ''; public ?int $age_Req = null; public ?int $age = null; protected array $rules = [ 'email' => '', 'email_Req' => 'required', 'age' => 'numeric', 'age_Req' => 'numeric|required', ]; public function render(): string { return '<div></div>'; } public function submit() { $this->validate(); }
use function RalphJSmit\PestPluginLivewire\validInput; $component = Livewire::test(TestRequiredPropertiesComponent::class); $validInput = validInput([ 'email' => 'alex@example.com', 'age' => 25, ]); expect($component) ->toHaveRequiredProperties(validInput, ['email_Req', 'age_Req'], 'submit');
NB.: Using
->not
to negate the test is currently not yet supported.
expect(...)->toNotHaveRequiredProperties()
Test whether Livewire properties aren't required. Let's continue the example from above with a test for the properties that are not required:
use function RalphJSmit\PestPluginLivewire\validInput; $component = Livewire::test(TestRequiredPropertiesComponent::class); $validInput = validInput([ 'email' => 'alex@example.com', 'age' => 25, ]); expect($component) ->toNotHaveRequiredProperties(validInput, ['email', 'name'], 'submit');
NB.: Using
->not
to negate the test is currently not yet supported.
Functions
assertRequiredProperties()
Use this function to test whether Livewire properties are required. Consider using an expect(...)
call if you want to chain multiple expectations:
use function RalphJSmit\PestPluginLivewire\assertRequiredProperties; assertRequiredProperties(TestableLivewire $livewire, Collection $validInput, array $requiredProperties, string $submitFunction);
assertNotRequiredProperties()
Use this function to test whether Livewire properties aren't required. Consider using an expect(...)
call if you want to chain multiple expectations:
use function RalphJSmit\PestPluginLivewire\assertNotRequiredProperties; assertNotRequiredProperties(TestableLivewire $livewire, Collection $validInput, array $requiredProperties, string $submitFunction);
validInput()
Use this function to get a ValidInput
object (Laravel Collection). It's needed for using the above functions.
General
🐞 If you spot a bug, please submit a detailed issue and I'll try to fix it as soon as possible.
🔐 If you discover a vulnerability, please review our security policy.
🙌 If you want to contribute, please submit a pull request. All PRs will be fully credited. If you're unsure whether I'd accept your idea, feel free to contact me!
🙋♂️ Ralph J. Smit