mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-11 15:23:17 +01:00
Add additional tests for filament
This commit is contained in:
committed by
Constantin Graf
parent
0551c633a3
commit
08f9edbc96
116
tests/Unit/Filament/Widgets/TimeEntriesCreatedWidgetTest.php
Normal file
116
tests/Unit/Filament/Widgets/TimeEntriesCreatedWidgetTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Filament\Widgets;
|
||||
|
||||
use App\Filament\Widgets\TimeEntriesCreated;
|
||||
use App\Models\TimeEntry;
|
||||
use Livewire\Livewire;
|
||||
use PHPUnit\Framework\Attributes\UsesClass;
|
||||
|
||||
#[UsesClass(TimeEntriesCreated::class)]
|
||||
class TimeEntriesCreatedWidgetTest extends ChartWidgetTestCase
|
||||
{
|
||||
public function test_widget_renders_with_filters(): void
|
||||
{
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertSee('Time Entries Created');
|
||||
$response->assertSee('Last week');
|
||||
$response->assertSee('Last month');
|
||||
$response->assertSee('Last year');
|
||||
$response->assertSet('filter', 'week');
|
||||
}
|
||||
|
||||
public function test_counts_only_non_imported_time_entries_created_in_the_last_week_by_default(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(2), 'is_imported' => true],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class);
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame('Time Entries Created', $data['datasets'][0]['label']);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
$this->assertSame(now()->subWeek()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_month_filter_counts_time_entries_created_in_the_last_month_per_day(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => true],
|
||||
['created_at' => now()->subMonths(2), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'month');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$response->assertDispatched('updateChartData');
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertSame(now()->subMonth()->format('Y-m-d'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m-d'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_year_filter_counts_time_entries_created_in_the_last_year_per_month(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => false],
|
||||
['created_at' => now()->subMonths(3), 'is_imported' => true],
|
||||
['created_at' => now()->subYears(2), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'year');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(2, $this->sumOfChartData($data));
|
||||
$this->assertCount(13, $data['labels']);
|
||||
$this->assertSame(now()->subYear()->format('Y-m'), $data['labels']->first());
|
||||
$this->assertSame(now()->format('Y-m'), $data['labels']->last());
|
||||
}
|
||||
|
||||
public function test_unknown_filter_falls_back_to_last_week(): void
|
||||
{
|
||||
// Arrange
|
||||
TimeEntry::factory()->createMany([
|
||||
['created_at' => now()->subDay(), 'is_imported' => false],
|
||||
['created_at' => now()->subDays(10), 'is_imported' => false],
|
||||
]);
|
||||
|
||||
// Act
|
||||
$response = Livewire::test(TimeEntriesCreated::class)
|
||||
->set('filter', 'unknown');
|
||||
|
||||
// Assert
|
||||
$response->assertSuccessful();
|
||||
$data = $this->getChartData($response);
|
||||
$this->assertSame(1, $this->sumOfChartData($data));
|
||||
$this->assertCount(8, $data['labels']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user