Skip to content

Commit 09c002a

Browse files
committed
#11 Add one more test for savingCallback()
1 parent 63a7638 commit 09c002a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/UnlayerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace InteractionDesignFoundation\NovaUnlayerField\Tests;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use InteractionDesignFoundation\NovaUnlayerField\Unlayer;
7+
use Laravel\Nova\Fields\Field;
8+
use Laravel\Nova\Http\Requests\NovaRequest;
69

710
final class UnlayerTest extends TestCase
811
{
@@ -16,6 +19,26 @@ public function it_resolves_callback_to_html_code(): void
1619
$this->assertSame('<p>Hello!</p>', $field->meta()['html'] ?? null);
1720
}
1821

22+
/** @test */
23+
public function it_properly_runs_savingCallback(): void
24+
{
25+
$inMemoryModel = new class extends Model {
26+
public string $design = '';
27+
public string $html = '';
28+
};
29+
$field = Unlayer::make('Design', 'design')
30+
->savingCallback(function (NovaRequest $request, $attribute, Model $model, $outputHtmlFieldName) {
31+
$model->html = $request->input($outputHtmlFieldName);
32+
});
33+
34+
$this->emulateNovaUpdateRequestForSingleField($field, 'design', $inMemoryModel, [
35+
'design' => '{}',
36+
'design_html' => '<p>Hello!</p>', // automatically added field. Pattern: [original field name] + _html (in this case "design" + "_html")
37+
]);
38+
39+
$this->assertSame('<p>Hello!</p>', $inMemoryModel->html);
40+
}
41+
1942
/** @test */
2043
public function it_accepts_array_as_config(): void
2144
{
@@ -39,4 +62,16 @@ public function it_accepts_callable_as_config(): void
3962
$this->assertArrayHasKey('projectId', $field->meta()['config']);
4063
$this->assertSame('XXX', $field->meta()['config']['projectId']);
4164
}
65+
66+
/**
67+
* White box testing of Nova field needed to unsure
68+
* that custom functionality [savingCallback() method] works as expected.
69+
* @param array<string, mixed> $resourceUpdateRequestData
70+
* @return void
71+
*/
72+
private function emulateNovaUpdateRequestForSingleField(Field $field, string $fieldAttributeName, Model $model, array $resourceUpdateRequestData): void
73+
{
74+
$request = NovaRequest::create('', 'POST', $resourceUpdateRequestData);
75+
$field->fillInto($request, $model, $fieldAttributeName);
76+
}
4277
}

0 commit comments

Comments
 (0)