Git Product home page Git Product logo

inertiajs-events-laravel-dusk's People

Contributors

pascalbaljet avatar ramonmalcolm10 avatar sebastiaanluca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

inertiajs-events-laravel-dusk's Issues

Proposal for a method without any special JavaScript code

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.