Skip to content

Commit

Permalink
(╯°□°)╯︵ ┻━┻
Browse files Browse the repository at this point in the history
Tests work, but the 1 test in BlogPostTest is flaky.

I tried explicitly setting the `publish` and `published_at` states for the 1 post I'm passing in to guarantee it'll be published, but it's not working...

And it's literally just for this 1 test.

In BlogIndexTest, it seems to be consistent...or is it?
  • Loading branch information
JSn1nj4 committed Jul 8, 2024
1 parent 5c4ade2 commit c5c18b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
18 changes: 18 additions & 0 deletions tests/Feature/Livewire/BlogIndexTest.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<?php

use Livewire\Volt\Volt;
use function Pest\Laravel\get;

voltMountable('blog-index');

it('renders posts that are published', function () {
$posts = createPosts(10, true);

Volt::test('blog-index')
->assertOk()
->assertSee($posts->get('title'))
->assertSee($posts->get('excerpt'))
->assertSee('Read More');

// for some reason this needs to be called after doing the actual volt test
get('/blog')
->assertOk()
->assertSeeVolt('blog-index');
});
13 changes: 12 additions & 1 deletion tests/Feature/Livewire/BlogPostTest.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<?php

voltMountable('blog-post', static fn () => ['post' => createPost(true)]);
use Livewire\Volt\Volt;

it('can be mounted', function () {
$post = createPost(true);

expect($post->published)
->toBeTrue('Testing "published" field')
->and($post->published_at)
->toBeInstanceOf(\Carbon\Carbon::class, 'Testing "published_at" field');

Volt::test('blog-post', compact('post'))->assertOk();
});
19 changes: 14 additions & 5 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\Post;
use App\Models\Project;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Livewire\Livewire;
use Livewire\Volt\Volt;
Expand Down Expand Up @@ -54,13 +55,21 @@ function createImage(): Image
return Image::factory()->createOne();
}

function createPost(bool $publish = false): Post
function createPost(bool|null $publish = null): Post|null
{
$factory = Post::factory();
return createPosts(1, $publish)->first();
}

function createPosts(int $count = 1, bool|null $publish = null): Collection|null
{
$factory = Post::factory()->count($count);

if ($publish !== null) $factory->state([
'publish' => $publish,
'published_at' => $publish ? fake()->dateTime() : null,
]);

if ($publish) $factory->state(['published' => true]);

return $factory->createOne();
return $factory->create();
}


Expand Down

0 comments on commit c5c18b1

Please sign in to comment.