22
33namespace Rappasoft \LaravelLivewireTables \Tests \Unit \Views \Columns ;
44
5+ use Illuminate \Support \Facades \Blade ;
56use Rappasoft \LaravelLivewireTables \Exceptions \DataTableConfigurationException ;
7+ use Rappasoft \LaravelLivewireTables \Tests \Http \Components \TestComponent ;
68use Rappasoft \LaravelLivewireTables \Tests \Models \Pet ;
79use Rappasoft \LaravelLivewireTables \Tests \TestCase ;
810use Rappasoft \LaravelLivewireTables \Views \Column ;
911use Rappasoft \LaravelLivewireTables \Views \Columns \ComponentColumn ;
1012
1113final class ComponentColumnTest extends TestCase
1214{
15+ protected function setUp (): void
16+ {
17+ parent ::setUp ();
18+ }
19+
1320 public function test_can_set_the_column_title (): void
1421 {
1522 $ column = ComponentColumn::make ('Name ' , 'name ' );
@@ -34,4 +41,57 @@ public function test_can_not_be_both_collapsible_on_mobile_and_on_tablet(): void
3441 $ column ->getContents ($ row );
3542
3643 }
44+
45+ public function test_can_set_custom_slot (): void
46+ {
47+ $ column = ComponentColumn::make ('Age 2 ' , 'age ' )
48+ ->attributes (fn ($ value , $ row , Column $ column ) => [
49+ 'age ' => $ row ->age ,
50+ ])
51+ ->slot (fn ($ value , $ row , Column $ column ) => [
52+ ($ row ->age < 2 ) => 'test1 ' ,
53+ ($ row ->age > 2 ) => 'test2 ' ,
54+ ]);
55+ $ this ->assertTrue ($ column ->hasSlotCallback ());
56+ }
57+
58+ public function test_can_get_custom_slot (): void
59+ {
60+
61+ $ column = ComponentColumn::make ('Age 2 ' , 'age ' )
62+ ->attributes (fn ($ value , $ row , Column $ column ) => [
63+ 'age ' => $ row ->age ,
64+ ])
65+ ->slot (fn ($ value , $ row , Column $ column ) => (($ row ->age < 10 ) ? 'youngslot ' : 'oldslot ' ))
66+ ->component ('livewire-tables-test::test ' );
67+
68+ $ pet1 = Pet::where ('age ' , '> ' , 11 )->first ();
69+ $ pet1_contents = $ column ->getContents ($ pet1 );
70+ $ this ->assertSame ('oldslot ' , $ pet1_contents ->getData ()['slot ' ]->__toString ());
71+
72+ $ pet2 = Pet::where ('age ' , '< ' , 5 )->first ();
73+ $ pet2_contents = $ column ->getContents ($ pet2 );
74+ $ this ->assertSame ('youngslot ' , $ pet2_contents ->getData ()['slot ' ]->__toString ());
75+
76+ }
77+
78+ public function test_can_get_attributes (): void
79+ {
80+
81+ $ column = ComponentColumn::make ('Age 2 ' , 'age ' )
82+ ->attributes (fn ($ value , $ row , Column $ column ) => [
83+ 'age ' => $ row ->age ,
84+ ])
85+ ->slot (fn ($ value , $ row , Column $ column ) => (($ row ->age < 10 ) ? 'youngslot ' : 'oldslot ' ))
86+ ->component ('livewire-tables-test::test ' );
87+
88+ $ pet1 = Pet::where ('age ' , '> ' , 11 )->first ();
89+ $ pet1_contents = $ column ->getContents ($ pet1 );
90+ $ this ->assertSame (22 , $ pet1_contents ->getData ()['attributes ' ]['age ' ]);
91+
92+ $ pet2 = Pet::where ('age ' , '< ' , 5 )->first ();
93+ $ pet2_contents = $ column ->getContents ($ pet2 );
94+ $ this ->assertSame (2 , $ pet2_contents ->getData ()['attributes ' ]['age ' ]);
95+
96+ }
3797}
0 commit comments