Skip to content

Commit

Permalink
Rules Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dansysanalyst committed May 19, 2024
1 parent c933120 commit 23ac059
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/Components/Rules/RuleCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setAttribute(string $attribute = null, string $value = null): se
}

/**
* Hides the button.
* Hides the input.
*/
public function hide(): self
{
Expand All @@ -30,7 +30,7 @@ public function hide(): self
}

/**
* Disables the button.
* Disables the input.
*/
public function disable(): self
{
Expand Down
80 changes: 80 additions & 0 deletions tests/Concerns/Components/ComponentsForTestRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Rule;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishesTable;
use PowerComponents\LivewirePowerGrid\{Button, Column, Detail};

$baseRuleComponent = new class () extends DishesTable {
public function setUp(): array
{
$this->showCheckBox();

return [
...parent::setUp(),

Detail::make()
->view('components.detail')
->showCollapseIcon()
->params(['name' => 'Luan']),
];
}

public function columns(): array
{
return [
Column::make('id', 'id'),

Column::add()
->title('In Stock')
->field('in_stock')
->toggleable(hasPermission: true, trueLabel: 'it-is-in-stock', falseLabel: 'it-is-not-in-stock')
->sortable(),

Column::make('NEVER HAS TOGGLEABLE', 'active')
->toggleable(hasPermission: false, trueLabel: 'it-is-active', falseLabel: 'it-is-not-active')
->sortable(),

Column::make('Name', 'name')
->searchable()
->editOnClick(hasPermission: true, dataField: 'name')
->sortable(),

Column::make('NEVER HAS EDIT ON CLICK', 'serving_at')
->searchable()
->sortable(),

Column::action('Action'),
];
}

public function actions($row): array
{
return [
Button::make('dispatch')
->slot('dispatch: ' . $row->id)
->dispatch('executeDispatch', ['id' => $row->id]),
];
}

public function actionRules($row): array
{
return [
Rule::rows()
->when(fn ($dish) => $dish->id == 1)
->hideToggleDetail(),

Rule::toggleable('active')
->when(fn ($dish) => $dish->id == 1)
->show(),

Rule::editOnClick('serving_at')
->when(fn ($dish) => $dish->id == 1)
->enable(),

Rule::rows()
->when(fn ($dish) => $dish->id == 5)
->hideToggleable()
->disableEditOnClick(),
];
}
};
1 change: 1 addition & 0 deletions tests/Concerns/Components/DishesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function fields(): PowerGridFields
->add('name')
->add('storage_room')
->add('chef_name')
->add('active')
->add('serving_at')
->add('calories')
->add('calories', function (Dish $dish) {
Expand Down
50 changes: 0 additions & 50 deletions tests/Feature/ActionRules/DisableTest.php

This file was deleted.

23 changes: 23 additions & 0 deletions tests/Feature/ActionRules/EditOnClickRulesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

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

it('properly enabled EditOnClick with Rule::toggleable for dish #1 in a column that has it disabled', function (string $baseRuleComponent, object $params) {
livewire($baseRuleComponent, [
'join' => $params->join,
])->call($params->theme)
->set('setUp.footer.perPage', 5)
->assertSeeHtml("pgEditable(JSON.parse('{\u0022theme\u0022:\u0022{$params->themeName}\u0022,\u0022tableName\u0022:\u0022default\u0022,\u0022id\u0022:1,\u0022dataField\u0022:\u0022serving_at")
->assertDontSeeHtml("pgEditable(JSON.parse('{\u0022theme\u0022:\u0022{$params->themeName}\u0022,\u0022tableName\u0022:\u0022default\u0022,\u0022id\u0022:2,\u0022dataField\u0022:\u0022serving_at")
->assertDontSeeHtml("pgEditable(JSON.parse('{\u0022theme\u0022:\u0022{$params->themeName}\u0022,\u0022tableName\u0022:\u0022default\u0022,\u0022id\u0022:3,\u0022dataField\u0022:\u0022serving_at");
})
->with([
'tailwind using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => false, 'themeName' => 'tailwind']],
'bootstrap using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => false, 'themeName' => 'bootstrap5']],
'tailwind join using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => true, 'themeName' => 'tailwind']],
'bootstrap join using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => true, 'themeName' => 'bootstrap5']],
])
->only()
->group('actionRules');
23 changes: 23 additions & 0 deletions tests/Feature/ActionRules/HideToggleDetailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

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

it('properly hides the toggle detail button', function (string $baseRuleComponent, object $params) {
livewire($baseRuleComponent, [
'join' => $params->join,
])->call($params->theme)
->set('setUp.footer.perPage', 5)
->assertDontSeeHtml('$wire.toggleDetail(\'1\')')
->assertSeeHtml('$wire.toggleDetail(\'2\')')
->assertSeeHtml('$wire.toggleDetail(\'3\')');
})
->with([
'tailwind using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => true],
],
])
->group('actionRules');
40 changes: 40 additions & 0 deletions tests/Feature/ActionRules/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public function actionRules($row): array
}
};

$alternatingComponent = new class () extends DishTableBase {
public function actionRules($row): array
{
return [
Rule::rows()
->alternating()
->setAttribute('class', '!bg-somecolor-100'),
];
}
};

dataset('actionRules:loop', [
'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]],
'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]],
Expand All @@ -41,3 +52,32 @@ public function actionRules($row): array
]);
})->skip()->with('actionRules:loop')
->group('actionRules');

it('properly applies alternating() Row Rule', function (string $alternatingComponent, object $params) {
livewire($alternatingComponent, [
'join' => $params->join,
])->call($params->theme)
->set('setUp.footer.perPage', 5)
->assertSeeHtmlInOrder([
'
style=""
class="',
'
style=""
class="!bg-somecolor-100',
'
style=""
class="',
'
style=""
class="!bg-somecolor-100',
]);
})
->with([
'tailwind using when' => [$alternatingComponent::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap using when' => [$alternatingComponent::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join using when' => [$alternatingComponent::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join using when' => [$alternatingComponent::class, (object) ['theme' => 'bootstrap', 'join' => true],
],
])
->group('actionRules');
46 changes: 46 additions & 0 deletions tests/Feature/ActionRules/RuleToggleableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

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

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

it('properly hides toggleable with Row->hideToggleable() for dish #5', function (string $baseRuleComponent, object $params) {
livewire($baseRuleComponent, [
'join' => $params->join,
])->call($params->theme)
->set('setUp.footer.perPage', 5)
->assertDontSeeHtml("pgToggleable(JSON.parse('{\u0022id\u0022:5,\u0022isHidden\u0022:false,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022in_stock\u0022,")
->assertSeeHtmlInOrder(
[
"pgToggleable(JSON.parse('{\u0022id\u0022:3,\u0022isHidden\u0022:false,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022in_stock\u0022,",

"pgToggleable(JSON.parse('{\u0022id\u0022:4,\u0022isHidden\u0022:false,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022in_stock\u0022,",
]
);
})
->with([
'tailwind using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => true],
],
])
->group('actionRules');

it('properly shows toggleable with Rule::toggleable for dish #1', function (string $baseRuleComponent, object $params) {
livewire($baseRuleComponent, [
'join' => $params->join,
])->call($params->theme)
->set('setUp.footer.perPage', 5)
->assertSeeHtml("pgToggleable(JSON.parse('{\u0022id\u0022:1,\u0022isHidden\u0022:false,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022active\u0022,")
->assertSeeHtml("pgToggleable(JSON.parse('{\u0022id\u0022:2,\u0022isHidden\u0022:true,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022active\u0022,")
->assertSeeHtml("pgToggleable(JSON.parse('{\u0022id\u0022:3,\u0022isHidden\u0022:true,\u0022tableName\u0022:\u0022default\u0022,\u0022field\u0022:\u0022active\u0022,");
})
->with([
'tailwind using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => false]],
'bootstrap using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => false]],
'tailwind join using when' => [$baseRuleComponent::class, (object) ['theme' => 'tailwind', 'join' => true]],
'bootstrap join using when' => [$baseRuleComponent::class, (object) ['theme' => 'bootstrap', 'join' => true],
],
])
->group('actionRules');

0 comments on commit 23ac059

Please sign in to comment.