Skip to content

Commit

Permalink
add support and dashboard tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-cintra committed Oct 19, 2023
1 parent f41628c commit 69e3b9c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
41 changes: 41 additions & 0 deletions stubs/tests/Feature/Dashboard/DashboardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Inertia\Testing\AssertableInertia as Assert;
use Modules\User\Models\User;

beforeEach(function () {
$this->user = User::factory()->create();
$this->loggedRequest = $this->actingAs($this->user);
});

it('can render the dashboard page', function () {
$dashboardUrl = route(config('modular.default-logged-route'));
$response = $this->loggedRequest->get($dashboardUrl);

$response->assertStatus(200);

$response->assertInertia(
fn (Assert $page) => $page
->component('Dashboard/DashboardIndex')
->has(
'auth.user',
fn (Assert $page) => $page
->where('id', $this->user->id)
->where('name', $this->user->name)
->etc()
)
->has(
'auth.permissions',
)
->has(
'errors',
)
->has(
'flash',
2,
)
->has(
'ziggy.routes',
)
);
});
27 changes: 27 additions & 0 deletions stubs/tests/Feature/Support/EditorImageTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile;
use Modules\Support\Traits\EditorImage;

use function PHPUnit\Framework\assertFileExists;

uses(EditorImage::class);

beforeEach(function () {
(new Filesystem)->deleteDirectory(storage_path('editor-files'));
});

afterAll(function () {
(new Filesystem)->deleteDirectory(storage_path('editor-files'));
});

it('can upload an image to the default editor media file path', function () {
$file = UploadedFile::fake()->image('A nice file.jpg');

$result = $this->uploadImage($file, 'original');

expect($result)->toHaveKeys(['fileName', 'filePath', 'fileUrl', 'readableName']);
expect($result['readableName'])->toBe('A nice file');
assertFileExists($result['filePath']);
});
24 changes: 24 additions & 0 deletions stubs/tests/Feature/Support/FileNameGeneratorTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile;
use Modules\Support\Traits\FileNameGenerator;

beforeEach(function () {
(new Filesystem)->deleteDirectory(storage_path('editor-files'));
});

afterAll(function () {
(new Filesystem)->deleteDirectory(storage_path('editor-files'));
});

uses(FileNameGenerator::class);

it('can set the name of a uploaded file', function () {
$file = UploadedFile::fake()->image('A nice file.jpg');

$readableName = $this->getReadableName($file);
$fileName = $this->getFileName($file, $readableName, 'original');

expect($fileName)->toBe('a-nice-file.jpg');
});
28 changes: 28 additions & 0 deletions stubs/tests/Feature/Support/UploadFileTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Modules\Support\Traits\UploadFile;

uses(UploadFile::class);

beforeEach(function () {
(new Filesystem)->deleteDirectory(storage_path('user-files'));
});

afterAll(function () {
(new Filesystem)->deleteDirectory(storage_path('user-files'));
});

it('can handle requests without uploads', function () {
$result = $this->uploadFile('');

expect($result)->toBe([]);
});

it('can handle requests with wrong inputs', function () {
request('file', 'not an uploaded file');

$result = $this->uploadFile('file');

expect($result)->toBe([]);
});

0 comments on commit 69e3b9c

Please sign in to comment.