Skip to content

Instantly share code, notes, and snippets.

@michaelhue
Created April 17, 2022 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelhue/6d62cd0470ecac2b21d296151e25fc76 to your computer and use it in GitHub Desktop.
Save michaelhue/6d62cd0470ecac2b21d296151e25fc76 to your computer and use it in GitHub Desktop.
InertiaBrowser
<?php
namespace App\Tests;
use Orchestra\Testbench\Dusk\TestCase as BaseTestCase;
use App\Testing\Concerns\CreatesApplication;
use App\Testing\Concerns\ProvidesInertiaBrowser;
abstract class BrowserTestCase extends BaseTestCase
{
use CreatesApplication, ProvidesInertiaBrowser;
}
<?php
namespace App\Testing;
use Laravel\Dusk\Browser;
class InertiaBrowser extends Browser
{
public const INERTIA_ERROR = 'error';
public const INERTIA_EXCEPTION = 'exception';
public const INERTIA_FINISH = 'finish';
public const INERTIA_INVALID = 'invalid';
public const INERTIA_NAVIGATE = 'navigate';
public const INERTIA_SUCCESS = 'success';
/**
* Browse to the given URL and wait for Inertia navigation.
*/
public function visitInertia(string $url, ?int $seconds = null): self
{
return $this->visit($url)->waitForInertia($seconds);
}
/**
* Browse to the given route and wait for Inertia navigation.
*
* @param array<string, string> $parameters
*/
public function visitInertiaRoute(
string $route,
array $parameters = [],
?int $seconds = null
): self {
return $this->visitRoute($route, $parameters)->waitForInertia($seconds);
}
/**
* Click the link with the given text and wait for Inertia navigation.
*/
public function clickLinkAndWaitForInertia(
string $link,
string $element = 'a',
?int $seconds = null
): self {
return $this->clickLink($link, $element)->waitForInertia($seconds);
}
/**
* Press the button with the given text or name and wait for Inertia
* navigation.
*/
public function pressAndWaitForInertia(
string $button,
?int $seconds = null
): self {
return $this->press($button)->waitForInertia($seconds);
}
/**
* Wait for Inertia's navigate event.
*
* The event fires on successful page visits, as well as when navigating
* through history.
*
* @see https://inertiajs.com/events#navigate
*/
public function waitForInertia(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_NAVIGATE, $seconds);
}
/**
* Wait for Inertia's error event.
*
* The event fires when validation errors are present on "successful" page
* visits.
*
* @see https://inertiajs.com/events#error
*/
public function waitForInertiaError(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_NAVIGATE, $seconds);
}
/**
* Wait for Inertia's exception event.
*
* The event fies when on unexpected XHR errors, such as network
* interruptions, and for errors generated when resolving page components.
*
* @see https://inertiajs.com/events#exception
*/
public function waitForInertiaException(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_EXCEPTION, $seconds);
}
/**
* Wait for Inertia's finish event.
*
* The event fires after an XHR request has completed for both
* successful and unsuccessful responses.
*
* @see https://inertiajs.com/events#finish
*/
public function waitForInertiaFinish(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_FINISH, $seconds);
}
/**
* Wait for Inertia's invalid event.
*
* The event fires when a non-Inertia response is received from the
* server, such as an HTML or JSON response.
*
* @see https://inertiajs.com/events#invalid
*/
public function waitForInertiaInvalid(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_INVALID, $seconds);
}
/**
* Wait for Inertia's success event.
*
* The event fires on successful page visits, unless validation errors
* are present.
*
* @see https://inertiajs.com/events#success
*/
public function waitForInertiaSuccess(?int $seconds = null): self
{
return $this->waitForInertiaEvent(static::INERTIA_SUCCESS, $seconds);
}
/**
* Wait for the given Inertia event.
*
* @see https://inertiajs.com/events
*/
protected function waitForInertiaEvent(
string $eventName,
?int $seconds = null
): self {
return $this->waitForEvent("inertia:${eventName}", 'document', $seconds);
}
}
<?php
namespace App\Testing\Concerns;
use App\Testing\InertiaBrowser;
trait ProvidesInertiaBrowser
{
/**
* Create a new Browser instance.
*
* @param \Facebook\WebDriver\Remote\RemoteWebDriver $driver
* @return \Laravel\Dusk\Browser
*/
public function newBrowser($driver)
{
return new InertiaBrowser($driver);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment