usernotnull / tall-toasts

A Toast notification library for the Laravel TALL stack. You can push notifications from the backend or frontend to render customizable toasts with almost zero footprint on the published CSS/JS 🔥🚀
MIT License
541 stars 32 forks source link

Assertion that toast was called (with options?) #464

Open nexxai opened 9 months ago

nexxai commented 9 months ago

Description

Ok, so in my application, I'd like to be able to do something like $this->assertToast(success, "Thanks for joining!"); to ensure that the toasts are actually being sent.

Additional context

n/a

usernotnull commented 9 months ago

Hey. You can easily be inspired from the tests in this library to make your own.

nexxai commented 9 months ago

I've spent the last couple hours actually trying that, but I can't seem to figure out how to verify that if I call ->push(); while inside a Livewire component, how to verify that the Toast actually appeared.

Example code:

public function sendInvite(): void
{
    ...

    toast()->success('Invite sent!')->push();
}

This test still fails:

test('a toast is pushed when invite is sent', function () {

    assertFalse(ToastManager::componentRendered());

    Livewire::test(Admin::class)
        ->set('emailAddress', 'bob@example.org')
        ->call('sendInvite');

    assertTrue(ToastManager::componentRendered());    // <---- this line is what fails
});

I tried using the tests in this package as a guide to write my own but I can't even figure out the basic "does the component show", let alone is it a "success" type with the correct message.

usernotnull commented 9 months ago

All what you can test for is that your message is sent and exists in the session(). Anything beyond that is actually tested in this library, so would be redundant.

If you want to test that it is actually displayed on the front end, then you'd have to use a tool like Laravel Dusk, as the js part of this app cannot be tested without such tools.

I'm open to PRs if anyone is willing to write such tests or expand on the existing ones.