Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InteractsWithLiveComponent] Validate the response of a failed LiveAction #2675

Open
Mauriceu opened this issue Apr 2, 2025 · 0 comments
Open
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed

Comments

@Mauriceu
Copy link

Mauriceu commented Apr 2, 2025

Consider the following: A client invokes a LiveComponents LiveAction that does some work. However, that LiveAction fails somehow and raises an UnprocessableEntityHttpException to re-render the Component, e.g. to render an error message.

As an example consider this LiveAction:

#[AsLiveComponent(template: 'component.html.twig')]
class Component {
    #[LiveAction]
    public function doSomething(): void
    {
         // do some work...
         if ($success === false) {
                  // this property is rendered in the template
                  $this->rendered_errors[] = "Oh no, something went wrong ...";
                  throw new UnprocessableEntityHttpException();
         }
    }
}

A test for this LiveAction would look something like this:

class ComponentTest extends KernelTestCase {
    use InteractsWithLiveComponents;

    public function testLiveActionError(): void
    {
        $component = $this->createLiveComponent(Component::class);
        try {
            $component = $testComponent->call('doSomething');
        } catch (\Throwable $e) {
            self::assertInstanceOf(UnprocessableEntityHttpException::class, $e);

            // I would expect the response to contain html populated with the values in the "rendered_errors" property
            // however, this is not the case. It seems the previous content is being retrieved.
            // $html = $testComponent->response()->getContent();
            // self::assertStringContainsString('Oh no, something went wrong ...', $html);
        }
    }
}

As it stands it is impossible to assert the rendered content upon failure of a LiveAction, though I'd love to be proven wrong.
I built some workarounds for this issue, but its kind of a messy hassle honestly. Any ideas?

@Mauriceu Mauriceu added the Bug Bug Fix label Apr 2, 2025
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
Projects
None yet
Development

No branches or pull requests

2 participants