ralphjsmit/pest-plugin-livewire

This package is abandoned and no longer maintained. No replacement package was suggested.

A plugin for Pest to test Livewire forms.

2.0 2023-04-18 17:43 UTC

This package is auto-updated.

Last update: 2024-04-14 08:13:20 UTC


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

  1. Expectations
    1. expect(...)->toHaveRequiredProperties()
    2. expect(...)->toNotHaveRequiredProperties()
  2. Functions
    1. assertRequiredProperties(...)
    2. assertNotRequiredProperties(...)
    3. validInput(...)

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