Skip to content

Commit d32b5f4

Browse files
committed
cleanup
1 parent 7b06673 commit d32b5f4

File tree

6 files changed

+70
-257
lines changed

6 files changed

+70
-257
lines changed

app/Livewire/MobilePricing.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
class MobilePricing extends Component
1313
{
1414
protected $listeners = [
15-
'email-submitted' => 'handleEmailSubmitted',
15+
'purchase-request-submitted' => 'handlePurchaseRequest',
1616
];
1717

18-
public function handleEmailSubmitted(array $data)
18+
public function handlePurchaseRequest(array $data)
1919
{
2020
$user = $this->findOrCreateUser($data['email']);
2121

@@ -24,15 +24,11 @@ public function handleEmailSubmitted(array $data)
2424

2525
public function createCheckoutSession(string $plan, ?User $user = null)
2626
{
27-
$user ??= Auth::user();
28-
29-
if (! $user) {
27+
if (! ($user ??= Auth::user())) {
3028
return;
3129
}
3230

33-
$subscription = Subscription::tryFrom($plan);
34-
35-
if (! $subscription) {
31+
if (! ($subscription = Subscription::tryFrom($plan))) {
3632
return;
3733
}
3834

@@ -60,7 +56,14 @@ private function findOrCreateUser(string $email): User
6056

6157
private function successUrl(): string
6258
{
63-
return Str::replace('abc', '{CHECKOUT_SESSION_ID}', route('order.success', ['checkoutSessionId' => 'abc']));
59+
// This is a hack to get the route() function to work. If you try
60+
// to pass {CHECKOUT_SESSION_ID} to the route function, it will
61+
// throw an error.
62+
return Str::replace(
63+
'abc',
64+
'{CHECKOUT_SESSION_ID}',
65+
route('order.success', ['checkoutSessionId' => 'abc'])
66+
);
6467
}
6568

6669
public function render()

app/Livewire/PurchaseModal.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Livewire;
44

5+
use Livewire\Attributes\Renderless;
56
use Livewire\Attributes\Validate;
67
use Livewire\Component;
78

@@ -18,10 +19,10 @@ class PurchaseModal extends Component
1819
'email' => 'required|email',
1920
];
2021

21-
public function openModal($plan): void
22+
#[Renderless]
23+
public function setPlan(string $plan): void
2224
{
2325
$this->selectedPlan = $plan;
24-
$this->showModal = true;
2526
}
2627

2728
public function closeModal(): void
@@ -31,11 +32,11 @@ public function closeModal(): void
3132
$this->resetValidation();
3233
}
3334

34-
public function emitEmail()
35+
public function submit(): void
3536
{
3637
$this->validate();
3738

38-
$this->dispatch('email-submitted', [
39+
$this->dispatch('purchase-request-submitted', [
3940
'email' => $this->email,
4041
'plan' => $this->selectedPlan,
4142
]);

resources/views/livewire/purchase-modal.blade.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
<div
33
x-data="{
44
open: @entangle('showModal'),
5-
selectedPlan: @entangle('selectedPlan'),
65
}"
76
@open-purchase-modal.window="
87
open = true;
9-
selectedPlan = $event.detail.plan;
10-
$wire.openModal($event.detail.plan);
8+
$wire.setPlan($event.detail.plan);
119
"
1210
>
1311
<!-- Modal Backdrop -->
@@ -23,7 +21,6 @@ class="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm"
2321
x-cloak
2422
></div>
2523

26-
<!-- Modal Content -->
2724
<div
2825
x-show="open"
2926
x-transition:enter="transition duration-300 ease-out"
@@ -49,7 +46,7 @@ class="w-full max-w-md rounded-2xl bg-white p-8 shadow-xl dark:bg-mirage"
4946
</div>
5047

5148
<form
52-
wire:submit.prevent="emitEmail"
49+
wire:submit.prevent="submit"
5350
class="space-y-8"
5451
>
5552
<div>

tests/Feature/Livewire/PurchaseModalTest.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,18 @@
33
namespace Tests\Feature\Livewire;
44

55
use App\Livewire\PurchaseModal;
6-
use Illuminate\Foundation\Testing\RefreshDatabase;
76
use Livewire\Livewire;
87
use PHPUnit\Framework\Attributes\Test;
98
use Tests\TestCase;
109

1110
class PurchaseModalTest extends TestCase
1211
{
13-
use RefreshDatabase;
14-
1512
#[Test]
16-
public function purchase_modal_can_be_opened_with_plan()
13+
public function purchase_modal_can_set_a_plan()
1714
{
1815
Livewire::test(PurchaseModal::class)
19-
->call('openModal', 'mini')
20-
->assertSet('selectedPlan', 'mini')
21-
->assertSet('showModal', true);
16+
->call('setPlan', 'mini')
17+
->assertSet('selectedPlan', 'mini');
2218
}
2319

2420
#[Test]
@@ -38,30 +34,28 @@ public function purchase_modal_can_be_closed()
3834
public function purchase_modal_validates_email()
3935
{
4036
Livewire::test(PurchaseModal::class)
41-
->call('openModal', 'mini')
4237
->set('email', 'invalid-email')
43-
->call('emitEmail')
38+
->call('submit')
4439
->assertHasErrors(['email' => 'email']);
4540
}
4641

4742
#[Test]
4843
public function purchase_modal_requires_email()
4944
{
5045
Livewire::test(PurchaseModal::class)
51-
->call('openModal', 'mini')
5246
->set('email', '')
53-
->call('emitEmail')
47+
->call('submit')
5448
->assertHasErrors(['email' => 'required']);
5549
}
5650

5751
#[Test]
58-
public function purchase_modal_emits_event_with_valid_email()
52+
public function test_submit_action()
5953
{
6054
Livewire::test(PurchaseModal::class)
61-
->call('openModal', 'mini')
55+
->call('setPlan', 'mini')
6256
->set('email', '[email protected]')
63-
->call('emitEmail')
64-
->assertDispatched('email-submitted', [
57+
->call('submit')
58+
->assertDispatched('purchase-request-submitted', [
6559
'email' => '[email protected]',
6660
'plan' => 'mini',
6761
]);
@@ -71,25 +65,9 @@ public function purchase_modal_emits_event_with_valid_email()
7165
public function purchase_modal_closes_after_emitting_event()
7266
{
7367
Livewire::test(PurchaseModal::class)
74-
->call('openModal', 'mini')
68+
->call('setPlan', 'mini')
7569
->set('email', '[email protected]')
76-
->call('emitEmail')
70+
->call('submit')
7771
->assertSet('showModal', false);
7872
}
79-
80-
#[Test]
81-
public function purchase_modal_can_be_opened_via_alpine_event()
82-
{
83-
$component = Livewire::test(PurchaseModal::class);
84-
85-
// Simulate the Alpine.js event
86-
$component->dispatch('open-purchase-modal', ['plan' => 'pro'])
87-
->assertDispatched('open-purchase-modal');
88-
89-
// Since we can't directly test Alpine.js event handling in PHPUnit,
90-
// we'll verify that the openModal method works as expected
91-
$component->call('openModal', 'pro')
92-
->assertSet('selectedPlan', 'pro')
93-
->assertSet('showModal', true);
94-
}
9573
}

0 commit comments

Comments
 (0)