22
33namespace Yajra \DataTables \Html \Tests ;
44
5- use Illuminate \Support \Facades \View ;
65use InvalidArgumentException ;
6+ use Livewire \Exceptions \ComponentNotFoundException ;
77use PHPUnit \Framework \Attributes \Test ;
88use Yajra \DataTables \Html \Builder ;
99use Yajra \DataTables \Html \Enums \LayoutPosition ;
1010use 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
1215class 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}
0 commit comments