protonemedia / inertiajs-events-laravel-dusk Goto Github PK
View Code? Open in Web Editor NEWInertia.js Events for Laravel Dusk
Home Page: https://protone.media/en/blog/a-package-for-laravel-dusk-to-wait-for-inertiajs-events
License: MIT License
Inertia.js Events for Laravel Dusk
Home Page: https://protone.media/en/blog/a-package-for-laravel-dusk-to-wait-for-inertiajs-events
License: MIT License
Hi! I just wanted to propose a method for this package that does not require any special JavaScript code to work. This method uses the WebDriver's executeAsyncScript
function to attach an actual event listener.
This is the basic function:
use Facebook\WebDriver\Exception\ScriptTimeoutException;
use Facebook\WebDriver\Exception\TimeoutException;
use Laravel\Dusk\Browser;
function (string $name, ?int $seconds = null): Browser {
/** @var \Laravel\Dusk\Browser $this */
$seconds = is_null($seconds) ? $this::$waitSeconds : $seconds;
try {
$this->driver->manage()
->timeouts()
->setScriptTimeout($seconds);
$this->driver->executeAsyncScript("
const done = () => arguments[0]();
document.addEventListener('inertia:{$name}', done, { once: true });
");
} catch (ScriptTimeoutException $e) {
throw new TimeoutException("Waited {$seconds} seconds for an Inertia '{$name}' event.");
}
return $this;
}
I created a mixin class that can be used with Browser::mixin()
:
<?php
use Closure;
use Facebook\WebDriver\Exception\ScriptTimeoutException;
use Facebook\WebDriver\Exception\TimeoutException;
use Laravel\Dusk\Browser;
/** @mixin \Laravel\Dusk\Browser */
class InertiaEventsMixin
{
public function waitForInertia(?int $seconds = null): Closure
{
return fn (?int $seconds = null) => $this->waitForInertiaEvent('navigate', $seconds);
}
public function waitForInertiaError(?int $seconds = null): Closure
{
return fn (?int $seconds = null) => $this->waitForInertiaEvent('error', $seconds);
}
public function waitForInertiaFinish(?int $seconds = null): Closure
{
return fn (?int $seconds = null) => $this->waitForInertiaEvent('finish', $seconds);
}
public function waitForInertiaInvalid(?int $seconds = null): Closure
{
return fn (?int $seconds = null) => $this->waitForInertiaEvent('invalid', $seconds);
}
public function waitForInertiaSuccess(): Closure
{
return fn (?int $seconds = null) => $this->waitForInertiaEvent('success', $seconds);
}
public function waitForInertiaEvent(): Closure
{
return function (string $name, ?int $seconds = null): Browser {
/** @var \Laravel\Dusk\Browser $this */
$seconds = is_null($seconds) ? $this::$waitSeconds : $seconds;
try {
$this->driver->manage()
->timeouts()
->setScriptTimeout($seconds);
$this->driver->executeAsyncScript("
const done = () => arguments[0]();
document.addEventListener('inertia:{$name}', done, { once: true });
");
} catch (ScriptTimeoutException $e) {
throw new TimeoutException("Waited {$seconds} seconds for an Inertia '{$name}' event.");
}
return $this;
};
}
}
Then in my testing service provider:
\Laravel\Dusk\Browser::mixin(new InertiaEventsMixin);
I'd be happy to provide a pull request if you are interested.
Anyways, thanks for maintaining this package!
Cheers from Germany,
Michael
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.