feat: extract fuel.recommendation Livewire component
This commit is contained in:
23
app/Livewire/Public/Fuel/Recommendation.php
Normal file
23
app/Livewire/Public/Fuel/Recommendation.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Public\Fuel;
|
||||
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Attributes\On;
|
||||
use Livewire\Component;
|
||||
|
||||
final class Recommendation extends Component
|
||||
{
|
||||
public ?array $prediction = null;
|
||||
|
||||
#[On('stations-found')]
|
||||
public function handle(array $results, array $meta, int $radius = 5, ?array $prediction = null): void
|
||||
{
|
||||
$this->prediction = $prediction;
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.public.fuel.recommendation');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<div>
|
||||
@if ($prediction)
|
||||
<div class="px-5 pb-5">
|
||||
<x-fuel.recommendation :prediction="$prediction" />
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
52
tests/Feature/Livewire/Fuel/RecommendationTest.php
Normal file
52
tests/Feature/Livewire/Fuel/RecommendationTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use App\Livewire\Public\Fuel\Recommendation;
|
||||
use Livewire\Livewire;
|
||||
|
||||
it('renders nothing before stations-found fires', function () {
|
||||
Livewire::test(Recommendation::class)
|
||||
->assertStatus(200)
|
||||
->assertSet('prediction', null)
|
||||
->assertDontSee('Recommendation');
|
||||
});
|
||||
|
||||
it('shows recommendation card when stations-found includes a prediction', function () {
|
||||
$prediction = [
|
||||
'action' => 'fill_now',
|
||||
'confidence_score' => 80.0,
|
||||
'confidence_label' => 'high',
|
||||
'reasoning' => 'Prices are rising sharply.',
|
||||
'predicted_direction' => 'up',
|
||||
'predicted_change_pence' => 3.5,
|
||||
];
|
||||
|
||||
Livewire::test(Recommendation::class)
|
||||
->dispatch('stations-found', results: [], meta: [], prediction: $prediction, radius: 5)
|
||||
->assertSet('prediction', $prediction)
|
||||
->assertSee('Recommendation')
|
||||
->assertSee('Fill up now');
|
||||
});
|
||||
|
||||
it('shows nothing when stations-found has null prediction', function () {
|
||||
Livewire::test(Recommendation::class)
|
||||
->dispatch('stations-found', results: [], meta: [], prediction: null, radius: 5)
|
||||
->assertSet('prediction', null)
|
||||
->assertDontSee('Recommendation');
|
||||
});
|
||||
|
||||
it('clears previous prediction when new stations-found fires with null prediction', function () {
|
||||
$prediction = [
|
||||
'action' => 'fill_now',
|
||||
'confidence_score' => 80.0,
|
||||
'confidence_label' => 'high',
|
||||
'reasoning' => 'Prices rising.',
|
||||
'predicted_direction' => 'up',
|
||||
'predicted_change_pence' => 3.5,
|
||||
];
|
||||
|
||||
Livewire::test(Recommendation::class)
|
||||
->dispatch('stations-found', results: [], meta: [], prediction: $prediction, radius: 5)
|
||||
->assertSee('Recommendation')
|
||||
->dispatch('stations-found', results: [], meta: [], prediction: null, radius: 5)
|
||||
->assertDontSee('Recommendation');
|
||||
});
|
||||
Reference in New Issue
Block a user