employee = User::factory()->create(); $this->actingAs($this->employee); $this->job = CustomerJobModel::factory()->create(['status' => 'order', 'responsible_user_id' => $this->employee->id]); $workType = WorkTypeModel::query()->where('name', 'graafinen työ')->sole(); $this->task = TaskModel::factory()->create(['customer_job_id' => $this->job->id, 'work_type_id' => $workType->id, 'title' => 'Mainoskuvat']); $this->params = ['jobId' => $this->job->id, 'taskId' => $this->task->id, 'taskTitle' => 'Mainoskuvat']; }); /** * @return list */ function checklistTexts(int $taskId): array { return ChecklistItemModel::query()->where('task_id', $taskId)->orderBy('sort_order')->orderBy('id')->pluck('text')->all(); } it('shows the checklist with progress on the job page', function () { ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1, 'done_at' => now(), 'done_by' => $this->employee->id]); ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Mikit', 'sort_order' => 2]); $this->get(route('jobs.show', $this->job->id)) ->assertOk() ->assertSee('Tarkistuslista') ->assertSee('(1/2 tehty)') ->assertSeeInOrder(['Valot', 'Mikit']); }); it('adds, checks, renames, moves and removes checklist items', function () { $component = Livewire::test('jobs::task-checklist', $this->params) ->set('newItem', ' Valot ') ->call('add') ->assertHasNoErrors() ->assertSet('newItem', '') ->assertSee('Kohta Valot lisätty.') ->set('newItem', 'Mikit') ->call('add'); [$lights, $mics] = ChecklistItemModel::query()->orderBy('id')->pluck('id')->all(); $component->call('check', $lights, true) ->assertSee('Kohta Valot merkitty tehdyksi.') ->assertSee('(1/2 tehty)'); expect(ChecklistItemModel::query()->find($lights)->done_by)->toBe($this->employee->id); $component->call('startRename', $lights) ->assertSet('editingItemId', $lights) ->set('editingText', 'Valot ja jalustat') ->call('saveRename') ->assertSee('Kohta Valot ja jalustat tallennettu.') ->assertSet('editingItemId', null) ->call('move', $mics, 'up') ->assertSee('Kohta Mikit siirretty ylöspäin.') ->call('move', $mics, 'up') ->assertSee('Kohta Mikit on jo ensimmäisenä.'); expect(checklistTexts($this->task->id))->toBe(['Mikit', 'Valot ja jalustat']); $component->call('remove', $mics)->assertSee('Kohta Mikit poistettu.'); expect(checklistTexts($this->task->id))->toBe(['Valot ja jalustat']); }); it('shows Finnish errors for empty and too long items', function () { Livewire::test('jobs::task-checklist', $this->params) ->call('add') ->assertHasErrors('newItem') ->assertSee('Anna kohdan teksti.') ->set('newItem', str_repeat('a', 256)) ->call('add') ->assertSee('Kohdan teksti voi olla enintään 255 merkkiä.'); expect(ChecklistItemModel::query()->count())->toBe(0); }); it('does not change items of another task through this one', function () { $other = ChecklistItemModel::query()->create(['task_id' => TaskModel::factory()->create()->id, 'text' => 'Vieras', 'sort_order' => 1]); Livewire::test('jobs::task-checklist', $this->params) ->call('remove', $other->id) ->assertSee('Kohtaa ei löytynyt. Päivitä sivu.'); expect($other->fresh())->not->toBeNull(); }); it('lets an attached subcontractor check items but not manage them', function () { $item = ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]); $subcontractor = User::factory()->subcontractor()->create(); TaskModel::factory()->assignedTo([$subcontractor->id])->create(['customer_job_id' => $this->job->id, 'work_type_id' => $this->task->work_type_id]); $this->actingAs($subcontractor); Livewire::test('jobs::task-checklist', $this->params) ->assertDontSee('Lisää kohta') ->call('check', $item->id, true) ->assertSee('Kohta Valot merkitty tehdyksi.') ->call('remove', $item->id) ->assertForbidden(); expect($item->fresh()->done_by)->toBe($subcontractor->id); }); it('forbids the checklist of a job the subcontractor is not attached to', function () { $this->actingAs(User::factory()->subcontractor()->create()); Livewire::test('jobs::task-checklist', $this->params)->assertForbidden(); }); it('removes the checklist with its task', function () { ChecklistItemModel::query()->create(['task_id' => $this->task->id, 'text' => 'Valot', 'sort_order' => 1]); $this->task->delete(); expect(ChecklistItemModel::query()->count())->toBe(0); });