Skip to content

[LiveComponent] Add assert in test live component #2712

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

Open
wants to merge 11 commits into
base: 2.x
Choose a base branch
from

Conversation

Arkalo2
Copy link

@Arkalo2 Arkalo2 commented May 5, 2025

Q A
Bug fix? no
New feature? yes
Docs? yes
Issues no
License MIT

This MR simplify liveComponent event testing :
Actually with the Test Helper, we can't simply test whether an event has been dispatched from a component.
This PR add 2 asserts :

$this->componentHasEmittedEvent($render, 'event-name', [
    'eventArg1' => $eventArg1,
]);
$this->assertComponentNotEmitEvent($render, 'event-name');

@carsonbot carsonbot added Bug Bug Fix Feature New Feature Status: Needs Review Needs to be reviewed labels May 5, 2025
@Arkalo2 Arkalo2 marked this pull request as draft May 5, 2025 14:29
@Arkalo2 Arkalo2 force-pushed the feat/add-live-component-event-test branch from c994d18 to a43398b Compare June 10, 2025 10:14
@Arkalo2 Arkalo2 marked this pull request as ready for review June 10, 2025 12:44
@Arkalo2 Arkalo2 force-pushed the feat/add-live-component-event-test branch from bc67a41 to 3f1ce0c Compare June 10, 2025 12:47
Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Jun 11, 2025
Copy link
Member

@Kocal Kocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor wordings and types, but otherwise that's fine, thanks :)

Comment on lines 58 to 71
foreach ($parameters as $key => $value) {
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will helps by displaying a nice diff between expected vs actual data:

Suggested change
foreach ($parameters as $key => $value) {
$this->assertSame($value, $eventData['data'][$key] ?? null, \sprintf('EventData (%s) is not valid', $key));
}
$this->assertEquals(expectedEventData, $event['data'], sprintf('The expected event "%s" data does not match.', $eventName));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep but with the global assertEquals, we must pass all expectedEventDatas.

If the event contains ['a' => 'b', 'c' => 'd'];
And I expect only ['a' => 'b'];
It throw an error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but I think that's the desired behavior. Asserting the whole event data makes more sense to me, but I may be biaised and I understand that sometimes you expect to assert an array subset, but it must be explicit.

I've seen it tons of time on multiple projects, where sometimes you assert everything, or sometimes only a subset, and every time that's a mess because the method-name / PHPUnit's way of working does not help.

I can suggest you this, so everyone would be happy:

// classic way
$this->assertComponentEmitEvent($component, 'foo'); // do not care about event data
$this->assertComponentEmitEventWithData($component, 'foo', []); // assert whole array
$this->assertComponentEmitEventWithDataSubset($component, 'foo', []); // assert subset array

// or a bit exotic 
$this->assertComponentEmitEvent($component, 'foo'); // do not care about event data
$this->assertComponentEmitEvent($component, 'foo')->withData([]); // assert whole array
$this->assertComponentEmitEvent($component, 'foo')->withDataSubset([]); // assert subset array

WDYT?

@carsonbot carsonbot added Status: Needs Work Additional work is needed Status: Needs Review Needs to be reviewed and removed Status: Reviewed Has been reviewed by a maintainer Status: Needs Work Additional work is needed labels Jun 11, 2025
@Arkalo2 Arkalo2 force-pushed the feat/add-live-component-event-test branch from 258f0ee to d859d84 Compare June 12, 2025 10:00
@Arkalo2 Arkalo2 force-pushed the feat/add-live-component-event-test branch from 32e10b9 to 2c5191f Compare June 12, 2025 14:25
@Kocal Kocal removed the Bug Bug Fix label Jun 13, 2025
@Kocal Kocal changed the title Add assert in test live component [LiveComponent] Add assert in test live component Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature LiveComponent Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants