Skip to content

Commit

Permalink
Split and refactor Multiple Filters Test
Browse files Browse the repository at this point in the history
  • Loading branch information
dansysanalyst committed May 9, 2024
1 parent 7ac36f0 commit 05556c9
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 52 deletions.
45 changes: 45 additions & 0 deletions tests/Concerns/Components/ComponentsForFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Filter;

use PowerComponents\LivewirePowerGrid\Tests\{Concerns\Components\DishesQueryBuilderTable,
Concerns\Components\DishesTable,
Concerns\Components\DishesTableWithJoin};

$component = new class () extends DishesTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentQueryBuilder = new class () extends DishesQueryBuilderTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentJoin = new class () extends DishesTableWithJoin {
public function filters(): array
{
return [
Filter::number('price_BRL') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('dish_name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};
10 changes: 10 additions & 0 deletions tests/Datasets/FilterComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

require(__DIR__ . '/../Concerns/Components/ComponentsForFilterTest.php');

dataset('filterComponent', [
'tailwind -> id' => [$component::class, (object) ['theme' => 'tailwind', 'field' => 'name']],
'bootstrap -> id' => [$component::class, (object) ['theme' => 'bootstrap', 'field' => 'name']],
'tailwind -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'tailwind', 'field' => 'dishes.name']],
'bootstrap -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'bootstrap', 'field' => 'dishes.name']],
]);
65 changes: 65 additions & 0 deletions tests/Feature/Filters/FilterInputTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

require(__DIR__ . '/../../Concerns/Components/ComponentsForFilterTest.php');

it('properly filters by inputText', function (string $component, object $params) {
$component = livewire($component)
->call($params->theme);

/** @var PowerGridComponent $component */
expect($component->filters)
->toMatchArray([]);

$component->set('filters', filterInputText('ba', 'contains', $params->field));

if (str_contains($params->field, '.')) {
$data = Str::of($params->field)->explode('.');
$table = $data->get(0);
$field = $data->get(1);

expect($component->filters)
->toMatchArray([
'input_text' => [
$table => [
$field => 'ba',
],
],
'input_text_options' => [
$table => [
$field => 'contains',
],
],
]);
} else {
expect($component->filters)
->toMatchArray([
'input_text' => [
$params->field => 'ba',
],
'input_text_options' => [
$params->field => 'contains',
],
]);
}

$component->assertSee('Barco-Sushi da Sueli')
->assertSeeHtml('dish_name_xyz_placeholder');

$filters = array_merge($component->filters, filterNumber('price', min: '1\'500.20', max: '3\'000.00'));

$component->set('filters', $filters)
->assertSeeHtml('placeholder="min_xyz_placeholder"')
->assertSeeHtml('placeholder="max_xyz_placeholder"')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Barco-Sushi da Sueli')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('борщ');

expect($component->filters)->toBe($filters);
})->group('filters')
->with('filterComponent');
42 changes: 42 additions & 0 deletions tests/Feature/Filters/FilterNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

require(__DIR__ . '/../../Concerns/Components/ComponentsForFilterTest.php');

it('properly filters by filter Number', function (string $component, object $params) {
$component = livewire($component)
->call($params->theme);

$filters = array_merge($component->filters, filterNumber('price', min: '1\'500.20', max: '3\'000.00'));

$component->set('filters', $filters)
->assertSeeHtml('placeholder="min_xyz_placeholder"')
->assertSeeHtml('placeholder="max_xyz_placeholder"')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Barco-Sushi da Sueli')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('борщ');

expect($component->filters)->toBe($filters);

// Use wrong separators
$filters = array_merge($component->filters, filterNumber('price', min: '1@500#20', max: '3@000#00'));

$component->set('filters', $filters)
->assertSee('No records found');
})->group('filters')
->with('filterComponent');

it('properly filters by filter Number with wrong separators', function (string $component, object $params) {
$component = livewire($component)
->call($params->theme);

// Use wrong separators
$filters = array_merge($component->filters, filterNumber('price', min: '1@500#20', max: '3@000#00'));

$component->set('filters', $filters)
->assertSee('No records found');
})->skipOnPostgreSQL()
->group('filters')
->with('filterComponent');
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
<?php

use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;

use function PowerComponents\LivewirePowerGrid\Tests\Plugins\livewire;

use PowerComponents\LivewirePowerGrid\Tests\{Concerns\Components\DishesQueryBuilderTable,
Concerns\Components\DishesTable,
Concerns\Components\DishesTableWithJoin};

$component = new class () extends DishesTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentQueryBuilder = new class () extends DishesQueryBuilderTable {
public function filters(): array
{
return [
Filter::number('price_BRL')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::number('price') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};

$componentJoin = new class () extends DishesTableWithJoin {
public function filters(): array
{
return [
Filter::number('price_BRL') ->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::inputText('dish_name')->placeholder('dish_name_xyz_placeholder')->operators(),
Filter::number('price')->placeholder('min_xyz_placeholder', 'max_xyz_placeholder')->thousands("'")->decimal(','),
Filter::boolean('in_stock'),
];
}
};
require(__DIR__ . '/../../Concerns/Components/ComponentsForFilterTest.php');

it('properly filters by inputText, number, boolean filter and clearAll', function (string $component, object $params) {
$component = livewire($component)
Expand Down Expand Up @@ -103,9 +62,6 @@ public function filters(): array

expect($component->filters)->toBe($filters);

// Use wrong separators
$filters = array_merge($component->filters, filterNumber('price', min: '1@500#20', max: '3@000#00'));

$component->set('filters', $filters)
->assertSee('No records found');

Expand All @@ -130,9 +86,4 @@ public function filters(): array
expect($component->filters)
->toMatchArray([]);
})->group('filters')
->with([
'tailwind -> id' => [$component::class, (object) ['theme' => 'tailwind', 'field' => 'name']],
'bootstrap -> id' => [$component::class, (object) ['theme' => 'bootstrap', 'field' => 'name']],
'tailwind -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'tailwind', 'field' => 'dishes.name']],
'bootstrap -> dishes.id' => [$componentJoin::class, (object) ['theme' => 'bootstrap', 'field' => 'dishes.name']],
]);
->with('filterComponent');
19 changes: 18 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ function requiresMySQL()

return test();
}
function skipOnMySQL()
{
if (DB::getDriverName() === 'mysql') {
test()->markTestSkipped('This test does not run on MySQL');
}

return test();
}

function requiresSQLite()
{
Expand All @@ -51,7 +59,7 @@ function requiresSQLite()
function skipOnSQLite()
{
if (DB::getDriverName() === 'sqlite') {
test()->markTestSkipped('This test requires MYSQL/PGSQL database');
test()->markTestSkipped('This test does not run on SQLite');
}

return test();
Expand All @@ -66,6 +74,15 @@ function requiresPostgreSQL()
return test();
}

function skipOnPostgreSQL()
{
if (DB::getDriverName() === 'pgsql') {
test()->markTestSkipped('This test does not run on PostgreSQL');
}

return test();
}

function requiresOpenSpout()
{
$isInstalled = \Composer\InstalledVersions::isInstalled('openspout/openspout');
Expand Down

0 comments on commit 05556c9

Please sign in to comment.