Skip to content

Commit

Permalink
Merge pull request #57 from musimana/feature/AdminEditPageTests
Browse files Browse the repository at this point in the history
feat: Admin Edit Page Tests
  • Loading branch information
musimana authored May 12, 2024
2 parents 84f292d + dcaf52c commit 1ef1d91
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ parameters:
- # From the way the methods are inherited by the Browser object
message: '#Call to an undefined method Laravel\\Dusk\\Browser::#'
path: tests/Browser
count: 19
count: 22
- # From the way the methods are inherited by Pest
message: '#Function testNotifiedUpdate#'
path: tests/Feature/Http/Controllers/Auth/PasswordForgottenControllerTest.php
Expand Down
33 changes: 33 additions & 0 deletions tests/Browser/Pages/Admin/Page/AdminEditPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Browser\Pages\Admin\Page;

use App\Models\Page;
use Laravel\Dusk\Browser;
use Tests\Browser\Pages\AdminPage;

final class AdminEditPage extends AdminPage
{
/** Instantiate the class. */
public function __construct(
private Page $page
) {
}

/** Get the URL for the page. */
public function url(): string
{
return $this->page->getUrlEdit();
}

/** Assert that the browser is on the page. */
public function assert(Browser $browser): void
{
$browser
->assertHasLoadedCorrectly($this->url())
->assertHasCorrectCookies()
->assertHasRenderedCorrectly($this->page->getTitle())

->pause(config('dusk.pause_length'));
}
}
30 changes: 30 additions & 0 deletions tests/Browser/Pages/AdminPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests\Browser\Pages;

use Laravel\Dusk\Browser;
use Tests\Browser\Traits\HasForm;

abstract class AdminPage extends Page
{
use HasForm;

/** Assert the page has loaded correctly. */
public function assertHasLoadedCorrectly(Browser $browser, string $url): void
{
$browser
->waitForLocation($url, config('tests.wait_length'))
->assertUrlIs($url)
->assertScript('window.__VUE__')
->assertScript('document.readyState', 'complete');
}

/** Assert the page has rendered correctly. */
public function assertHasRenderedCorrectly(Browser $browser, string $title): void
{
$browser
->assertTitle($title . ' - ' . config('app.name'))
->assertVisible('#app')
->assertVisible('main');
}
}
55 changes: 55 additions & 0 deletions tests/Browser/TestContent/Admin/PageAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Browser\TestContent\Admin;

use App\Models\Page;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Laravel\Dusk\Browser;
use PHPUnit\Framework\Assert;
use Tests\Browser\Pages\Admin\Page\AdminEditPage;
use Tests\DuskTestCase;

final class PageAdminTest extends DuskTestCase
{
use DatabaseTruncation;

/** Test the admin page edit view renders & behaves correctly. */
public function testAdminPageEdit(): void
{
$initial_array = [
'in_sitemap' => 0,
'meta_description' => 'Old meta-description.',
'meta_title' => 'Old Title',
'subtitle' => 'Old subtitle',
'title' => 'Old Title',
];

$expected_array = [
'title' => 'New Title',
'subtitle' => 'New subtitle',
'metaTitle' => 'New meta-title',
'metaDescription' => 'New meta-description.',
'inSitemap' => ['type' => 'checkbox'],
];

$page = Page::factory()->create($initial_array);
$user = User::factory()->isAdmin()->create();

Assert::assertTrue($page->title === $initial_array['title']);
Assert::assertTrue($page->subtitle === $initial_array['subtitle']);
Assert::assertTrue($page->meta_title === $initial_array['meta_title']);
Assert::assertTrue($page->meta_description === $initial_array['meta_description']);
Assert::assertFalse($page->in_sitemap);

$this->browse(fn (Browser $browser) => $browser
->loginAs($user)

->visit(new AdminEditPage($page))
->screenshotWholePage('admin-pages-edit-blank')
->completeForm('form', $expected_array)
->screenshotWholePage('admin-pages-edit-filled')
->submitForm('form')
);
}
}

0 comments on commit 1ef1d91

Please sign in to comment.