-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create availability control component
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicResrv\Livewire; | ||
|
||
use Livewire\Attributes\Session; | ||
use Livewire\Component; | ||
use Reach\StatamicResrv\Livewire\Forms\AvailabilityData; | ||
|
||
class AvailabilityControl extends Component | ||
{ | ||
#[Session('resrv-search')] | ||
public AvailabilityData $data; | ||
|
||
public function save(): void | ||
{ | ||
$this->data->validate(); | ||
|
||
$this->dispatch('availability-search-updated', $this->data); | ||
} | ||
|
||
public function render() | ||
{ | ||
return <<<'HTML' | ||
<div></div> | ||
HTML; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Reach\StatamicResrv\Tests\Livewire; | ||
|
||
use Livewire\Livewire; | ||
use Reach\StatamicResrv\Livewire\AvailabilityControl; | ||
use Reach\StatamicResrv\Livewire\AvailabilitySearch; | ||
use Reach\StatamicResrv\Tests\TestCase; | ||
|
||
class AvailabilityControlTest extends TestCase | ||
{ | ||
public $date; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->date = now()->setTime(12, 0, 0); | ||
$this->travelTo(today()->setHour(12)); | ||
} | ||
|
||
public function test_renders_successfully() | ||
{ | ||
Livewire::test(AvailabilityControl::class) | ||
->assertStatus(200); | ||
} | ||
|
||
public function test_can_load_data_from_session() | ||
{ | ||
Livewire::test(AvailabilitySearch::class) | ||
->set('data.dates', | ||
[ | ||
'date_start' => $this->date, | ||
'date_end' => $this->date->copy()->add(1, 'day'), | ||
] | ||
) | ||
->set('data.quantity', 2) | ||
->set('data.advanced', 'test') | ||
->set('data.customer', ['adults' => 2]); | ||
|
||
Livewire::test(AvailabilityControl::class) | ||
->assertSet('data.dates.date_start', $this->date) | ||
->assertSet('data.dates.date_end', $this->date->copy()->add(1, 'day')) | ||
->assertSet('data.quantity', 2) | ||
->assertSet('data.advanced', 'test') | ||
->assertSet('data.customer', ['adults' => 2]); | ||
} | ||
|
||
public function test_can_save_valid_data() | ||
{ | ||
Livewire::test(AvailabilityControl::class) | ||
->set('data.dates', [ | ||
'date_start' => $this->date, | ||
'date_end' => $this->date->copy()->add(1, 'day'), | ||
]) | ||
->set('data.quantity', 2) | ||
->set('data.advanced', 'test') | ||
->set('data.customer', ['adults' => 2]) | ||
->call('save') | ||
->assertDispatched('availability-search-updated', [ | ||
'dates' => [ | ||
'date_start' => $this->date->toISOString(), | ||
'date_end' => $this->date->copy()->add(1, 'day')->toISOString(), | ||
], | ||
'quantity' => 2, | ||
'advanced' => 'test', | ||
'customer' => ['adults' => 2], | ||
]); | ||
} | ||
|
||
public function test_validates_dates_before_save() | ||
{ | ||
Livewire::test(AvailabilityControl::class) | ||
->set('data.dates', [ | ||
'date_start' => $this->date->copy()->add(1, 'day'), | ||
'date_end' => $this->date, | ||
]) | ||
->call('save') | ||
->assertHasErrors(['data.dates.date_start', 'data.dates.date_end']) | ||
->assertNotDispatched('availability-search-updated'); | ||
} | ||
} |