|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Columns; |
| 4 | + |
| 5 | +use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException; |
| 6 | +use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; |
| 7 | +use Rappasoft\LaravelLivewireTables\Tests\TestCase; |
| 8 | +use Rappasoft\LaravelLivewireTables\Views\Column; |
| 9 | +use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn; |
| 10 | + |
| 11 | +final class LivewireComponentColumnTest extends TestCase |
| 12 | +{ |
| 13 | + public function test_can_set_the_column_title(): void |
| 14 | + { |
| 15 | + $column = LivewireComponentColumn::make('Name', 'name'); |
| 16 | + |
| 17 | + $this->assertSame('Name', $column->getTitle()); |
| 18 | + } |
| 19 | + |
| 20 | + public function test_can_not_be_a_label_without_component(): void |
| 21 | + { |
| 22 | + $this->expectException(DataTableConfigurationException::class); |
| 23 | + |
| 24 | + $column = LivewireComponentColumn::make('Total Users')->label(fn () => 'My Label')->getContents(Pet::find(1)); |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + public function test_can_not_be_a_label_with_component(): void |
| 29 | + { |
| 30 | + $this->expectException(DataTableConfigurationException::class); |
| 31 | + |
| 32 | + $column = LivewireComponentColumn::make('Total Users')->component('test-component')->label(fn () => 'My Label')->getContents(Pet::find(1)); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public function test_can_add_livewire_component(): void |
| 37 | + { |
| 38 | + $column = LivewireComponentColumn::make('Name', 'name'); |
| 39 | + |
| 40 | + $this->assertFalse($column->hasLivewireComponent()); |
| 41 | + $column->component('test-component'); |
| 42 | + $this->assertTrue($column->hasLivewireComponent()); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_can_get_livewire_component(): void |
| 46 | + { |
| 47 | + $column = LivewireComponentColumn::make('Name', 'name'); |
| 48 | + |
| 49 | + $this->assertFalse($column->hasLivewireComponent()); |
| 50 | + $this->assertNull($column->getLivewireComponent()); |
| 51 | + |
| 52 | + $column->component('test-component'); |
| 53 | + |
| 54 | + $this->assertTrue($column->hasLivewireComponent()); |
| 55 | + $this->assertSame('test-component', $column->getLivewireComponent()); |
| 56 | + } |
| 57 | + |
| 58 | + public function test_can_not_avoid_defining_livewire_component(): void |
| 59 | + { |
| 60 | + $this->expectException(DataTableConfigurationException::class); |
| 61 | + |
| 62 | + $contents = LivewireComponentColumn::make('Name')->getContents(Pet::find(1)); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public function test_attributes_should_return_array(): void |
| 67 | + { |
| 68 | + $this->expectException(DataTableConfigurationException::class); |
| 69 | + |
| 70 | + $column = LivewireComponentColumn::make('Name')->component('test-component')->attributes(fn ($value, $row, Column $column) => 'test'); |
| 71 | + |
| 72 | + $column->getContents(Pet::find(1)); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_can_check_attribute_callback_presence(): void |
| 76 | + { |
| 77 | + $column = LivewireComponentColumn::make('Name', 'name')->component('test-component'); |
| 78 | + $this->assertFalse($column->hasAttributesCallback()); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_can_set_attribute_callback(): void |
| 82 | + { |
| 83 | + $column = LivewireComponentColumn::make('Name', 'name')->component('test-component'); |
| 84 | + $this->assertFalse($column->hasAttributesCallback()); |
| 85 | + |
| 86 | + $column->attributes(function ($row) { |
| 87 | + return [ |
| 88 | + 'class' => '!rounded-lg self-center', |
| 89 | + 'default' => true, |
| 90 | + ]; |
| 91 | + }); |
| 92 | + |
| 93 | + $this->assertTrue($column->hasAttributesCallback()); |
| 94 | + } |
| 95 | +} |
0 commit comments