2
2
3
3
namespace Yajra \DataTables \Html \Tests ;
4
4
5
- use Illuminate \Support \Facades \View ;
6
5
use InvalidArgumentException ;
6
+ use Livewire \Exceptions \ComponentNotFoundException ;
7
7
use PHPUnit \Framework \Attributes \Test ;
8
8
use Yajra \DataTables \Html \Builder ;
9
9
use Yajra \DataTables \Html \Enums \LayoutPosition ;
10
10
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 ;
11
14
12
15
class LayoutTest extends TestCase
13
16
{
@@ -185,8 +188,6 @@ public function it_can_accept_js_selector_for_layout_content(): void
185
188
#[Test]
186
189
public function it_can_accept_view_instance_or_string_for_layout_content (): void
187
190
{
188
- View::addLocation (__DIR__ .'/TestBlade ' );
189
-
190
191
$ builder = resolve (Builder::class);
191
192
192
193
$ view = view ('test-view ' );
@@ -196,25 +197,49 @@ public function it_can_accept_view_instance_or_string_for_layout_content(): void
196
197
view: $ view ,
197
198
layoutPosition: LayoutPosition::TopStart,
198
199
order: 1
199
- )->addView (
200
+ )
201
+ ->addView (
200
202
view: 'test-view ' ,
201
203
layoutPosition: LayoutPosition::BottomEnd,
202
204
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
+ );
204
217
205
218
$ this ->assertArrayHasKey ('layout ' , $ builder ->getAttributes ());
219
+
206
220
$ this ->assertArrayHasKey ('top1Start ' , $ builder ->getAttributes ()['layout ' ]);
207
221
$ this ->assertEquals (
208
222
'function() { return ' .json_encode ($ view ->render ()).'; } ' ,
209
223
$ builder ->getAttributes ()['layout ' ]['top1Start ' ]
210
224
);
211
225
212
- $ this ->assertArrayHasKey ('layout ' , $ builder ->getAttributes ());
213
226
$ this ->assertArrayHasKey ('bottom2End ' , $ builder ->getAttributes ()['layout ' ]);
214
227
$ this ->assertEquals (
215
228
'function() { return ' .json_encode ($ view ->render ()).'; } ' ,
216
229
$ builder ->getAttributes ()['layout ' ]['bottom2End ' ]
217
230
);
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
+ );
218
243
}
219
244
220
245
#[Test]
@@ -234,4 +259,39 @@ public function it_throws_an_exception_if_the_view_does_not_exist_when_adding_vi
234
259
layoutPosition: LayoutPosition::Bottom,
235
260
));
236
261
}
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
+ }
237
297
}
0 commit comments