Skip to content

Commit 8094445

Browse files
committed
Fix tests now that a live id is always added
Some tests don't go through a normal render flow, where a Template would be available, so we'll add a dummy live id for tests (except those where the live id itself is tested).
1 parent 41889f4 commit 8094445

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

src/LiveComponent/tests/Functional/EventListener/AddLiveAttributesSubscriberTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ public function testInitLiveComponent(): void
4040
$this->assertSame('live', $div->attr('data-controller'));
4141
$this->assertSame('/_components/component_with_writable_props', $div->attr('data-live-url-value'));
4242
$this->assertNotNull($div->attr('data-live-csrf-value'));
43-
$this->assertCount(3, $props);
43+
$this->assertCount(4, $props);
4444
$this->assertSame(5, $props['max']);
4545
$this->assertSame(1, $props['count']);
4646
$this->assertArrayHasKey('@checksum', $props);
47+
$this->assertArrayHasKey('@attributes', $props);
48+
$this->assertArrayHasKey('data-live-id', $props['@attributes']);
4749
}
4850

4951
public function testCanUseCustomAttributesVariableName(): void

src/LiveComponent/tests/Integration/EventListener/DataModelPropsSubscriberTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function testDataModelPropsAreSharedToChild(): void
3333
// content is mapped down to "value" in a child component
3434
'content' => 'Hello data-model!',
3535
'content2' => 'Value for second child',
36+
// Normally createAndRender is always called from within a Template via the ComponentExtension.
37+
// To avoid that the DeterministicTwigIdCalculator complains that there's no Template
38+
// to base the live id on, we'll add this dummy one, so it gets skipped.
39+
'attributes' => ['data-live-id' => 'dummy-live-id'],
3640
]);
3741

3842
$this->assertStringContainsString('<textarea data-model="content:value">Hello data-model!</textarea>', $html);

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ public function testCorrectlyUsesCustomFrontendNameInDehydrateAndHydrate(): void
12151215

12161216
public function testCanDehydrateAndHydrateComponentsWithAttributes(): void
12171217
{
1218-
$mounted = $this->mountComponent('with_attributes', $attributes = ['class' => 'foo', 'value' => null]);
1218+
$mounted = $this->mountComponent('with_attributes', $attributes = ['class' => 'foo', 'value' => null], false);
12191219

12201220
$this->assertSame($attributes, $mounted->getAttributes()->all());
12211221

@@ -1231,7 +1231,7 @@ public function testCanDehydrateAndHydrateComponentsWithAttributes(): void
12311231

12321232
public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void
12331233
{
1234-
$mounted = $this->mountComponent('with_attributes');
1234+
$mounted = $this->mountComponent('with_attributes', [], false);
12351235

12361236
$this->assertSame([], $mounted->getAttributes()->all());
12371237

src/LiveComponent/tests/Integration/Twig/LiveComponentRuntimeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function testGetComponentUrl(): void
3131
'prop1' => null,
3232
'prop2' => new \DateTime('2022-10-06-0'),
3333
'prop3' => 'howdy',
34+
'attributes' => ['data-live-id' => 'in-a-real-scenario-it-would-already-have-one'],
3435
]);
3536

3637
$this->assertStringStartsWith('/_components/component1?props=%7B%22prop1%22:null,%22prop2%22:%222022-10-06T00:00:00%2B00:00%22,%22prop3%22:%22howdy%22,%22', $url);

src/LiveComponent/tests/LiveComponentTestHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ private function getComponent(string $name): object
3838
return $this->factory()->get($name);
3939
}
4040

41-
private function mountComponent(string $name, array $data = []): MountedComponent
41+
private function mountComponent(string $name, array $data = [], $addDummyLiveId = true): MountedComponent
4242
{
43+
if ($addDummyLiveId && empty($data['attributes']['data-live-id'])) {
44+
$data['attributes']['data-live-id'] = 'in-a-real-scenario-it-would-already-have-one';
45+
}
46+
4347
return $this->factory()->create($name, $data);
4448
}
4549

0 commit comments

Comments
 (0)