122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Livewire;
|
|
|
|
use App\Livewire\Dashboard;
|
|
use App\Models\Article;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Carbon;
|
|
use Livewire\Livewire;
|
|
use Tests\TestCase;
|
|
|
|
class DashboardTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_defaults_to_the_current_day(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->assertSet('from', '2026-07-15')
|
|
->assertSet('to', '2026-07-15');
|
|
}
|
|
|
|
public function test_it_recomputes_stats_when_the_range_changes(): void
|
|
{
|
|
Article::factory()->count(3)->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', '2026-07-31')
|
|
->assertSee('3')
|
|
->set('from', '2026-08-01')
|
|
->set('to', '2026-08-31')
|
|
->assertSee('0');
|
|
}
|
|
|
|
public function test_it_populates_the_range_when_a_preset_is_applied(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'month')
|
|
->assertSet('from', '2026-07-01')
|
|
->assertSet('to', '2026-07-31');
|
|
}
|
|
|
|
public function test_it_applies_the_all_preset_as_a_bounded_range(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'all')
|
|
->assertSet('to', '2026-07-15');
|
|
}
|
|
|
|
public function test_it_ignores_an_unknown_preset(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->call('applyPreset', 'fortnight')
|
|
->assertSet('from', '2026-07-15')
|
|
->assertSet('to', '2026-07-15')
|
|
->assertHasNoErrors();
|
|
}
|
|
|
|
public function test_it_reports_an_inverted_range_against_the_end_date(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-31')
|
|
->set('to', '2026-07-01')
|
|
->assertHasErrors('to');
|
|
}
|
|
|
|
public function test_it_blames_the_start_date_when_the_start_date_is_malformed(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', 'not-a-date')
|
|
->assertHasErrors('from')
|
|
->assertHasNoErrors('to');
|
|
}
|
|
|
|
public function test_it_blames_the_end_date_when_the_end_date_is_malformed(): void
|
|
{
|
|
Carbon::setTestNow('2026-07-15 13:45:00');
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('to', 'not-a-date')
|
|
->assertHasErrors('to')
|
|
->assertHasNoErrors('from');
|
|
}
|
|
|
|
public function test_it_reports_zero_stats_for_an_invalid_range(): void
|
|
{
|
|
Article::factory()->create(['created_at' => Carbon::parse('2026-07-10 12:00:00')]);
|
|
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', 'not-a-date')
|
|
->assertSet('rangeIsValid', false);
|
|
}
|
|
|
|
public function test_it_marks_a_well_formed_range_as_valid(): void
|
|
{
|
|
Livewire::test(Dashboard::class)
|
|
->set('from', '2026-07-01')
|
|
->set('to', '2026-07-31')
|
|
->assertSet('rangeIsValid', true);
|
|
}
|
|
|
|
public function test_it_renders_the_preset_buttons(): void
|
|
{
|
|
Livewire::test(Dashboard::class)
|
|
->assertSee('This Month')
|
|
->assertSee('All Time');
|
|
}
|
|
}
|