Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Nov 12, 2024
1 parent 8d5142b commit b34d195
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/Concerns/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,10 @@ public function datePickerChanged(
$this->resetPage();

if (Str::contains($dateStr, 'to')) {
[$start, $end] = explode(' to ', $dateStr);

$startDate = strval($start);
$endDate = strval($end);
[$startDate, $endDate] = explode(' to ', $dateStr);
} else {
$startDate = strval($selectedDates[0]);
$endDate = strval($selectedDates[1]);
$endDate = strval($selectedDates[1]);
}

$appTimeZone = strval(config('app.timezone'));
Expand All @@ -139,7 +136,7 @@ public function datePickerChanged(

if ($type === 'datetime') {
$endDate->setTimeZone($appTimeZone);

if ($endDate->isStartOfDay()) {
$endDate->endOfDay()->setTimeZone($appTimeZone);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Concerns/TestDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function generate(): array
'chef_id' => 1,
'price' => 10.00,
'in_stock' => true,
'produced_at' => '2021-01-01 00:00:00',
'produced_at' => '2021-01-01 03:00:00',
'chef_name' => null,
'diet' => 2,
'serving_at' => 'table',
Expand All @@ -167,7 +167,7 @@ public static function generate(): array
'chef_id' => 1,
'price' => 20.50,
'in_stock' => true,
'produced_at' => '2021-02-02 00:00:00',
'produced_at' => '2021-02-02 05:00:00',
'chef_name' => 'Nábia',
'diet' => 1,
],
Expand Down
109 changes: 109 additions & 0 deletions tests/Feature/Filters/FilterDatePickerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\DishesTable;

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

use PowerComponents\LivewirePowerGrid\Themes\{Bootstrap5, Tailwind};

$component = new class () extends DishesTable {
public string $tableName = 'filter-date-picker-test';

public function filters(): array
{
return [
Filter::datepicker('produced_at'),
];
}
};

it('should dispatch "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
$component = livewire($component)
->call('setTestThemeClass', $params->theme)
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2024-11-06', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 to 2021-02-02',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Peixada');

expect($component->filters)
->toBe([
'datetime' => [
'produced_at' => [
'start' => 'Fri Jan 01 2021 00:00:00 GMT+0000',
'end' => 'Tue Feb 02 2021 23:59:59 GMT+0000',
'formatted' => '2021-01-01 to 2021-02-02',
],
],
]);
})->with('filterComponent');

it('should filter "pg:datePicker-{tableName}" with date format', function (string $component, object $params) {
livewire($component)
->call('setTestThemeClass', $params->theme)
->assertSee('Peixada')
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2021-03-03', '2021-05-05'],
field: 'produced_at',
dateStr: '2021-03-03 to 2021-05-05',
label: 'Produced At',
type: 'date',
timezone: 'UTC'
)
->assertDontSee('Peixada');
})->with('filterComponent');

it('should filter "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
livewire($component)
->call('setTestThemeClass', $params->theme)
->assertSee('Peixada', 'Pastel de Nata')
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2021-01-01', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 00:00:00 to 2021-02-02 04:00:00',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Pastel de Nata')
->assertDontSee('Peixada');
})->with('filterComponent');

it('should dispatch "pg:datePicker-{tableName}" with datetime format', function (string $component, object $params) {
$component = livewire($component)
->call('setTestThemeClass', $params->theme)
->dispatch(
'pg:datePicker-filter-date-picker-test',
selectedDates: ['2024-11-06', '2021-02-02'],
field: 'produced_at',
dateStr: '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
label: 'Produced At',
type: 'datetime',
timezone: 'UTC'
)
->assertSee('Peixada');

expect($component->filters)
->toBe([
'datetime' => [
'produced_at' => [
'start' => 'Fri Jan 01 2021 03:00:00 GMT+0000',
'end' => 'Tue Feb 02 2021 05:00:00 GMT+0000',
'formatted' => '2021-01-01 03:00:00 to 2021-02-02 05:00:00',
],
],
]);
})->with('filterComponent');

dataset('filterComponent', [
'tailwind -> id' => [$component::class, (object) ['theme' => Tailwind::class]],
'bootstrap -> id' => [$component::class, (object) ['theme' => Bootstrap5::class]],
]);

0 comments on commit b34d195

Please sign in to comment.