Allow editing an existing transaction
Desktop reuses the add-form with a Modifier link per row; mobile gets a "Dernières opérations" list under the quick-entry form since no table exists there. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
9114bb2e8d
commit
97dc7cdb2a
@@ -2,6 +2,7 @@
|
||||
|
||||
use App\Livewire\Suivi\Index;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Livewire\Livewire;
|
||||
|
||||
it('records a transaction for the current household', function (): void {
|
||||
@@ -98,6 +99,89 @@ it('resets the form to defaults after adding a transaction', function (): void {
|
||||
expect($component->get('details'))->toBe('');
|
||||
});
|
||||
|
||||
it('prefills the form when starting to edit a transaction', function (): void {
|
||||
$user = User::factory()->create();
|
||||
$household = $user->currentHousehold;
|
||||
$category = $household->categories()->create(['name' => 'Loyer', 'type' => 'depense', 'position' => 1]);
|
||||
$transaction = $household->transactions()->create([
|
||||
'category_id' => $category->id, 'user_id' => $user->id,
|
||||
'date' => '2026-03-05', 'date_effective' => '2026-03-06', 'amount' => 1200, 'details' => 'Loyer mars',
|
||||
]);
|
||||
|
||||
$component = Livewire::actingAs($user)->test(Index::class)->call('startEditTransaction', $transaction->id);
|
||||
|
||||
expect($component->get('editingTransactionId'))->toBe($transaction->id);
|
||||
expect($component->get('categoryId'))->toBe($category->id);
|
||||
expect($component->get('amount'))->toBe('1200.00');
|
||||
expect($component->get('details'))->toBe('Loyer mars');
|
||||
expect($component->get('date'))->toBe('2026-03-05');
|
||||
expect($component->get('dateEffective'))->toBe('2026-03-06');
|
||||
});
|
||||
|
||||
it('updates an existing transaction instead of creating a new one', function (): void {
|
||||
$user = User::factory()->create();
|
||||
$household = $user->currentHousehold;
|
||||
$category = $household->categories()->create(['name' => 'Loyer', 'type' => 'depense', 'position' => 1]);
|
||||
$newCategory = $household->categories()->create(['name' => 'Courses', 'type' => 'depense', 'position' => 2]);
|
||||
$transaction = $household->transactions()->create([
|
||||
'category_id' => $category->id, 'user_id' => $user->id,
|
||||
'date' => '2026-03-05', 'date_effective' => '2026-03-05', 'amount' => 1200, 'details' => 'Loyer',
|
||||
]);
|
||||
|
||||
Livewire::actingAs($user)
|
||||
->test(Index::class)
|
||||
->call('startEditTransaction', $transaction->id)
|
||||
->set('categoryId', $newCategory->id)
|
||||
->set('amount', '150')
|
||||
->set('details', 'Courses rectifiées')
|
||||
->call('addTransaction')
|
||||
->assertDispatched('transaction-updated');
|
||||
|
||||
expect($household->transactions()->count())->toBe(1);
|
||||
|
||||
$transaction->refresh();
|
||||
expect($transaction->category_id)->toBe($newCategory->id);
|
||||
expect((float) $transaction->amount)->toBe(150.0);
|
||||
expect($transaction->details)->toBe('Courses rectifiées');
|
||||
});
|
||||
|
||||
it('cancels editing and resets the form without touching the transaction', function (): void {
|
||||
$user = User::factory()->create();
|
||||
$household = $user->currentHousehold;
|
||||
$category = $household->categories()->create(['name' => 'Loyer', 'type' => 'depense', 'position' => 1]);
|
||||
$transaction = $household->transactions()->create([
|
||||
'category_id' => $category->id, 'user_id' => $user->id,
|
||||
'date' => '2026-03-05', 'date_effective' => '2026-03-05', 'amount' => 1200, 'details' => 'Loyer',
|
||||
]);
|
||||
|
||||
$component = Livewire::actingAs($user)
|
||||
->test(Index::class)
|
||||
->call('startEditTransaction', $transaction->id)
|
||||
->call('cancelEditTransaction');
|
||||
|
||||
expect($component->get('editingTransactionId'))->toBeNull();
|
||||
expect($component->get('categoryId'))->toBeNull();
|
||||
expect($component->get('amount'))->toBe('');
|
||||
|
||||
$transaction->refresh();
|
||||
expect((float) $transaction->amount)->toBe(1200.0);
|
||||
});
|
||||
|
||||
it('does not allow editing a transaction belonging to another household', function (): void {
|
||||
$owner = User::factory()->create();
|
||||
$intruder = User::factory()->create();
|
||||
$category = $owner->currentHousehold->categories()->create(['name' => 'Loyer', 'type' => 'depense', 'position' => 1]);
|
||||
$transaction = $owner->currentHousehold->transactions()->create([
|
||||
'category_id' => $category->id, 'user_id' => $owner->id,
|
||||
'date' => '2026-03-05', 'date_effective' => '2026-03-05', 'amount' => 1200,
|
||||
]);
|
||||
|
||||
expect(fn () => Livewire::actingAs($intruder)
|
||||
->test(Index::class)
|
||||
->call('startEditTransaction', $transaction->id)
|
||||
)->toThrow(ModelNotFoundException::class);
|
||||
});
|
||||
|
||||
it('lists active recurring transactions due this month as pending', function (): void {
|
||||
$this->travelTo(now()->setDate(2026, 3, 10));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user