|
| 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 | +} |
0 commit comments