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

[Live] add test helper #823

Merged
merged 1 commit into from
Aug 17, 2023
Merged

[Live] add test helper #823

merged 1 commit into from
Aug 17, 2023

Conversation

kbond
Copy link
Member

@kbond kbond commented Apr 29, 2023

Q A
Bug fix? no
New feature? yes
Tickets Continuation of #821
License MIT
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Test\InteractsWithLiveComponents;

class MyComponentTest extends KernelTestCase
{
    use InteractsWithLiveComponents;

    public function testThisComponent(): void
    {
        $testComponent = $this->createLiveComponent('my_component');
        $testComponent = $this->createLiveComponent(Component::class); // or use the FQCN
        
        (string) $testComponent->render(); // string - initial state html
        $testComponent->component(); // object - component instance at initial state
        
        $testComponent
            ->call('increase') // call a live action
            ->call('increase', ['amount' => 2]) // call a live action with arguments
            ->set('count', 5) // set a live prop
            ->emit('inceaseEvent') // emit a live event
            ->emit('inceaseEvent', ['amount' => 2]) // emit a live event with arguments
            ->refresh() // simply refresh the component
        ;
        
        (string) $testComponent->render(); // string - updated state html
        $testComponent->component(); // object - component instance at updated state

        /** @var Symfony\Component\HttpFoundation\Response $response */
        $response = $testComponent->call('actionThatRedirects')->response();
    }
}

TODO:

  • handle action responses
  • ->refresh() method?
  • Docs
  • Changelog

@kbond
Copy link
Member Author

kbond commented Apr 29, 2023

Again, with the system I describe in #818 (comment), we could make a really nice chainable test experience:

$this->createLiveComponent('my_component')
    ->assertSeeIn('div.count', 1)
    ->call('increase')
    ->assertSeeIn('div.count', 2)
    ->call('increase', ['amount' => 3])
    ->assertSeeIn('div.count', 5)
;

@WebMamba
Copy link
Contributor

Hey @kbond, I am using this PR a lot to test my component and it's working great! Do you need a hand in order to finish it?

@kbond
Copy link
Member Author

kbond commented Aug 16, 2023

I am using this PR a lot to test my component and it's working great! Do you need a hand in order to finish it?

I was really just waiting for this feedback! Think it's current state is enough to start?

@kbond kbond force-pushed the live-test-helpers branch from f9892d9 to 4b06ebd Compare August 16, 2023 13:57
@kbond kbond changed the title [Live][WIP] add test helper [Live] add test helper Aug 16, 2023
@kbond kbond marked this pull request as ready for review August 16, 2023 13:59
@kbond kbond force-pushed the live-test-helpers branch from 4b06ebd to 4e22914 Compare August 16, 2023 14:01
@WebMamba
Copy link
Contributor

Think it's current state is enough to start?

I guess, I mean I didn't have a complex need yet but for my current need, it's working perfectly fine.

@kbond
Copy link
Member Author

kbond commented Aug 16, 2023

Ok, I'll add refresh()/response() methods + docs and I think we should be good to go here.

@kbond kbond force-pushed the live-test-helpers branch 2 times, most recently from eb6012f to 04cff46 Compare August 16, 2023 16:26
@kbond
Copy link
Member Author

kbond commented Aug 16, 2023

Ok, I believe this to be ready!

I also added 586a572 but can move to another PR if desired.

@kbond kbond requested a review from weaverryan August 16, 2023 16:41
@weaverryan
Copy link
Member

Thank you Kevin!

@weaverryan weaverryan merged commit 25f0ab8 into symfony:2.x Aug 17, 2023
@kbond kbond deleted the live-test-helpers branch August 17, 2023 15:08
weaverryan added a commit that referenced this pull request Sep 19, 2023
This PR was merged into the 2.x branch.

Discussion
----------

fix(live): fix testing events

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Tickets       | Fix #1110
| License       | MIT

When implementing #823, I made the wrong assumption that events were identical to actions. This fixes that.

Commits
-------

3bb139b fix(live): fix testing events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants