From 2466cc274cecafbcd6a266a0e0789aee8ce16b41 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Thu, 31 Dec 2020 00:00:43 -0300 Subject: [PATCH] Tests the response macros --- src/NamesResolver.php | 5 ++ src/TurboStreamResponseMacro.php | 6 +- tests/Http/ResponseMacrosTest.php | 94 ++++++++++++++++++++++++++ tests/Http/stubs/_test_model.blade.php | 1 + 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 tests/Http/ResponseMacrosTest.php create mode 100644 tests/Http/stubs/_test_model.blade.php diff --git a/src/NamesResolver.php b/src/NamesResolver.php index fdc4f66..85244ac 100644 --- a/src/NamesResolver.php +++ b/src/NamesResolver.php @@ -27,6 +27,11 @@ public static function resourceNameSingular(Model $model): string return static::resourceNameSingularFor(class_basename($model)); } + public static function resourceVariableName(Model $model): string + { + return Str::camel(static::resourceNameSingular($model)); + } + public static function partialNameFor(Model $model): string { $root = static::resourceName($model); diff --git a/src/TurboStreamResponseMacro.php b/src/TurboStreamResponseMacro.php index fa6cf2d..1997d8b 100644 --- a/src/TurboStreamResponseMacro.php +++ b/src/TurboStreamResponseMacro.php @@ -52,7 +52,7 @@ private function renderModelAddedStream(Model $model, string $action = null) : NamesResolver::partialNameFor($model), 'data' => method_exists($model, 'hotwirePartialData') ? $model->hotwirePartialData() - : [NamesResolver::resourceNameSingular($model) => $model], + : [NamesResolver::resourceVariableName($model) => $model], ])); } @@ -62,13 +62,13 @@ private function renderModelUpdatedStream(Model $model) 'target' => method_exists($model, 'hotwireTargetDomId') ? $model->hotwireTargetDomId() : NamesResolver::resourceId($model::class, $model->id), - 'action' => 'update', + 'action' => 'replace', 'resourcePartialName' => method_exists($model, 'hotwirePartialName') ? $model->hotwirePartialName() : NamesResolver::partialNameFor($model), 'data' => method_exists($model, 'hotwirePartialData') ? $model->hotwirePartialData() - : [NamesResolver::resourceNameSingular($model) => $model], + : [NamesResolver::resourceVariableName($model) => $model], ])); } } diff --git a/tests/Http/ResponseMacrosTest.php b/tests/Http/ResponseMacrosTest.php new file mode 100644 index 0000000..6d4c232 --- /dev/null +++ b/tests/Http/ResponseMacrosTest.php @@ -0,0 +1,94 @@ + 'test']); + + $expected = << + + +html; + + $resp = response()->turboStream($testModel); + + $this->assertEquals($expected, trim($resp->getContent())); + $this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type')); + } + + /** @test */ + public function streams_model_on_update() + { + $testModel = TestModel::create(['name' => 'test'])->fresh(); + + $expected = << + + +html; + + $resp = response()->turboStream($testModel); + + $this->assertEquals($expected, trim($resp->getContent())); + $this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type')); + } + + /** @test */ + public function streams_model_on_delete() + { + $testModel = tap(TestModel::create(['name' => 'test']))->delete(); + + $expected = << +html; + + $resp = response()->turboStream($testModel); + + $this->assertEquals($expected, trim($resp->getContent())); + $this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type')); + } + + /** @test */ + public function streams_custom_view() + { + $testModel = TestModel::create(['name' => 'test']); + + $expected = <<hello +html; + + $resp = response()->turboStreamView(View::file(__DIR__ . '/stubs/_test_model.blade.php', [ + 'testModel' => $testModel, + ])); + + $this->assertEquals($expected, trim($resp->getContent())); + $this->assertEquals('text/html; turbo-stream', $resp->headers->get('Content-Type')); + } +} + +class TestModel extends \Tonysm\TurboLaravel\Tests\TestModel +{ + public function hotwirePartialName() + { + return "test-stubs::_test_model"; + } +} diff --git a/tests/Http/stubs/_test_model.blade.php b/tests/Http/stubs/_test_model.blade.php new file mode 100644 index 0000000..7e8f78d --- /dev/null +++ b/tests/Http/stubs/_test_model.blade.php @@ -0,0 +1 @@ +
hello