-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TESTS] Enhance PowerGrid Fields tests (#1521)
* Create OrderTable for test * enhance orders table and improve seeder * improve pg fields test * Add tests for e() helper and script tag removal * Add more casts to Order model
- Loading branch information
1 parent
2836d50
commit 42f8d0d
Showing
4 changed files
with
109 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace PowerComponents\LivewirePowerGrid\Tests\Concerns\Components; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Order; | ||
use PowerComponents\LivewirePowerGrid\{ | ||
Column, | ||
PowerGrid, | ||
PowerGridComponent, | ||
PowerGridFields, | ||
}; | ||
|
||
class OrderTable extends PowerGridComponent | ||
{ | ||
public function datasource(): Builder | ||
{ | ||
return Order::query(); | ||
} | ||
|
||
public function fields(): PowerGridFields | ||
{ | ||
return PowerGrid::fields() | ||
->add('name') | ||
->add('tax') | ||
->add('price') | ||
->add('link', fn (Order $order): string|null => $order->link) | ||
->add('is_active_label', fn (Order $order): string => $order->price ? 'active' : 'inactive') | ||
->add('price_formatted', fn (Order $order): float => $order->price * 100); | ||
} | ||
|
||
public function columns(): array | ||
{ | ||
return [ | ||
Column::make('Name', 'name'), | ||
Column::make('Link', 'link'), | ||
Column::make('Is Active', 'is_active_label'), | ||
Column::make('Price', 'price_formatted', 'price'), | ||
Column::make('Tax', 'tax'), | ||
]; | ||
} | ||
|
||
public function bootstrap() | ||
{ | ||
config(['livewire-powergrid.theme' => 'bootstrap']); | ||
} | ||
|
||
public function tailwind() | ||
{ | ||
config(['livewire-powergrid.theme' => 'tailwind']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
<?php | ||
use Illuminate\Database\Eloquent\Builder; | ||
use PowerComponents\LivewirePowerGrid\PowerGrid; | ||
|
||
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\OrderTable; | ||
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Order; | ||
|
||
use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire; | ||
|
||
use PowerComponents\LivewirePowerGrid\{Column, PowerGridComponent, PowerGridFields}; | ||
|
||
$component = new class () extends PowerGridComponent { | ||
public function datasource(): Builder | ||
{ | ||
return Order::query(); | ||
} | ||
|
||
public function fields(): PowerGridFields | ||
{ | ||
return PowerGrid::fields() | ||
->add('name') | ||
->add('tax') | ||
->add('price') | ||
->add('price_formatted', fn (Order $model) => $model->price * 100); | ||
} | ||
|
||
public function columns(): array | ||
{ | ||
return [ | ||
Column::make('Name', 'name'), | ||
Column::make('Price', 'price_formatted', 'price'), | ||
Column::make('Tax', 'tax'), | ||
]; | ||
} | ||
}; | ||
|
||
it('can add fields', function (string $name, string|float $price, string|float $tax) use ($component) { | ||
$component = livewire($component::class); | ||
|
||
$component->assertSee($name) | ||
->assertSee($price) | ||
->assertSee($tax); | ||
})->with([ | ||
['Order 1', 1000, 127.30], | ||
['Order 2', 2000, 259.50], | ||
['Order 3', '', ''], | ||
it('removes <script> tag from custom field', function (string $component, object $params) { | ||
Order::first()->update(['link' => 'hello there! <script>alert(document.cookie)</script>']); | ||
|
||
livewire($component) | ||
->call($params->theme) | ||
->assertDontSeeHtml('<script>alert') | ||
->assertDontSeeHtml('<script>alert') | ||
->assertSeeHtml('hello there!'); | ||
})->with('order_table'); | ||
|
||
it('runs e() helper in PG fields', function (string $component, object $params) { | ||
Order::first()->update(['name' => '<img src="invalid_url.png" onerror=alert(document.cookie)>']); | ||
|
||
livewire($component) | ||
->call($params->theme) | ||
->assertDontSeeHtml('<img src="invalid_url.png"') | ||
->assertSeeHtml('<div><img src="invalid_url.png" onerror=alert(document.cookie)>'); | ||
})->with('order_table'); | ||
|
||
it('does not run e() in custom PG fields', function (string $component, object $params) { | ||
$link = '<a href="https://google.com" target="_blank">Link from closure</a>'; | ||
|
||
Order::first()->update(['link' => $link]); | ||
|
||
livewire($component) | ||
->call($params->theme) | ||
->assertDontSeeHtml(e($link)) | ||
->assertSeeHtml($link); | ||
})->with('order_table'); | ||
|
||
it('can fields with casting and custom fields', function (string $component, object $params) { | ||
livewire($component) | ||
->call($params->theme) | ||
->assertSeeHtmlInOrder(['Order 1', 'Order 2', 'Order 3']) | ||
->assertSeeHtmlInOrder(['active', 'active', 'inactive']) | ||
->assertSeeHtmlInOrder(['1000', '2000', '0']) | ||
->assertSeeHtmlInOrder(['127.3', '259.5', '']); | ||
})->with('order_table'); | ||
|
||
dataset('order_table', [ | ||
'tailwind' => [OrderTable::class, (object) ['theme' => 'tailwind']], | ||
'bootstrap' => [OrderTable::class, (object) ['theme' => 'bootstrap']], | ||
]); |