diff --git a/app/Livewire/Dashboard/DashboardConverter.php b/app/Livewire/Dashboard/DashboardConverter.php index 16782b3..85c9ade 100644 --- a/app/Livewire/Dashboard/DashboardConverter.php +++ b/app/Livewire/Dashboard/DashboardConverter.php @@ -58,6 +58,26 @@ public function storeUpload(StoreUploadedFileAction $storeUploadedFile): void $this->step = 'format'; } + public function goToFormatStep(): void + { + if ($this->currentFile === null) { + $this->currentFileId = null; + $this->step = 'upload'; + + return; + } + + $this->step = 'format'; + } + + public function ensureValidStep(): void + { + if ($this->step === 'format' && $this->currentFile === null) { + $this->currentFileId = null; + $this->step = 'upload'; + } + } + public function replaceFile(): void { $this->resetCurrentUpload(); diff --git a/tests/Feature/Livewire/DashboardConverterTest.php b/tests/Feature/Livewire/DashboardConverterTest.php index 6f8aad8..22f67b7 100644 --- a/tests/Feature/Livewire/DashboardConverterTest.php +++ b/tests/Feature/Livewire/DashboardConverterTest.php @@ -1,6 +1,8 @@ assertSee('PNG, JPG, WEBP and PDF supported in beta') ->assertSee('Choose file'); }); + +it('does not allow format step without uploaded file', function () { + Livewire::test(DashboardConverter::class) + ->set('step', 'format') + ->call('ensureValidStep') + ->assertSet('step', 'upload'); +}); + +it('returns to upload step when current file is missing', function () { + Livewire::test(DashboardConverter::class) + ->call('goToFormatStep') + ->assertSet('step', 'upload') + ->assertSee('Drop your file here'); +}); + +it('handles a stale current file id by returning to upload step', function () { + $user = User::factory()->create(); + + Livewire::actingAs($user) + ->test(DashboardConverter::class) + ->set('currentFileId', 999999) + ->call('goToFormatStep') + ->assertSet('step', 'upload') + ->assertSet('currentFileId', null); +}); + +it('opens the format step when a current file exists', function () { + $user = User::factory()->create(); + $file = FileRecord::factory()->for($user)->create(['extension' => 'png']); + + Livewire::actingAs($user) + ->test(DashboardConverter::class) + ->set('currentFileId', $file->id) + ->call('goToFormatStep') + ->assertSet('step', 'format'); +});