From b34d195fd20aa461d7bf97f756caaac44973f29e Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Tue, 12 Nov 2024 11:41:58 -0300 Subject: [PATCH] Add tests --- src/Concerns/Filter.php | 9 +- tests/Concerns/TestDatabase.php | 4 +- .../Feature/Filters/FilterDatePickerTest.php | 109 ++++++++++++++++++ 3 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 tests/Feature/Filters/FilterDatePickerTest.php diff --git a/src/Concerns/Filter.php b/src/Concerns/Filter.php index 176be3c81..40ac3d3c2 100644 --- a/src/Concerns/Filter.php +++ b/src/Concerns/Filter.php @@ -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')); @@ -139,7 +136,7 @@ public function datePickerChanged( if ($type === 'datetime') { $endDate->setTimeZone($appTimeZone); - + if ($endDate->isStartOfDay()) { $endDate->endOfDay()->setTimeZone($appTimeZone); } diff --git a/tests/Concerns/TestDatabase.php b/tests/Concerns/TestDatabase.php index b4f0cdc68..99472e578 100644 --- a/tests/Concerns/TestDatabase.php +++ b/tests/Concerns/TestDatabase.php @@ -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', @@ -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, ], diff --git a/tests/Feature/Filters/FilterDatePickerTest.php b/tests/Feature/Filters/FilterDatePickerTest.php new file mode 100644 index 000000000..d5eb3d1ba --- /dev/null +++ b/tests/Feature/Filters/FilterDatePickerTest.php @@ -0,0 +1,109 @@ +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]], +]);