feat: strip FuelFinder to layout shell, wire sub-components
This commit is contained in:
@@ -2,117 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Livewire\Public;
|
namespace App\Livewire\Public;
|
||||||
|
|
||||||
use Illuminate\Http\Client\ConnectionException;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
use Livewire\Attributes\Layout;
|
use Livewire\Attributes\Layout;
|
||||||
use Livewire\Attributes\Validate;
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
|
|
||||||
#[Layout('layouts.guest')]
|
#[Layout('layouts.shell')]
|
||||||
class FuelFinder extends Component
|
final class FuelFinder extends Component
|
||||||
{
|
{
|
||||||
#[Validate('required|string', message: 'Please enter a postcode, town or city.')]
|
|
||||||
public string $search = '';
|
|
||||||
|
|
||||||
#[Validate('required|string', message: 'Please select a fuel type.')]
|
|
||||||
public string $fuelType = 'petrol';
|
|
||||||
|
|
||||||
#[Validate('required|integer|min:1|max:20')]
|
|
||||||
public int $radius = 5;
|
|
||||||
|
|
||||||
#[Validate('nullable|string|in:price,distance,updated,brand,reliable')]
|
|
||||||
public string $sort = 'reliable';
|
|
||||||
|
|
||||||
public array $results = [];
|
|
||||||
|
|
||||||
public array $meta = [];
|
|
||||||
|
|
||||||
public ?array $prediction = null;
|
|
||||||
|
|
||||||
public ?string $apiError = null;
|
|
||||||
|
|
||||||
public bool $hasSearched = false;
|
|
||||||
|
|
||||||
public function updatedFuelType(): void
|
|
||||||
{
|
|
||||||
if ($this->hasSearched) {
|
|
||||||
$this->findStations();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updatedRadius(): void
|
|
||||||
{
|
|
||||||
if ($this->hasSearched) {
|
|
||||||
$this->findStations();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function updatedSort(): void
|
|
||||||
{
|
|
||||||
if ($this->hasSearched) {
|
|
||||||
$this->findStations();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function findStations(): void
|
|
||||||
{
|
|
||||||
$this->validate();
|
|
||||||
|
|
||||||
$this->results = [];
|
|
||||||
$this->meta = [];
|
|
||||||
$this->prediction = null;
|
|
||||||
$this->apiError = null;
|
|
||||||
$this->hasSearched = false;
|
|
||||||
|
|
||||||
$radiusKm = round($this->radius * 1.60934, 2);
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = Http::timeout(10)
|
|
||||||
->withHeaders(['X-Api-Key' => config('app.api_secret_key')])
|
|
||||||
->get(url('/api/stations'), [
|
|
||||||
'postcode' => $this->search,
|
|
||||||
'fuel_type' => $this->fuelType,
|
|
||||||
'radius' => $radiusKm,
|
|
||||||
'sort' => $this->sort,
|
|
||||||
]);
|
|
||||||
} catch (ConnectionException) {
|
|
||||||
$this->apiError = 'Unable to fetch stations. Please try again.';
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($response->status() === 422) {
|
|
||||||
$errors = $response->json('errors', []);
|
|
||||||
$this->apiError = collect($errors)->flatten()->first()
|
|
||||||
?? $response->json('message', 'Validation error.');
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! $response->successful()) {
|
|
||||||
$this->apiError = 'Unable to fetch stations. Please try again.';
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->results = $response->json('data', []);
|
|
||||||
$this->meta = $response->json('meta', []);
|
|
||||||
$this->hasSearched = true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
$predictionResponse = Http::timeout(10)
|
|
||||||
->withHeaders(['X-Api-Key' => config('app.api_secret_key')])
|
|
||||||
->get(url('/api/prediction'));
|
|
||||||
|
|
||||||
if ($predictionResponse->successful()) {
|
|
||||||
$this->prediction = $predictionResponse->json();
|
|
||||||
}
|
|
||||||
} catch (ConnectionException) {
|
|
||||||
// Prediction failure is silent — stations are more important
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function render(): View
|
public function render(): View
|
||||||
{
|
{
|
||||||
return view('livewire.public.fuel-finder');
|
return view('livewire.public.fuel-finder');
|
||||||
|
|||||||
@@ -1,201 +1,77 @@
|
|||||||
<div class="flex h-dvh flex-col bg-[#f5ede5]">
|
<div class="flex h-dvh flex-col bg-surface-page">
|
||||||
|
|
||||||
<x-mobile-header />
|
{{-- HEADER --}}
|
||||||
|
<header class="shrink-0 z-50 bg-surface border-b border-border shadow-sm
|
||||||
|
flex items-center justify-between px-5 pb-4 md:px-8"
|
||||||
|
style="padding-top: max(1rem, env(safe-area-inset-top))">
|
||||||
|
|
||||||
{{-- Scrollable main content, offset for fixed header (~112px) and footer (~80px) --}}
|
<a href="{{ route('home') }}" class="flex items-center gap-2.5">
|
||||||
<main
|
<div class="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-primary shadow-md md:h-10 md:w-10">
|
||||||
class="flex-1 overflow-y-auto pt-28 pb-20"
|
<iconify-icon icon="lucide:fuel" class="text-lg text-white md:text-xl"></iconify-icon>
|
||||||
style="-ms-overflow-style:none;scrollbar-width:none;"
|
</div>
|
||||||
>
|
<span class="text-xl font-black tracking-tighter text-primary md:text-2xl">FuelAlert</span>
|
||||||
|
</a>
|
||||||
|
|
||||||
{{-- #search --}}
|
<nav class="hidden items-center gap-8 md:flex">
|
||||||
<section class="space-y-3 px-5 pt-5 pb-4">
|
<a href="#" class="text-sm font-semibold text-text-muted transition-colors hover:text-primary">Prices</a>
|
||||||
<form wire:submit="findStations">
|
<a href="#" class="text-sm font-semibold text-text-muted transition-colors hover:text-primary">Alerts</a>
|
||||||
|
<a href="#" class="text-sm font-semibold text-text-muted transition-colors hover:text-primary">Trends</a>
|
||||||
|
</nav>
|
||||||
|
|
||||||
<div
|
@auth
|
||||||
x-data="{
|
<a href="{{ route('dashboard') }}"
|
||||||
query: @js($search),
|
class="flex h-9 w-9 items-center justify-center rounded-full border border-border bg-surface-subtle">
|
||||||
locatingUser: false,
|
<iconify-icon icon="lucide:user" class="text-base text-text-muted"></iconify-icon>
|
||||||
_usedIpFallback: false,
|
</a>
|
||||||
async _postcodeFromLatLng(lat, lng) {
|
@else
|
||||||
const res = await fetch(`https://api.postcodes.io/postcodes?lon=${lng}&lat=${lat}&limit=1&radius=1000`);
|
<a href="{{ route('login') }}"
|
||||||
const data = await res.json();
|
class="flex h-9 w-9 items-center justify-center rounded-full border border-border bg-surface-subtle">
|
||||||
return data?.result?.[0]?.postcode ?? null;
|
<iconify-icon icon="lucide:user" class="text-base text-text-muted"></iconify-icon>
|
||||||
},
|
</a>
|
||||||
async locateUser() {
|
@endauth
|
||||||
this.locatingUser = true;
|
</header>
|
||||||
this._usedIpFallback = false;
|
|
||||||
try {
|
|
||||||
let lat, lng;
|
|
||||||
try {
|
|
||||||
const pos = await new Promise((resolve, reject) =>
|
|
||||||
navigator.geolocation.getCurrentPosition(resolve, reject, { enableHighAccuracy: false, timeout: 5000, maximumAge: 60000 })
|
|
||||||
);
|
|
||||||
lat = pos.coords.latitude;
|
|
||||||
lng = pos.coords.longitude;
|
|
||||||
} catch (e) {
|
|
||||||
const d = await fetch('https://ipapi.co/json/').then(r => r.json());
|
|
||||||
lat = d.latitude;
|
|
||||||
lng = d.longitude;
|
|
||||||
this._usedIpFallback = true;
|
|
||||||
}
|
|
||||||
const postcode = await this._postcodeFromLatLng(lat, lng);
|
|
||||||
if (postcode) {
|
|
||||||
this.query = postcode;
|
|
||||||
this.$wire.set('search', postcode);
|
|
||||||
this.$wire.findStations();
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
// silent
|
|
||||||
} finally {
|
|
||||||
this.locatingUser = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}"
|
|
||||||
class="relative mb-3"
|
|
||||||
>
|
|
||||||
<iconify-icon
|
|
||||||
icon="lucide:map-pin"
|
|
||||||
class="absolute left-4 top-1/2 -translate-y-1/2 text-xl text-[#89726c] pointer-events-none"
|
|
||||||
></iconify-icon>
|
|
||||||
<input
|
|
||||||
wire:model="search"
|
|
||||||
x-model="query"
|
|
||||||
x-ref="searchInput"
|
|
||||||
type="text"
|
|
||||||
name="search"
|
|
||||||
@focus="_usedIpFallback = false"
|
|
||||||
placeholder="Postcode, town or city"
|
|
||||||
class="h-14 w-full rounded-xl border border-[#e5ded7] bg-[#faf6f3] pl-12 pr-36 text-base font-semibold text-[#4a3f3b] focus:outline-none focus:ring-2 focus:ring-[#bb5b3e] focus:border-transparent"
|
|
||||||
/>
|
|
||||||
{{-- Right-side controls --}}
|
|
||||||
<div class="absolute inset-y-0 right-0 flex items-center gap-2 pr-3">
|
|
||||||
{{-- Clear --}}
|
|
||||||
<button
|
|
||||||
x-show="query.length > 0"
|
|
||||||
x-cloak
|
|
||||||
type="button"
|
|
||||||
@click="query = ''; $wire.set('search', ''); _usedIpFallback = false"
|
|
||||||
class="text-[#89726c] hover:text-[#4a3f3b]"
|
|
||||||
>
|
|
||||||
<iconify-icon icon="lucide:x" class="text-base"></iconify-icon>
|
|
||||||
</button>
|
|
||||||
{{-- Near me pill --}}
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
@click="locateUser()"
|
|
||||||
:disabled="locatingUser"
|
|
||||||
class="flex items-center gap-1.5 rounded-full bg-[#e5ded7] px-3 py-1.5 text-sm font-semibold text-[#4a3f3b] disabled:opacity-40"
|
|
||||||
>
|
|
||||||
<iconify-icon x-show="!locatingUser" icon="lucide:locate-fixed" class="text-sm"></iconify-icon>
|
|
||||||
<iconify-icon x-show="locatingUser" icon="lucide:loader-circle" class="text-sm animate-spin"></iconify-icon>
|
|
||||||
<span x-text="locatingUser ? 'Finding...' : 'Near me'">Near me</span>
|
|
||||||
</button>
|
|
||||||
{{-- Search --}}
|
|
||||||
<button
|
|
||||||
type="submit"
|
|
||||||
wire:loading.attr="disabled"
|
|
||||||
class="text-[#89726c] disabled:opacity-60"
|
|
||||||
>
|
|
||||||
<iconify-icon wire:loading.remove wire:target="findStations" icon="lucide:search" class="text-xl"></iconify-icon>
|
|
||||||
<iconify-icon wire:loading wire:target="findStations" icon="lucide:loader-circle" class="text-xl animate-spin"></iconify-icon>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
{{-- IP fallback nudge --}}
|
|
||||||
<div
|
|
||||||
x-show="_usedIpFallback"
|
|
||||||
x-cloak
|
|
||||||
x-transition:enter="transition ease-out duration-300 delay-500"
|
|
||||||
x-transition:enter-start="opacity-0 -translate-y-2"
|
|
||||||
x-transition:enter-end="opacity-100 translate-y-0"
|
|
||||||
x-transition:leave="transition ease-in duration-200"
|
|
||||||
x-transition:leave-end="opacity-0"
|
|
||||||
class="absolute top-full left-0 right-0 z-40 mt-2"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
@click="$refs.searchInput.focus(); _usedIpFallback = false"
|
|
||||||
class="cursor-pointer rounded-xl border border-amber-200 bg-amber-50 px-3 py-2 transition-colors hover:bg-amber-100"
|
|
||||||
>
|
|
||||||
<p class="text-center text-xs text-amber-800">
|
|
||||||
<span class="font-medium">Showing approximate location.</span>
|
|
||||||
<span class="underline">Enter your postcode above</span> for exact results.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
@error('search')
|
|
||||||
<p class="mb-2 text-sm text-red-600">{{ $message }}</p>
|
|
||||||
@enderror
|
|
||||||
|
|
||||||
{{-- Filter rows --}}
|
{{-- MAIN --}}
|
||||||
<div class="space-y-2 mb-3">
|
<main class="flex-1 overflow-y-auto" style="-ms-overflow-style:none;scrollbar-width:none;">
|
||||||
<div class="flex gap-2">
|
<div class="md:mx-auto md:max-w-3xl">
|
||||||
<div class="shrink-0">
|
|
||||||
<x-fuel.type-select wire:model.live="fuelType" />
|
|
||||||
</div>
|
|
||||||
<div class="shrink-0">
|
|
||||||
<x-fuel.sort-select wire:model.live="sort" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="flex">
|
|
||||||
<div class="shrink-0">
|
|
||||||
<x-fuel.radius-select wire:model.live="radius" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</form>
|
<livewire:public.fuel.search />
|
||||||
|
<livewire:public.fuel.recommendation />
|
||||||
|
<livewire:public.fuel.map />
|
||||||
|
<livewire:public.fuel.station-list />
|
||||||
|
|
||||||
@if ($apiError)
|
|
||||||
<div class="rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">
|
|
||||||
{{ $apiError }}
|
|
||||||
</div>
|
|
||||||
@endif
|
|
||||||
</section>
|
|
||||||
|
|
||||||
{{-- #recommendation --}}
|
|
||||||
{{-- @if ($prediction)
|
|
||||||
<section class="px-5 pb-5">
|
|
||||||
<x-fuel.recommendation :prediction="$prediction" />
|
|
||||||
</section>
|
|
||||||
@endif --}}
|
|
||||||
|
|
||||||
{{-- #map --}}
|
|
||||||
<section class="mb-4" wire:ignore>
|
|
||||||
<x-fuel.station-map :results="$results" />
|
|
||||||
</section>
|
|
||||||
|
|
||||||
{{-- #stations --}}
|
|
||||||
@if ($hasSearched)
|
|
||||||
<section class="px-5 pb-5">
|
|
||||||
@if (! empty($meta))
|
|
||||||
<div class="mb-3 flex items-center justify-between">
|
|
||||||
<h3 class="text-base font-bold text-[#4a3f3b]">Stations Nearby</h3>
|
|
||||||
<span class="text-[10px] font-bold uppercase tracking-widest text-[#89726c]">
|
|
||||||
{{ $meta['count'] ?? 0 }} {{ str('Result')->plural($meta['count'] ?? 0) }}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
@endif
|
|
||||||
|
|
||||||
@forelse ($results as $station)
|
|
||||||
<div class="mb-2">
|
|
||||||
<x-fuel.station-card :station="$station" />
|
|
||||||
</div>
|
|
||||||
@empty
|
|
||||||
<p class="text-sm text-[#89726c]">
|
|
||||||
No stations found within {{ $radius }} {{ str('mile')->plural($radius) }} of "{{ $search }}".
|
|
||||||
</p>
|
|
||||||
@endforelse
|
|
||||||
</section>
|
|
||||||
@endif
|
|
||||||
|
|
||||||
{{-- #forecast --}}
|
|
||||||
<section class="px-5 pb-8">
|
<section class="px-5 pb-8">
|
||||||
<x-fuel.forecast />
|
<x-fuel.forecast />
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<x-mobile-footer />
|
{{-- BOTTOM TAB BAR --}}
|
||||||
|
@php
|
||||||
|
$tabs = [
|
||||||
|
['label' => 'Prices', 'icon' => 'lucide:fuel', 'route' => 'home'],
|
||||||
|
['label' => 'Alerts', 'icon' => 'lucide:bell', 'route' => null],
|
||||||
|
['label' => 'Forecourts', 'icon' => 'lucide:map-pin', 'route' => null],
|
||||||
|
['label' => 'Trends', 'icon' => 'lucide:trending-up', 'route' => null],
|
||||||
|
];
|
||||||
|
@endphp
|
||||||
|
<nav class="shrink-0 border-t border-border bg-surface md:hidden"
|
||||||
|
style="padding-bottom: max(0.5rem, env(safe-area-inset-bottom))">
|
||||||
|
<div class="flex pt-3">
|
||||||
|
@foreach ($tabs as $tab)
|
||||||
|
@php $active = $tab['route'] && request()->routeIs($tab['route']); @endphp
|
||||||
|
<div class="flex flex-1 flex-col items-center gap-1">
|
||||||
|
<iconify-icon
|
||||||
|
icon="{{ $tab['icon'] }}"
|
||||||
|
class="text-xl {{ $active ? 'text-primary' : 'text-text-muted' }}"
|
||||||
|
></iconify-icon>
|
||||||
|
<span class="text-[10px] font-bold uppercase tracking-wide {{ $active ? 'text-primary' : 'text-text-muted' }}">
|
||||||
|
{{ $tab['label'] }}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
@endforeach
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,223 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Livewire\Public\FuelFinder;
|
use App\Livewire\Public\FuelFinder;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
||||||
use Illuminate\Support\Facades\Http;
|
|
||||||
use Livewire\Livewire;
|
use Livewire\Livewire;
|
||||||
|
|
||||||
uses(RefreshDatabase::class);
|
it('renders the fuel finder shell', function () {
|
||||||
|
|
||||||
it('renders the fuel finder page', function () {
|
|
||||||
Livewire::test(FuelFinder::class)
|
Livewire::test(FuelFinder::class)
|
||||||
->assertStatus(200)
|
->assertStatus(200);
|
||||||
->assertSeeHtml('name="search"');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('has default property values', function () {
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->assertSet('search', '')
|
|
||||||
->assertSet('fuelType', 'petrol')
|
|
||||||
->assertSet('radius', 5)
|
|
||||||
->assertSet('sort', 'reliable')
|
|
||||||
->assertSet('results', [])
|
|
||||||
->assertSet('meta', [])
|
|
||||||
->assertSet('prediction', null)
|
|
||||||
->assertSet('apiError', null)
|
|
||||||
->assertSet('hasSearched', false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('validates search is required', function () {
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->call('findStations')
|
|
||||||
->assertHasErrors(['search' => 'required']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('validates fuelType is required', function () {
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', '')
|
|
||||||
->call('findStations')
|
|
||||||
->assertHasErrors(['fuelType' => 'required']);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('populates results, meta, and prediction on successful search', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response([
|
|
||||||
'data' => [
|
|
||||||
[
|
|
||||||
'station_id' => 'abc123',
|
|
||||||
'name' => 'BP Garage',
|
|
||||||
'brand' => 'BP',
|
|
||||||
'is_supermarket' => false,
|
|
||||||
'address' => '1 High Street',
|
|
||||||
'postcode' => 'SW1A 1AA',
|
|
||||||
'lat' => 51.5074,
|
|
||||||
'lng' => -0.1278,
|
|
||||||
'distance_km' => 1.5,
|
|
||||||
'fuel_type' => 'e10',
|
|
||||||
'price_pence' => 14390,
|
|
||||||
'price' => 143.9,
|
|
||||||
'price_updated_at' => '2026-04-05T08:00:00.000Z',
|
|
||||||
'price_classification' => 'current',
|
|
||||||
'price_classification_label' => 'Current',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'meta' => [
|
|
||||||
'count' => 1,
|
|
||||||
'lowest_pence' => 14390,
|
|
||||||
'avg_pence' => 14390.0,
|
|
||||||
],
|
|
||||||
], 200),
|
|
||||||
'*/api/prediction*' => Http::response([
|
|
||||||
'action' => 'fill_now',
|
|
||||||
'confidence_score' => 80.0,
|
|
||||||
'confidence_label' => 'high',
|
|
||||||
'reasoning' => 'Prices rising.',
|
|
||||||
'predicted_direction' => 'up',
|
|
||||||
'predicted_change_pence' => 3.5,
|
|
||||||
], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->call('findStations')
|
|
||||||
->assertSet('hasSearched', true)
|
|
||||||
->assertSet('apiError', null)
|
|
||||||
->assertSet('results', fn (array $r) => count($r) === 1 && $r[0]['name'] === 'BP Garage')
|
|
||||||
->assertSet('meta', fn (array $m) => $m['count'] === 1)
|
|
||||||
->assertSet('prediction', fn (?array $p) => $p !== null && $p['action'] === 'fill_now');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets apiError from 422 station response and leaves prediction null', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response([
|
|
||||||
'errors' => ['postcode' => ['Postcode not found.']],
|
|
||||||
], 422),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'ZZ99 9ZZ')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->call('findStations')
|
|
||||||
->assertSet('results', [])
|
|
||||||
->assertSet('meta', [])
|
|
||||||
->assertSet('prediction', null)
|
|
||||||
->assertSet('hasSearched', false)
|
|
||||||
->assertSet('apiError', 'Postcode not found.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets generic apiError on server error', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response([], 500),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->call('findStations')
|
|
||||||
->assertSet('apiError', 'Unable to fetch stations. Please try again.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('converts radius from miles to km in the outgoing stations request', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response(['action' => 'no_signal', 'confidence_score' => 0, 'confidence_label' => 'low', 'reasoning' => '', 'predicted_direction' => 'stable', 'predicted_change_pence' => 0], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->set('radius', 5)
|
|
||||||
->call('findStations');
|
|
||||||
|
|
||||||
Http::assertSent(function ($request) {
|
|
||||||
if (! str_contains($request->url(), 'api/stations')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$data = $request->data();
|
|
||||||
|
|
||||||
return isset($data['radius']) && abs((float) $data['radius'] - 8.05) < 0.01;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('resets state before each new search', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response(['action' => 'no_signal', 'confidence_score' => 0, 'confidence_label' => 'low', 'reasoning' => '', 'predicted_direction' => 'stable', 'predicted_change_pence' => 0], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->set('results', [['name' => 'Old Result']])
|
|
||||||
->set('apiError', 'Old error')
|
|
||||||
->call('findStations')
|
|
||||||
->assertSet('apiError', null)
|
|
||||||
->assertSet('results', []);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not call findStations on updatedFuelType if not yet searched', function () {
|
|
||||||
Http::fake();
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('fuelType', 'diesel');
|
|
||||||
|
|
||||||
Http::assertNothingSent();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('re-runs findStations on updatedFuelType when already searched', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response(['action' => 'no_signal', 'confidence_score' => 0, 'confidence_label' => 'low', 'reasoning' => '', 'predicted_direction' => 'stable', 'predicted_change_pence' => 0], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('hasSearched', true)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'diesel');
|
|
||||||
|
|
||||||
Http::assertSentCount(2); // stations + prediction
|
|
||||||
});
|
|
||||||
|
|
||||||
it('re-runs findStations on updatedRadius when already searched', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response(['action' => 'no_signal', 'confidence_score' => 0, 'confidence_label' => 'low', 'reasoning' => '', 'predicted_direction' => 'stable', 'predicted_change_pence' => 0], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('hasSearched', true)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('radius', 10);
|
|
||||||
|
|
||||||
Http::assertSentCount(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('re-runs findStations on updatedSort when already searched', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response(['action' => 'no_signal', 'confidence_score' => 0, 'confidence_label' => 'low', 'reasoning' => '', 'predicted_direction' => 'stable', 'predicted_change_pence' => 0], 200),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('hasSearched', true)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('sort', 'price');
|
|
||||||
|
|
||||||
Http::assertSentCount(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('prediction remains null when prediction api fails', function () {
|
|
||||||
Http::fake([
|
|
||||||
'*/api/stations*' => Http::response(['data' => [], 'meta' => ['count' => 0]], 200),
|
|
||||||
'*/api/prediction*' => Http::response([], 500),
|
|
||||||
]);
|
|
||||||
|
|
||||||
Livewire::test(FuelFinder::class)
|
|
||||||
->set('search', 'SW1A 1AA')
|
|
||||||
->set('fuelType', 'petrol')
|
|
||||||
->call('findStations')
|
|
||||||
->assertSet('hasSearched', true)
|
|
||||||
->assertSet('prediction', null);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user