mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2026-01-26 05:23:19 +00:00
✨ Add Livewire Flux components and new tests for project proposal and editing forms
This commit is contained in:
111
tests/Feature/Livewire/ProjectProposalFormTest.php
Normal file
111
tests/Feature/Livewire/ProjectProposalFormTest.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
use App\Livewire\Forms\ProjectProposalForm;
|
||||
|
||||
it('has correct validation rules for all fields', function () {
|
||||
$form = new ProjectProposalForm;
|
||||
|
||||
// Test name field - required|min:5
|
||||
$form->name = '';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
$form->name = 'short'; // Less than 5 characters
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
// Test support_in_sats field - required|numeric|min:21
|
||||
$form->name = 'Valid Project';
|
||||
$form->support_in_sats = '';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
$form->support_in_sats = 'not-numeric';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
$form->support_in_sats = '20'; // Less than 21
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
// Test description field - required|string|min:5
|
||||
$form->name = 'Valid Project';
|
||||
$form->support_in_sats = '21000';
|
||||
$form->description = '';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
$form->description = 'short';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
// Test website field - required|url
|
||||
$form->name = 'Valid Project';
|
||||
$form->support_in_sats = '21000';
|
||||
$form->description = 'Valid description';
|
||||
$form->website = 'not-a-url';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
});
|
||||
|
||||
it('accepts valid project proposal data', function () {
|
||||
$form = new ProjectProposalForm;
|
||||
|
||||
$form->name = 'Test Project';
|
||||
$form->support_in_sats = '21000';
|
||||
$form->description = 'This is a test project description that meets the minimum length requirement.';
|
||||
$form->website = 'https://example.com';
|
||||
$form->accepted = true;
|
||||
$form->sats_paid = 5000;
|
||||
|
||||
$result = $form->validate();
|
||||
expect($result)->toBeArray();
|
||||
expect($result)->toBeEmpty();
|
||||
});
|
||||
|
||||
it('validates accepted field as boolean', function () {
|
||||
$form = new ProjectProposalForm;
|
||||
$form->name = 'Valid Project';
|
||||
$form->support_in_sats = '21000';
|
||||
$form->description = 'Valid description';
|
||||
$form->website = 'https://example.com';
|
||||
|
||||
$form->accepted = 'not-boolean';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
// Test with boolean values
|
||||
$form->accepted = false;
|
||||
expect($form->accepted)->toBeBool();
|
||||
|
||||
$form->accepted = true;
|
||||
expect($form->accepted)->toBeBool();
|
||||
});
|
||||
|
||||
it('validates sats_paid as nullable numeric', function () {
|
||||
$form = new ProjectProposalForm;
|
||||
$form->name = 'Valid Project';
|
||||
$form->support_in_sats = '21000';
|
||||
$form->description = 'Valid description';
|
||||
$form->website = 'https://example.com';
|
||||
|
||||
// Test with null (should be acceptable)
|
||||
$form->sats_paid = null;
|
||||
$form->accepted = false;
|
||||
|
||||
$result = $form->validate();
|
||||
expect($result)->toBeArray();
|
||||
expect($result)->toBeEmpty();
|
||||
|
||||
// Test with numeric
|
||||
$form->sats_paid = 'not-numeric';
|
||||
expect(fn () => $form->validate())->toThrow();
|
||||
|
||||
$form->sats_paid = 10000;
|
||||
$form->accepted = false;
|
||||
$result = $form->validate();
|
||||
expect($result)->toBeArray();
|
||||
expect($result)->toBeEmpty();
|
||||
});
|
||||
|
||||
it('has correct default values', function () {
|
||||
$form = new ProjectProposalForm;
|
||||
|
||||
expect($form->name)->toBe('');
|
||||
expect($form->support_in_sats)->toBe('');
|
||||
expect($form->description)->toBe('');
|
||||
expect($form->website)->toBe('');
|
||||
expect($form->accepted)->toBeFalse();
|
||||
expect($form->sats_paid)->toBe(0);
|
||||
});
|
||||
98
tests/Feature/Livewire/ProjectSupportCreateTest.php
Normal file
98
tests/Feature/Livewire/ProjectSupportCreateTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\AssociationStatus;
|
||||
use App\Models\EinundzwanzigPleb;
|
||||
use App\Models\ProjectProposal;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Livewire;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->pleb = EinundzwanzigPleb::query()->create([
|
||||
'pubkey' => 'test_pubkey_'.Str::random(20),
|
||||
'npub' => 'test_npub_'.Str::random(20),
|
||||
'association_status' => AssociationStatus::ACTIVE->value,
|
||||
]);
|
||||
|
||||
// Create payment event for the current year
|
||||
$this->pleb->paymentEvents()->create([
|
||||
'year' => date('Y'),
|
||||
'amount' => 21000,
|
||||
'paid' => true,
|
||||
'event_id' => 'test_event_'.Str::random(40),
|
||||
]);
|
||||
});
|
||||
|
||||
it('renders create form for authorized users', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->assertStatus(200)
|
||||
->assertSee('Projektförderung anlegen')
|
||||
->assertSeeLivewire('association.project-support.form.create');
|
||||
});
|
||||
|
||||
it('does not render create form for unauthorized users', function () {
|
||||
$unauthorizedPleb = EinundzwanzigPleb::query()->create([
|
||||
'pubkey' => 'test_pubkey_'.Str::random(20),
|
||||
'npub' => 'test_npub_'.Str::random(20),
|
||||
'association_status' => AssociationStatus::DEFAULT->value,
|
||||
]);
|
||||
|
||||
Livewire::actingAs($unauthorizedPleb)
|
||||
->test('association.project-support.form.create')
|
||||
->assertSet('isAllowed', false)
|
||||
->assertDontSee('Projektförderung anlegen');
|
||||
});
|
||||
|
||||
it('validates required name field', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->set('form.name', '')
|
||||
->set('form.description', 'Test description')
|
||||
->call('save')
|
||||
->assertHasErrors(['form.name']);
|
||||
});
|
||||
|
||||
it('validates name max length', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->set('form.name', Str::random(300))
|
||||
->set('form.description', 'Test description')
|
||||
->call('save')
|
||||
->assertHasErrors(['form.name']);
|
||||
});
|
||||
|
||||
it('validates required description field', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->set('form.name', 'Test Project')
|
||||
->set('form.description', '')
|
||||
->call('save')
|
||||
->assertHasErrors(['form.description']);
|
||||
});
|
||||
|
||||
it('creates project proposal successfully', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->set('form.name', 'Test Project')
|
||||
->set('form.description', 'This is a test project for unit testing purposes.')
|
||||
->call('save')
|
||||
->assertHasNoErrors()
|
||||
->assertRedirect(route('association.projectSupport'));
|
||||
|
||||
expect(ProjectProposal::count())->toBe(1);
|
||||
$project = ProjectProposal::first();
|
||||
expect($project->name)->toBe('Test Project');
|
||||
expect($project->description)->toBe('This is a test project for unit testing purposes.');
|
||||
});
|
||||
|
||||
it('associates project proposal with current pleb', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.create')
|
||||
->set('form.name', 'Test Project')
|
||||
->set('form.description', 'Test description')
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$project = ProjectProposal::first();
|
||||
expect($project->einundzwanzig_pleb_id)->toBe($this->pleb->id);
|
||||
});
|
||||
109
tests/Feature/Livewire/ProjectSupportEditTest.php
Normal file
109
tests/Feature/Livewire/ProjectSupportEditTest.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\AssociationStatus;
|
||||
use App\Models\EinundzwanzigPleb;
|
||||
use App\Models\ProjectProposal;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Livewire;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->pleb = EinundzwanzigPleb::query()->create([
|
||||
'pubkey' => 'test_pubkey_'.Str::random(20),
|
||||
'npub' => 'test_npub_'.Str::random(20),
|
||||
'association_status' => AssociationStatus::ACTIVE->value,
|
||||
]);
|
||||
|
||||
// Create payment event for the current year
|
||||
$this->pleb->paymentEvents()->create([
|
||||
'year' => date('Y'),
|
||||
'amount' => 21000,
|
||||
'paid' => true,
|
||||
'event_id' => 'test_event_'.Str::random(40),
|
||||
]);
|
||||
|
||||
$this->project = ProjectProposal::query()->create([
|
||||
'einundzwanzig_pleb_id' => $this->pleb->id,
|
||||
'name' => 'Original Project',
|
||||
'description' => 'Original Description',
|
||||
]);
|
||||
|
||||
// Get board member pubkeys from config
|
||||
$boardPubkeys = config('einundzwanzig.config.current_board', []);
|
||||
$this->boardMember = EinundzwanzigPleb::query()->create([
|
||||
'pubkey' => 'board_pubkey_'.Str::random(20),
|
||||
'npub' => 'board_npub_'.Str::random(20),
|
||||
'association_status' => AssociationStatus::HONORARY->value,
|
||||
]);
|
||||
|
||||
// Simulate board member by temporarily updating config for testing
|
||||
config(['einundzwanzig.config.current_board' => [$this->boardMember->npub]]);
|
||||
});
|
||||
|
||||
it('renders edit form for authorized project owners', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->assertStatus(200)
|
||||
->assertSee('Projektförderung bearbeiten')
|
||||
->assertSet('form.name', $this->project->name)
|
||||
->assertSet('form.description', $this->project->description);
|
||||
});
|
||||
|
||||
it('renders edit form for board members', function () {
|
||||
Livewire::actingAs($this->boardMember)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->assertStatus(200)
|
||||
->assertSee('Projektförderung bearbeiten');
|
||||
});
|
||||
|
||||
it('does not render edit form for unauthorized users', function () {
|
||||
$unauthorizedPleb = EinundzwanzigPleb::query()->create([
|
||||
'pubkey' => 'test_pubkey_'.Str::random(20),
|
||||
'npub' => 'test_npub_'.Str::random(20),
|
||||
'association_status' => AssociationStatus::ACTIVE->value,
|
||||
]);
|
||||
|
||||
Livewire::actingAs($unauthorizedPleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->assertSet('isAllowed', false);
|
||||
});
|
||||
|
||||
it('validates required name field', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->set('form.name', '')
|
||||
->set('form.description', 'Test description')
|
||||
->call('update')
|
||||
->assertHasErrors(['form.name']);
|
||||
});
|
||||
|
||||
it('validates required description field', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->set('form.name', 'Test Project')
|
||||
->set('form.description', '')
|
||||
->call('update')
|
||||
->assertHasErrors(['form.description']);
|
||||
});
|
||||
|
||||
it('updates project proposal successfully', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->set('form.name', 'Updated Name')
|
||||
->set('form.description', 'Updated Description')
|
||||
->call('update')
|
||||
->assertHasNoErrors()
|
||||
->assertRedirect(route('association.projectSupport.item', $this->project));
|
||||
|
||||
$this->project->refresh();
|
||||
expect($this->project->name)->toBe('Updated Name');
|
||||
expect($this->project->description)->toBe('Updated Description');
|
||||
});
|
||||
|
||||
it('disables update button during save', function () {
|
||||
Livewire::actingAs($this->pleb)
|
||||
->test('association.project-support.form.edit', ['project' => $this->project])
|
||||
->set('form.name', 'Test')
|
||||
->set('form.description', 'Test')
|
||||
->call('update')
|
||||
->assertSeeHtml('wire:loading');
|
||||
});
|
||||
Reference in New Issue
Block a user