Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/Livewire/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Header extends Component implements HasForms, HasActions
public $logo;
public $projects;
public $similarItems = [];
public $currentProjectId = null;

public function mount()
{
Expand Down Expand Up @@ -100,7 +101,9 @@ public function submitItemAction(): Action
->modalIcon('heroicon-o-plus-circle')
->modalWidth('3xl')
->modalSubmitActionLabel('Confirm')
->fillForm([])
->fillForm(function () {
return $this->currentProjectId ? ['project_id' => $this->currentProjectId] : [];
})
->schema(function () {
$inputs = [];

Expand Down
5 changes: 4 additions & 1 deletion app/View/Components/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ class App extends Component
public array $fontFamily;
public bool $blockRobots = false;
public bool $userNeedsToVerify = false;
public ?int $currentProjectId = null;

public function __construct(public array $breadcrumbs = [])
public function __construct(public array $breadcrumbs = [], ?int $currentProjectId = null)
{
$this->currentProjectId = $currentProjectId;

$this->projects = Project::query()
->visibleForCurrentUser()
->when(app(GeneralSettings::class)->show_projects_sidebar_without_boards === false, function ($query) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/board.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<x-app :breadcrumbs="[
['title' => $project->title, 'url' => route('projects.show', $project)],
['title' => $board->title, 'url' => '']
]">
]" :current-project-id="$project->id">
<main class="p-4 h-full flex space-x-10 mx-auto max-w-6xl">
<section class="flex-1 max-h-full overflow-y-scroll">
<livewire:project.items :project="$project" :board="$board"/>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function updateTheme() {
</div>
@endif

<livewire:header :logo="$logo" />
<livewire:header :logo="$logo" :current-project-id="$currentProjectId" />

<div class="flex mx-auto py-5 lg:space-x-10 h-full px-4 sm:px-6 md:px-8 max-w-[1500px]">
@include('partials.navbar')
Expand Down
2 changes: 1 addition & 1 deletion resources/views/project.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<x-app :breadcrumbs="[
['title' => $project->title, 'url' => route('projects.show', $project)]
]">
]" :current-project-id="$project->id">
<div
@class([
'w-full h-[calc(100vh-170px)] overflow-x-auto',
Expand Down
179 changes: 179 additions & 0 deletions tests/Feature/Livewire/HeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

use App\Models\User;
use App\Models\Project;
use App\Models\Board;
use Livewire\Livewire;
use App\Livewire\Header;
use App\Settings\GeneralSettings;
use function Pest\Laravel\actingAs;

beforeEach(function () {
GeneralSettings::fake([
'select_project_when_creating_item' => true,
'select_board_when_creating_item' => true,
'project_required_when_creating_item' => false,
'board_required_when_creating_item' => false,
'users_must_verify_email' => false,
]);
});

test('header component renders successfully', function () {
$user = createAndLoginUser();

Livewire::test(Header::class)
->assertStatus(200);
});

test('submit item action pre-fills project when currentProjectId is set', function () {
$user = createAndLoginUser();
$project = Project::factory()->create();

// When we create an item with currentProjectId set, and we don't override the project_id,
// it should use the pre-filled project_id
Livewire::test(Header::class, ['currentProjectId' => $project->id])
->callAction('submitItem', data: [
'title' => 'Test Feature with Pre-fill',
'content' => 'This should use the pre-filled project.',
// Not explicitly providing project_id - should use pre-filled value
])
->assertHasNoFormErrors();

// The item should be created with the pre-filled project
$this->assertDatabaseHas('items', [
'title' => 'Test Feature with Pre-fill',
'project_id' => $project->id,
'user_id' => $user->id,
]);
});

test('submit item action does not pre-fill project when currentProjectId is null', function () {
$user = createAndLoginUser();

$component = Livewire::test(Header::class, ['currentProjectId' => null]);

// Mount the action
$component->mountAction('submitItem');

// Assert that the project_id is not set (or is null)
$component->assertFormSet([
'project_id' => null,
]);
});

test('submit item action shows login notification for guest users', function () {
// Test without logging in
Livewire::test(Header::class)
->callAction('submitItem')
->assertNotified();
});

test('user can create item with pre-filled project', function () {
$user = createAndLoginUser();
$project = Project::factory()->create();

Livewire::test(Header::class, ['currentProjectId' => $project->id])
->callAction('submitItem', data: [
'title' => 'Test Feature Request',
'content' => 'This is a test content for the feature request.',
'project_id' => $project->id,
])
->assertHasNoFormErrors();

$this->assertDatabaseHas('items', [
'title' => 'Test Feature Request',
'content' => 'This is a test content for the feature request.',
'project_id' => $project->id,
'user_id' => $user->id,
]);
});

test('user can override pre-filled project with different project', function () {
$user = createAndLoginUser();
$project1 = Project::factory()->create();
$project2 = Project::factory()->create();

Livewire::test(Header::class, ['currentProjectId' => $project1->id])
->callAction('submitItem', data: [
'title' => 'Test Feature Request',
'content' => 'This is a test content for the feature request.',
'project_id' => $project2->id, // Override with different project
])
->assertHasNoFormErrors();

// Assert that the item was created with project2, not project1
$this->assertDatabaseHas('items', [
'title' => 'Test Feature Request',
'project_id' => $project2->id,
'user_id' => $user->id,
]);

$this->assertDatabaseMissing('items', [
'title' => 'Test Feature Request',
'project_id' => $project1->id,
]);
});

test('project field is shown when select_project_when_creating_item setting is enabled', function () {
GeneralSettings::fake([
'select_project_when_creating_item' => true,
]);

$user = createAndLoginUser();

$component = Livewire::test(Header::class);

$component->mountAction('submitItem');

// Assert that the form has the project_id field
$component->assertFormFieldExists('project_id');
});

test('project field is hidden when select_project_when_creating_item setting is disabled', function () {
GeneralSettings::fake([
'select_project_when_creating_item' => false,
]);

$user = createAndLoginUser();

$component = Livewire::test(Header::class);

$component->mountAction('submitItem');

// Assert that the form does not have the project_id field
$component->assertFormFieldDoesNotExist('project_id');
});

test('project field is required when project_required_when_creating_item is true', function () {
GeneralSettings::fake([
'select_project_when_creating_item' => true,
'project_required_when_creating_item' => true,
]);

$user = createAndLoginUser();

Livewire::test(Header::class)
->callAction('submitItem', data: [
'title' => 'Test Feature Request',
'content' => 'This is a test content for the feature request.',
// Intentionally not providing project_id
])
->assertHasFormErrors(['project_id']);
});

test('project field is optional when project_required_when_creating_item is false', function () {
GeneralSettings::fake([
'select_project_when_creating_item' => true,
'project_required_when_creating_item' => false,
]);

$user = createAndLoginUser();

Livewire::test(Header::class)
->callAction('submitItem', data: [
'title' => 'Test Feature Request',
'content' => 'This is a test content for the feature request.',
// Not providing project_id - should be valid
])
->assertHasNoFormErrors();
});
Loading