Skip to content

Commit

Permalink
fix: Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 19, 2024
1 parent dadc10d commit 9fa513b
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"pestphp/pest-plugin-laravel": "^2.0",
"rector/rector": "^1.1",
"roave/security-advisories": "dev-latest",
"spatie/laravel-error-share": "^1.0",
"tightenco/duster": "^3.0"
},
"autoload": {
Expand Down
144 changes: 143 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function validateFeedback(): void
/**
* Sends the feedback request to the external service.
*
* @return \Illuminate\Http\Client\Response
* @return Illuminate\Http\Client\Response
*/
private function sendFeedbackRequest(): \Illuminate\Http\Client\Response
{
Expand Down Expand Up @@ -169,8 +169,8 @@ private function handleFeedbackResponse(\Illuminate\Http\Client\Response $respon
/**
* Handles unsuccessful responses from the feedback service.
*
* @param \Illuminate\Http\Client\Response $response
* @throws \RuntimeException
* @param Illuminate\Http\Client\Response $response
* @throws RuntimeException
*/
private function handleUnsuccessfulResponse(\Illuminate\Http\Client\Response $response): void
{
Expand Down
96 changes: 96 additions & 0 deletions tests/Feature/Profile/ExperimentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Pennant\Feature;
use Livewire\Volt\Volt;

beforeEach(function (): void {
Expand All @@ -29,3 +30,98 @@
->assertRedirect('login');
$this->assertGuest();
});

test('experiments are displayed correctly', function (): void {
Feature::define('test-experiment', true);

Volt::test('profile.experiments-manager')
->assertSee('Feature Experiments')
->assertSee('Test experiment');
});

test('experiment details can be viewed', function (): void {
Feature::define('test-experiment', true);

Volt::test('profile.experiments-manager')
->call('viewExperimentDetails', 'test-experiment')
->assertDispatched('open-modal', 'experiment-details')
->assertSee('Test experiment');
});

test('experiments can be toggled', function (): void {
Feature::define('test-experiment', true);

Volt::test('profile.experiments-manager')
->call('toggleExperiment', 'test-experiment')
->assertDispatched('experiment-toggled');

$this->assertFalse(Feature::active('test-experiment'));

Volt::test('profile.experiments-manager')
->call('toggleExperiment', 'test-experiment')
->assertDispatched('experiment-toggled');

$this->assertTrue(Feature::active('test-experiment'));
});

test('feedback modal can be opened', function (): void {
Feature::define('test-experiment', true);

Volt::test('profile.experiments-manager')
->call('viewExperimentDetails', 'test-experiment')
->call('openFeedbackModal')
->assertSet('showFeedbackModal', true)
->assertDispatched('close-modal', 'experiment-details')
->assertDispatched('open-modal', 'experiment-feedback');
});

test('feedback can be submitted', function (): void {
Feature::define('test-experiment', true);

Http::fake([
'https://feedback.vanguardbackup.com/api/feedback' => Http::response(['status' => 'success', 'message' => 'Feedback submitted successfully'], 200),
]);

Volt::test('profile.experiments-manager')
->set('selectedExperiment', 'test-experiment')
->set('feedbackText', 'This is a test feedback')
->set('feedbackEmail', '[email protected]')
->call('submitFeedback')
->assertSet('showFeedbackModal', false)
->assertDispatched('close-modal', 'experiment-feedback')
->assertDispatched('open-modal', 'experiment-details');
});

test('feedback submission handles validation errors', function (): void {
Feature::define('test-experiment', true);

Http::fake([
'https://feedback.vanguardbackup.com/api/feedback' => Http::response(['errors' => ['feedbackText' => ['The feedback text field is required.']]], 422),
]);

Volt::test('profile.experiments-manager')
->set('selectedExperiment', 'test-experiment')
->set('feedbackText', '')
->call('submitFeedback')
->assertHasErrors(['feedbackText' => 'required']);
});

test('feedback modal can be closed', function (): void {
Volt::test('profile.experiments-manager')
->set('showFeedbackModal', true)
->call('closeFeedbackModal')
->assertSet('showFeedbackModal', false)
->assertDispatched('close-modal', 'experiment-feedback')
->assertDispatched('open-modal', 'experiment-details');
});

test('experiment details are correctly populated', function (): void {
Feature::define('test-experiment', true);

$testable = Volt::test('profile.experiments-manager');

$testable->call('viewExperimentDetails', 'test-experiment');

$selectedExperiment = $testable->get('selectedExperiment');
$this->assertEquals('test-experiment', $selectedExperiment);
});

0 comments on commit 9fa513b

Please sign in to comment.