Skip to content

Commit 2466cc2

Browse files
committed
Tests the response macros
1 parent 765b446 commit 2466cc2

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

src/NamesResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public static function resourceNameSingular(Model $model): string
2727
return static::resourceNameSingularFor(class_basename($model));
2828
}
2929

30+
public static function resourceVariableName(Model $model): string
31+
{
32+
return Str::camel(static::resourceNameSingular($model));
33+
}
34+
3035
public static function partialNameFor(Model $model): string
3136
{
3237
$root = static::resourceName($model);

src/TurboStreamResponseMacro.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function renderModelAddedStream(Model $model, string $action = null)
5252
: NamesResolver::partialNameFor($model),
5353
'data' => method_exists($model, 'hotwirePartialData')
5454
? $model->hotwirePartialData()
55-
: [NamesResolver::resourceNameSingular($model) => $model],
55+
: [NamesResolver::resourceVariableName($model) => $model],
5656
]));
5757
}
5858

@@ -62,13 +62,13 @@ private function renderModelUpdatedStream(Model $model)
6262
'target' => method_exists($model, 'hotwireTargetDomId')
6363
? $model->hotwireTargetDomId()
6464
: NamesResolver::resourceId($model::class, $model->id),
65-
'action' => 'update',
65+
'action' => 'replace',
6666
'resourcePartialName' => method_exists($model, 'hotwirePartialName')
6767
? $model->hotwirePartialName()
6868
: NamesResolver::partialNameFor($model),
6969
'data' => method_exists($model, 'hotwirePartialData')
7070
? $model->hotwirePartialData()
71-
: [NamesResolver::resourceNameSingular($model) => $model],
71+
: [NamesResolver::resourceVariableName($model) => $model],
7272
]));
7373
}
7474
}

tests/Http/ResponseMacrosTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Tonysm\TurboLaravel\Tests\Http;
4+
5+
use Illuminate\Support\Facades\View;
6+
use Tonysm\TurboLaravel\Tests\TestCase;
7+
8+
class ResponseMacrosTest extends TestCase
9+
{
10+
public function setUp(): void
11+
{
12+
parent::setUp();
13+
14+
View::addNamespace('test-stubs', __DIR__ . '/stubs/');
15+
}
16+
17+
/** @test */
18+
public function streams_model_on_create()
19+
{
20+
$testModel = TestModel::create(['name' => 'test']);
21+
22+
$expected = <<<html
23+
<turbo-stream target="test_models" action="append">
24+
<template>
25+
<div id="test_model_{$testModel->id}">hello</div>
26+
</template>
27+
</turbo-stream>
28+
html;
29+
30+
$resp = response()->turboStream($testModel);
31+
32+
$this->assertEquals($expected, trim($resp->getContent()));
33+
$this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type'));
34+
}
35+
36+
/** @test */
37+
public function streams_model_on_update()
38+
{
39+
$testModel = TestModel::create(['name' => 'test'])->fresh();
40+
41+
$expected = <<<html
42+
<turbo-stream target="test_model_{$testModel->id}" action="replace">
43+
<template>
44+
<div id="test_model_{$testModel->id}">hello</div>
45+
</template>
46+
</turbo-stream>
47+
html;
48+
49+
$resp = response()->turboStream($testModel);
50+
51+
$this->assertEquals($expected, trim($resp->getContent()));
52+
$this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type'));
53+
}
54+
55+
/** @test */
56+
public function streams_model_on_delete()
57+
{
58+
$testModel = tap(TestModel::create(['name' => 'test']))->delete();
59+
60+
$expected = <<<html
61+
<turbo-stream target="test_model_{$testModel->id}" action="remove"></turbo-stream>
62+
html;
63+
64+
$resp = response()->turboStream($testModel);
65+
66+
$this->assertEquals($expected, trim($resp->getContent()));
67+
$this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type'));
68+
}
69+
70+
/** @test */
71+
public function streams_custom_view()
72+
{
73+
$testModel = TestModel::create(['name' => 'test']);
74+
75+
$expected = <<<html
76+
<div id="test_model_{$testModel->id}">hello</div>
77+
html;
78+
79+
$resp = response()->turboStreamView(View::file(__DIR__ . '/stubs/_test_model.blade.php', [
80+
'testModel' => $testModel,
81+
]));
82+
83+
$this->assertEquals($expected, trim($resp->getContent()));
84+
$this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type'));
85+
}
86+
}
87+
88+
class TestModel extends \Tonysm\TurboLaravel\Tests\TestModel
89+
{
90+
public function hotwirePartialName()
91+
{
92+
return "test-stubs::_test_model";
93+
}
94+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="@domid($testModel)">hello</div>

0 commit comments

Comments
 (0)