Skip to content

Commit c9a01e2

Browse files
committed
tests: adding inline view and livewire component tests
1 parent 4cf4905 commit c9a01e2

File tree

6 files changed

+123
-6
lines changed

6 files changed

+123
-6
lines changed

tests/LayoutTest.php

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Yajra\DataTables\Html\Tests;
44

5-
use Illuminate\Support\Facades\View;
65
use InvalidArgumentException;
6+
use Livewire\Exceptions\ComponentNotFoundException;
77
use PHPUnit\Framework\Attributes\Test;
88
use Yajra\DataTables\Html\Builder;
99
use Yajra\DataTables\Html\Enums\LayoutPosition;
1010
use Yajra\DataTables\Html\Layout;
11+
use Yajra\DataTables\Html\Tests\TestComponents\TestInlineView;
12+
use Yajra\DataTables\Html\Tests\TestComponents\TestLivewire;
13+
use Yajra\DataTables\Html\Tests\TestComponents\TestView;
1114

1215
class LayoutTest extends TestCase
1316
{
@@ -185,8 +188,6 @@ public function it_can_accept_js_selector_for_layout_content(): void
185188
#[Test]
186189
public function it_can_accept_view_instance_or_string_for_layout_content(): void
187190
{
188-
View::addLocation(__DIR__.'/TestBlade');
189-
190191
$builder = resolve(Builder::class);
191192

192193
$view = view('test-view');
@@ -196,25 +197,49 @@ public function it_can_accept_view_instance_or_string_for_layout_content(): void
196197
view: $view,
197198
layoutPosition: LayoutPosition::TopStart,
198199
order: 1
199-
)->addView(
200+
)
201+
->addView(
200202
view: 'test-view',
201203
layoutPosition: LayoutPosition::BottomEnd,
202204
order: 2
203-
));
205+
)
206+
->addView(
207+
view: (new TestView())->render(),
208+
layoutPosition: LayoutPosition::Top,
209+
order: 3
210+
)
211+
->addView(
212+
view: (new TestInlineView())->render(),
213+
layoutPosition: LayoutPosition::Bottom,
214+
order: 4
215+
)
216+
);
204217

205218
$this->assertArrayHasKey('layout', $builder->getAttributes());
219+
206220
$this->assertArrayHasKey('top1Start', $builder->getAttributes()['layout']);
207221
$this->assertEquals(
208222
'function() { return '.json_encode($view->render()).'; }',
209223
$builder->getAttributes()['layout']['top1Start']
210224
);
211225

212-
$this->assertArrayHasKey('layout', $builder->getAttributes());
213226
$this->assertArrayHasKey('bottom2End', $builder->getAttributes()['layout']);
214227
$this->assertEquals(
215228
'function() { return '.json_encode($view->render()).'; }',
216229
$builder->getAttributes()['layout']['bottom2End']
217230
);
231+
232+
$this->assertArrayHasKey('top3', $builder->getAttributes()['layout']);
233+
$this->assertEquals(
234+
'function() { return '.json_encode($view->render()).'; }',
235+
$builder->getAttributes()['layout']['top3']
236+
);
237+
238+
$this->assertArrayHasKey('bottom4', $builder->getAttributes()['layout']);
239+
$this->assertEquals(
240+
'function() { return '.json_encode('<p>Test Inline View</p>').'; }',
241+
$builder->getAttributes()['layout']['bottom4']
242+
);
218243
}
219244

220245
#[Test]
@@ -234,4 +259,39 @@ public function it_throws_an_exception_if_the_view_does_not_exist_when_adding_vi
234259
layoutPosition: LayoutPosition::Bottom,
235260
));
236261
}
262+
263+
#[Test]
264+
public function it_can_accept_livewire_component_as_layout_content(): void
265+
{
266+
$builder = resolve(Builder::class);
267+
$builder->layout(fn (Layout $layout) => $layout
268+
->addLivewire(TestLivewire::class, LayoutPosition::TopStart, 1)
269+
->addLivewire(TestLivewire::class, LayoutPosition::BottomEnd, 2));
270+
271+
$this->assertArrayHasKey('layout', $builder->getAttributes());
272+
$this->assertArrayHasKey('top1Start', $builder->getAttributes()['layout']);
273+
$this->assertStringContainsString(
274+
'test livewire',
275+
$builder->getAttributes()['layout']['top1Start']
276+
);
277+
278+
$this->assertArrayHasKey('layout', $builder->getAttributes());
279+
$this->assertArrayHasKey('bottom2End', $builder->getAttributes()['layout']);
280+
$this->assertStringContainsString(
281+
'test livewire',
282+
$builder->getAttributes()['layout']['bottom2End']
283+
);
284+
}
285+
286+
#[Test]
287+
public function it_throws_an_exception_if_the_livewire_component_does_not_exist_when_adding_livewire_component(): void
288+
{
289+
$this->expectException(ComponentNotFoundException::class);
290+
$this->expectExceptionMessage('Unable to find component: [Yajra\DataTables\Html\Tests\TestComponents\TestView]');
291+
292+
$builder = resolve(Builder::class);
293+
$builder->layout(fn (Layout $layout) => $layout
294+
->addLivewire(TestView::class, LayoutPosition::Top)
295+
->addLivewire(TestView::class, LayoutPosition::Bottom));
296+
}
237297
}

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Yajra\DataTables\Html\Tests;
44

55
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\Facades\View;
8+
use Livewire\LivewireServiceProvider;
69
use Orchestra\Testbench\TestCase as BaseTestCase;
710
use Yajra\DataTables\DataTablesServiceProvider;
811
use Yajra\DataTables\Html\Builder;
@@ -16,8 +19,11 @@ protected function setUp(): void
1619
{
1720
parent::setUp();
1821

22+
config()->set('app.key', 'base64:6pASQ5U2UYo+w6noM9hwOPeHJ5vGP+BNruyMtRA8FWY=');
23+
1924
$this->migrateDatabase();
2025
$this->seedDatabase();
26+
$this->setupViewAndBladeDirectory();
2127
}
2228

2329
protected function migrateDatabase(): void
@@ -104,6 +110,7 @@ protected function getEnvironmentSetUp($app): void
104110
protected function getPackageProviders($app): array
105111
{
106112
return [
113+
LivewireServiceProvider::class,
107114
DataTablesServiceProvider::class,
108115
HtmlServiceProvider::class,
109116
];
@@ -113,4 +120,10 @@ protected function getHtmlBuilder(): Builder
113120
{
114121
return app(Builder::class);
115122
}
123+
124+
protected function setupViewAndBladeDirectory(): void
125+
{
126+
View::addLocation(__DIR__.'/TestComponents');
127+
Blade::componentNamespace('Yajra\\DataTables\\Html\\Tests\\TestComponents', 'test');
128+
}
116129
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\TestComponents;
4+
5+
use Illuminate\View\Component;
6+
7+
class TestInlineView extends Component
8+
{
9+
public function render(): string
10+
{
11+
return <<<'blade'
12+
<p>Test Inline View</p>
13+
blade;
14+
}
15+
}

tests/TestComponents/TestLivewire.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\TestComponents;
4+
5+
use Livewire\Component;
6+
7+
class TestLivewire extends Component
8+
{
9+
public function render(): string
10+
{
11+
return <<<'blade'
12+
<div>test livewire</div>
13+
blade;
14+
}
15+
}

tests/TestComponents/TestView.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests\TestComponents;
4+
5+
use Illuminate\Contracts\Support\Renderable;
6+
use Illuminate\View\Component;
7+
8+
class TestView extends Component
9+
{
10+
public function render(): Renderable
11+
{
12+
return view('test-view');
13+
}
14+
}

0 commit comments

Comments
 (0)