Skip to content

Commit

Permalink
Tests the response macros
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Dec 31, 2020
1 parent 765b446 commit 2466cc2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/NamesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/TurboStreamResponseMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]));
}

Expand All @@ -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],
]));
}
}
94 changes: 94 additions & 0 deletions tests/Http/ResponseMacrosTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Tonysm\TurboLaravel\Tests\Http;

use Illuminate\Support\Facades\View;
use Tonysm\TurboLaravel\Tests\TestCase;

class ResponseMacrosTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

View::addNamespace('test-stubs', __DIR__ . '/stubs/');
}

/** @test */
public function streams_model_on_create()
{
$testModel = TestModel::create(['name' => 'test']);

$expected = <<<html
<turbo-stream target="test_models" action="append">
<template>
<div id="test_model_{$testModel->id}">hello</div>
</template>
</turbo-stream>
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
<turbo-stream target="test_model_{$testModel->id}" action="replace">
<template>
<div id="test_model_{$testModel->id}">hello</div>
</template>
</turbo-stream>
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
<turbo-stream target="test_model_{$testModel->id}" action="remove"></turbo-stream>
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 = <<<html
<div id="test_model_{$testModel->id}">hello</div>
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";
}
}
1 change: 1 addition & 0 deletions tests/Http/stubs/_test_model.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="@domid($testModel)">hello</div>

0 comments on commit 2466cc2

Please sign in to comment.