Skip to content

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

Open
@Mauriceu

Description

@Mauriceu

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions