Skip to content

Commit

Permalink
[TESTS] Enhance PowerGrid Fields tests (#1521)
Browse files Browse the repository at this point in the history
* 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
dansysanalyst authored Apr 30, 2024
1 parent 2836d50 commit 42f8d0d
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 53 deletions.
52 changes: 52 additions & 0 deletions tests/Concerns/Components/OrderTable.php
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']);
}
}
8 changes: 5 additions & 3 deletions tests/Concerns/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class Order extends Model
protected $table = 'orders';

protected $casts = [
'name' => 'string',
'price' => 'decimal:2',
'tax' => 'float',
'name' => 'string',
'link' => 'string',
'price' => 'decimal:2',
'tax' => 'float',
'is_active' => 'boolean',
];
}
19 changes: 8 additions & 11 deletions tests/Concerns/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\{DB, Schema};
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Chef;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\{Category, Chef};

class TestDatabase
{
Expand Down Expand Up @@ -85,8 +85,10 @@ public static function migrate(): void
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('link')->nullable();
$table->double('tax')->nullable();
$table->decimal('price')->nullable();
$table->boolean('is_active')->default(false);
$table->softDeletes();
$table->timestamps();
});
Expand Down Expand Up @@ -127,9 +129,9 @@ public static function seed(array $dishes = []): void
]);

DB::table('orders')->insert([
['name' => 'Order 1', 'price' => 10.00, 'tax' => 127.30],
['name' => 'Order 2', 'price' => 20.00, 'tax' => 259.50],
['name' => 'Order 3', 'price' => null, 'tax' => null],
['name' => 'Order 1', 'price' => 10.00, 'tax' => 127.30, 'is_active' => true],
['name' => 'Order 2', 'price' => 20.00, 'tax' => 259.50, 'is_active' => true],
['name' => 'Order 3', 'price' => null, 'tax' => null, 'is_active' => false],
]);

if (empty($dishes)) {
Expand All @@ -138,15 +140,10 @@ public static function seed(array $dishes = []): void

DB::table('dishes')->insert($dishes);

$chefCategories = [
'Luan' => [1, 3, 4],
'Dan' => [2, 5],
'Vitor' => [5, 6],
'Claudio' => [1, 6, 7],
];
$chefCategories = Category::all();

Chef::query()->get()->each(function (Chef $chef) use ($chefCategories) {
$chef->categories()->attach($chefCategories[$chef->name]);
$chef->categories()->attach($chefCategories->shuffle()->take(rand(1, 4)));
});
}

Expand Down
83 changes: 44 additions & 39 deletions tests/Feature/PowerGridFieldsTest.php
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('&lt;script&gt;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>&lt;img src=&quot;invalid_url.png&quot; onerror=alert(document.cookie)&gt;');
})->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']],
]);

0 comments on commit 42f8d0d

Please sign in to comment.