|
5 | 5 | use App\Models\HR\Employee; |
6 | 6 | use App\Models\HR\Expense; |
7 | 7 | use App\Models\HR\ExpenseLine; |
| 8 | +use Filament\Actions\Testing\TestAction; |
8 | 9 | use Filament\Forms\Components\Repeater; |
9 | 10 | use Illuminate\Support\Str; |
10 | 11 | use Livewire\Livewire; |
|
45 | 46 | Livewire::test(EditExpense::class, ['record' => $record->getRouteKey()]) |
46 | 47 | ->fillForm([ |
47 | 48 | 'description' => 'Updated description', |
| 49 | + 'total_amount' => 999.99, |
48 | 50 | ]) |
49 | 51 | ->call('save') |
50 | 52 | ->assertHasNoFormErrors() |
|
54 | 56 | expect((float) $record->total_amount)->toBe(100.00); |
55 | 57 | }); |
56 | 58 |
|
| 59 | +it('updates the unsaved total when lines change', function () { |
| 60 | + $undoRepeaterFake = Repeater::fake(); |
| 61 | + |
| 62 | + $record = Expense::factory()->for(Employee::factory(), 'employee')->create([ |
| 63 | + 'total_amount' => 1802.35, |
| 64 | + ]); |
| 65 | + $lines = $record->expenseLines()->createMany([ |
| 66 | + ['description' => 'Travel', 'quantity' => 5, 'unit_price' => 352.97, 'amount' => 1764.85, 'date' => now()], |
| 67 | + ['description' => 'Meals', 'quantity' => 3, 'unit_price' => 12.50, 'amount' => 37.50, 'date' => now()], |
| 68 | + ]); |
| 69 | + $firstLineKey = 'record-' . $lines[0]->getKey(); |
| 70 | + $secondLineKey = 'record-' . $lines[1]->getKey(); |
| 71 | + |
| 72 | + Livewire::test(EditExpense::class, ['record' => $record->getRouteKey()]) |
| 73 | + ->fillForm(["expenseLines.{$firstLineKey}.quantity" => 2]) |
| 74 | + ->assertSchemaStateSet(['total_amount' => '743.44']) |
| 75 | + ->fillForm(["expenseLines.{$secondLineKey}.unit_price" => 20.25]) |
| 76 | + ->assertSchemaStateSet(['total_amount' => '766.69']) |
| 77 | + ->callAction(TestAction::make('delete')->schemaComponent('expenseLines')->arguments(['item' => $firstLineKey])) |
| 78 | + ->assertSchemaStateSet(['total_amount' => '60.75']) |
| 79 | + ->callAction(TestAction::make('delete')->schemaComponent('expenseLines')->arguments(['item' => $secondLineKey])) |
| 80 | + ->assertSchemaStateSet(['total_amount' => '0.00']) |
| 81 | + ->callAction(TestAction::make('add')->schemaComponent('expenseLines')) |
| 82 | + ->fillForm([ |
| 83 | + 'expenseLines.0.description' => 'Taxi', |
| 84 | + 'expenseLines.0.quantity' => 2, |
| 85 | + 'expenseLines.0.unit_price' => 4.75, |
| 86 | + ]) |
| 87 | + ->assertSchemaStateSet(['total_amount' => '9.50']); |
| 88 | + |
| 89 | + expect((float) $record->fresh()->total_amount)->toBe(1802.35); |
| 90 | + expect($lines[0]->fresh()->quantity)->toBe(5); |
| 91 | + |
| 92 | + $undoRepeaterFake(); |
| 93 | +}); |
| 94 | + |
57 | 95 | it('validates the form data', function (array $data, array $errors) { |
58 | 96 | $record = Expense::factory()->for(Employee::factory(), 'employee')->has(ExpenseLine::factory()->count(1), 'expenseLines')->create(); |
59 | 97 |
|
|
0 commit comments