Skip to content

Commit d74e804

Browse files
committed
Add tests
1 parent 1a50d82 commit d74e804

4 files changed

Lines changed: 102 additions & 10 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{{ $name }}</h1><p>{{ $text }}</p>

tests/Http/Form/FormControllerTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use Code16\Sharp\Form\Fields\SharpFormAutocompleteLocalField;
88
use Code16\Sharp\Form\Fields\SharpFormCheckField;
99
use Code16\Sharp\Form\Fields\SharpFormEditorField;
10+
use Code16\Sharp\Form\Fields\SharpFormHtmlField;
11+
use Code16\Sharp\Form\Fields\SharpFormListField;
1012
use Code16\Sharp\Form\Fields\SharpFormTextField;
1113
use Code16\Sharp\Form\Layout\FormLayout;
1214
use Code16\Sharp\Form\Layout\FormLayoutColumn;
15+
use Code16\Sharp\Form\SharpForm;
1316
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
1417
use Code16\Sharp\Tests\Fixtures\Entities\SinglePersonEntity;
1518
use Code16\Sharp\Tests\Fixtures\Sharp\PersonForm;
@@ -606,3 +609,77 @@ public function buildFormConfig(): void
606609
->where('form.title', 'My custom create title')
607610
);
608611
});
612+
613+
it('handles html fields', function () {
614+
fakeFormFor('person', new class() extends SharpForm
615+
{
616+
public function buildFormFields(FieldsContainer $formFields): void
617+
{
618+
$formFields
619+
->addField(
620+
SharpFormTextField::make('name')
621+
)
622+
->addField(
623+
SharpFormHtmlField::make('html_string')
624+
->setTemplate('<h1>{{ $name }}</h1><p>{{ $text }}</p>')
625+
)
626+
->addField(
627+
SharpFormHtmlField::make('html_view')
628+
->setTemplate(view('fixtures::form-html-field'))
629+
)
630+
->addField(
631+
SharpFormHtmlField::make('html_closure')
632+
->setTemplate(fn ($data) => sprintf('<h1>%s</h1><p>%s</p>', $data['name'], $data['text'])
633+
)
634+
)
635+
->addField(
636+
SharpFormListField::make('list')
637+
->addItemField(
638+
SharpFormTextField::make('list_name')
639+
)
640+
->addItemField(
641+
SharpFormHtmlField::make('list_html')
642+
->setTemplate(fn ($data) => sprintf('<h1>%s</h1><p>%s</p>', $data['list_name'], $data['text'])
643+
)
644+
)
645+
);
646+
}
647+
648+
public function buildFormLayout(FormLayout $formLayout): void
649+
{
650+
$formLayout->addColumn(12, function (FormLayoutColumn $column) {
651+
$column->withField('html_string')
652+
->withField('html_view')
653+
->withField('html_closure')
654+
->withListField('list', function (FormLayoutColumn $column) {
655+
$column->withField('list_name')
656+
->withField('list_html');
657+
});
658+
});
659+
}
660+
661+
public function find($id): array
662+
{
663+
return $this->setCustomTransformer('html_string', fn ($value) => ['text' => 'example'])
664+
->setCustomTransformer('html_view', fn ($value) => ['text' => 'example'])
665+
->setCustomTransformer('html_closure', fn ($value) => ['text' => 'example'])
666+
->transform([
667+
'name' => 'Albert Einstein',
668+
'list' => [
669+
['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']],
670+
],
671+
]);
672+
}
673+
674+
public function update(mixed $id, array $data) {}
675+
});
676+
677+
$this->get('/sharp/s-list/person/s-form/person/1')
678+
->assertOk()
679+
->assertInertia(fn (Assert $page) => $page
680+
->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>')
681+
->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n")
682+
->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>')
683+
->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>')
684+
);
685+
});

tests/Unit/Form/Fields/Formatters/HtmlFormatterTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@
44
use Code16\Sharp\Form\Fields\SharpFormHtmlField;
55
use Illuminate\Support\Str;
66

7-
it('allows to format value to front', function () {
8-
$value = Str::random();
9-
7+
it('allows to format value to front, passthrough value', function () {
108
expect(
119
(new HtmlFormatter())->toFront(
1210
SharpFormHtmlField::make('html')->setTemplate('<b>{{ $text }}</b>'),
13-
['text' => $value]
11+
['text' => 'example']
1412
)
1513
)
16-
->toEqual("<b>$value</b>");
14+
->toEqual(['text' => 'example']);
1715
});
1816

19-
it('allows to format value with view to front', function () {
20-
$value = Str::random();
21-
17+
it('allows to format value with view to front, passthrough value', function () {
2218
expect(
2319
(new HtmlFormatter())->toFront(
2420
SharpFormHtmlField::make('html')->setTemplate(view('fixtures::test')),
25-
['text' => $value]
21+
['text' => 'example']
2622
)
2723
)
28-
->toContain("<b>$value</b>");
24+
->toEqual(['text' => 'example']);
2925
});
3026

3127
it('allows to format value from front', function () {

tests/Unit/Form/Fields/SharpFormHtmlFieldTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
->toEqual([
1111
'key' => 'html',
1212
'type' => 'html',
13+
'liveRefresh' => false,
1314
]);
1415
});
1516

@@ -21,5 +22,22 @@
2122
->toEqual([
2223
'key' => 'html',
2324
'type' => 'html',
25+
'liveRefresh' => false,
26+
]);
27+
});
28+
29+
it('allows to define with live refresh', function () {
30+
$defaultFormField = SharpFormHtmlField::make('html')
31+
->setLiveRefresh(linkedFields: ['field1', 'field2'])
32+
->setTemplate(function ($data) {
33+
return '<b>test</b>';
34+
});
35+
36+
expect($defaultFormField->toArray())
37+
->toEqual([
38+
'key' => 'html',
39+
'type' => 'html',
40+
'liveRefresh' => true,
41+
'liveRefreshLinkedFields' => ['field1', 'field2'],
2442
]);
2543
});

0 commit comments

Comments
 (0)