From 23ac0596dbd7c5a0855842b4dd814af11b53d26d Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 19 May 2024 17:42:21 +0200 Subject: [PATCH] Rules Tests --- src/Components/Rules/RuleCheckbox.php | 4 +- .../Components/ComponentsForTestRule.php | 80 +++++++++++++++++++ tests/Concerns/Components/DishesTable.php | 1 + tests/Feature/ActionRules/DisableTest.php | 50 ------------ .../ActionRules/EditOnClickRulesTest.php | 23 ++++++ .../ActionRules/HideToggleDetailTest.php | 23 ++++++ tests/Feature/ActionRules/LoopTest.php | 40 ++++++++++ .../ActionRules/RuleToggleableTest.php | 46 +++++++++++ 8 files changed, 215 insertions(+), 52 deletions(-) create mode 100644 tests/Concerns/Components/ComponentsForTestRule.php delete mode 100644 tests/Feature/ActionRules/DisableTest.php create mode 100644 tests/Feature/ActionRules/EditOnClickRulesTest.php create mode 100644 tests/Feature/ActionRules/HideToggleDetailTest.php create mode 100644 tests/Feature/ActionRules/RuleToggleableTest.php diff --git a/src/Components/Rules/RuleCheckbox.php b/src/Components/Rules/RuleCheckbox.php index 498d51262..66a482ca3 100644 --- a/src/Components/Rules/RuleCheckbox.php +++ b/src/Components/Rules/RuleCheckbox.php @@ -20,7 +20,7 @@ public function setAttribute(string $attribute = null, string $value = null): se } /** - * Hides the button. + * Hides the input. */ public function hide(): self { @@ -30,7 +30,7 @@ public function hide(): self } /** - * Disables the button. + * Disables the input. */ public function disable(): self { diff --git a/tests/Concerns/Components/ComponentsForTestRule.php b/tests/Concerns/Components/ComponentsForTestRule.php new file mode 100644 index 000000000..4420e65b6 --- /dev/null +++ b/tests/Concerns/Components/ComponentsForTestRule.php @@ -0,0 +1,80 @@ +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(), + ]; + } +}; diff --git a/tests/Concerns/Components/DishesTable.php b/tests/Concerns/Components/DishesTable.php index 30cedaa21..76acd462a 100644 --- a/tests/Concerns/Components/DishesTable.php +++ b/tests/Concerns/Components/DishesTable.php @@ -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) { diff --git a/tests/Feature/ActionRules/DisableTest.php b/tests/Feature/ActionRules/DisableTest.php deleted file mode 100644 index c939d23f7..000000000 --- a/tests/Feature/ActionRules/DisableTest.php +++ /dev/null @@ -1,50 +0,0 @@ -slot('dispatch: ' . $row->id) - ->dispatch('executeDispatch', ['id' => $row->id]), - ]; - } - - public function actionRules($row): array - { - return [ - Rule::button('dispatch') - ->when(fn ($dish) => $dish->id == 3) - ->disable(), - ]; - } -}; - -dataset('action:disabled', [ - 'tailwind' => [$component::class, (object) ['theme' => 'tailwind', 'join' => false]], - 'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]], - 'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]], - 'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]], -]); - -it('properly displays "hide" on edit button', function (string $component, object $params) { - livewire($component, [ - 'join' => $params->join, - ]) - ->call($params->theme) - ->set('setUp.footer.perPage', 3) - ->assertSeeHtml("\$dispatch('executeDispatch', JSON.parse('{\u0022id\u0022:1}'") - ->assertSeeHtml("\$dispatch('executeDispatch', JSON.parse('{\u0022id\u0022:2}'") - ->assertSeeHtmlInOrder([ - "disabled=\"disabled\"", - "\$dispatch('executeDispatch', JSON.parse('{\u0022id\u0022:3}'", - ]); -}) - ->with('action:disabled') - ->group('action'); diff --git a/tests/Feature/ActionRules/EditOnClickRulesTest.php b/tests/Feature/ActionRules/EditOnClickRulesTest.php new file mode 100644 index 000000000..511d5e51f --- /dev/null +++ b/tests/Feature/ActionRules/EditOnClickRulesTest.php @@ -0,0 +1,23 @@ + $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'); diff --git a/tests/Feature/ActionRules/HideToggleDetailTest.php b/tests/Feature/ActionRules/HideToggleDetailTest.php new file mode 100644 index 000000000..f263c72a2 --- /dev/null +++ b/tests/Feature/ActionRules/HideToggleDetailTest.php @@ -0,0 +1,23 @@ + $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'); diff --git a/tests/Feature/ActionRules/LoopTest.php b/tests/Feature/ActionRules/LoopTest.php index f76ce3aa3..720378006 100644 --- a/tests/Feature/ActionRules/LoopTest.php +++ b/tests/Feature/ActionRules/LoopTest.php @@ -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]], @@ -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'); diff --git a/tests/Feature/ActionRules/RuleToggleableTest.php b/tests/Feature/ActionRules/RuleToggleableTest.php new file mode 100644 index 000000000..74587a413 --- /dev/null +++ b/tests/Feature/ActionRules/RuleToggleableTest.php @@ -0,0 +1,46 @@ +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');