Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Feb 3, 2025
1 parent f14762f commit 86cd634
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/database/migrations/create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up(): void
$table->unsignedTinyInteger('rating')->default(0);
$table->json('tags')->nullable();
$table->string('title');
$table->json('json')->nullable();
$table->json('json_array_of_objects')->nullable();
$table->timestamps();
$table->softDeletes();
Expand Down
1 change: 1 addition & 0 deletions tests/database/migrations/modify_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->after('password', function (Blueprint $table) {
$table->json('json')->nullable();
$table->string('email_code_authentication_secret')->nullable();
$table->string('google_two_factor_authentication_secret')->nullable();
$table->text('google_two_factor_authentication_recovery_codes')->nullable();
Expand Down
12 changes: 12 additions & 0 deletions tests/src/Fixtures/Livewire/PostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ public function table(Table $table): Table
->state('correct state'),
Tables\Columns\TextColumn::make('formatted_state')
->formatStateUsing(fn () => 'formatted state'),
Tables\Columns\TextColumn::make('json.foo')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('json.bar.baz')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('json_array_of_objects.*.value'),
Tables\Columns\TextColumn::make('author.json.foo')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('author.json.bar.baz')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('extra_attributes')
->extraAttributes([
'class' => 'text-danger-500',
Expand Down
1 change: 1 addition & 0 deletions tests/src/Fixtures/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Post extends Model
protected $casts = [
'is_published' => 'boolean',
'tags' => 'array',
'json' => 'array',
'json_array_of_objects' => 'array',
];

Expand Down
1 change: 1 addition & 0 deletions tests/src/Fixtures/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class User extends Authenticatable implements FilamentUser, HasEmailCodeAuthenti
* @var array<string, string>
*/
protected $casts = [
'json' => 'array',
'email_verified_at' => 'datetime',
'google_two_factor_authentication_secret' => 'encrypted',
'google_two_factor_authentication_recovery_codes' => 'encrypted:array',
Expand Down
166 changes: 166 additions & 0 deletions tests/src/Tables/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use Filament\Tables\Columns\TextColumn;
use Filament\Tests\Fixtures\Livewire\PostsTable;
use Filament\Tests\Fixtures\Models\Post;
use Filament\Tests\Fixtures\Models\User;
use Filament\Tests\Tables\TestCase;
use Illuminate\Support\Str;

use function Filament\Tests\livewire;

Expand Down Expand Up @@ -44,6 +46,54 @@
->assertCanSeeTableRecords($posts->sortByDesc('author.name'), inOrder: true);
});

it('can sort records with JSON column', function () {
$posts = Post::factory()->count(10)->state(fn (): array => [
'json' => ['foo' => Str::random()],
])->create();

livewire(PostsTable::class)
->sortTable('json.foo')
->assertCanSeeTableRecords($posts->sortBy('json.foo'), inOrder: true)
->sortTable('json.foo', 'desc')
->assertCanSeeTableRecords($posts->sortByDesc('json.foo'), inOrder: true);
});

it('can sort records with nested JSON column', function () {
$posts = Post::factory()->count(10)->state(fn (): array => [
'json' => ['bar' => ['baz' => Str::random()]],
])->create();

livewire(PostsTable::class)
->sortTable('json.bar.baz')
->assertCanSeeTableRecords($posts->sortBy('json.bar.baz'), inOrder: true)
->sortTable('json.bar.baz', 'desc')
->assertCanSeeTableRecords($posts->sortByDesc('json.bar.baz'), inOrder: true);
});

it('can sort records with relationship JSON column', function () {
$posts = Post::factory()->count(10)->state(fn (): array => [
'author_id' => User::factory()->state(['json' => ['foo' => Str::random()]]),
])->create();

livewire(PostsTable::class)
->sortTable('author.json.foo')
->assertCanSeeTableRecords($posts->sortBy('author.json.foo'), inOrder: true)
->sortTable('author.json.foo', 'desc')
->assertCanSeeTableRecords($posts->sortByDesc('author.json.foo'), inOrder: true);
});

it('can sort records with relationship nested JSON column', function () {
$posts = Post::factory()->count(10)->state(fn (): array => [
'author_id' => User::factory()->state(['json' => ['bar' => ['baz' => Str::random()]]]),
])->create();

livewire(PostsTable::class)
->sortTable('author.json.bar.baz')
->assertCanSeeTableRecords($posts->sortBy('author.json.bar.baz'), inOrder: true)
->sortTable('author.json.bar.baz', 'desc')
->assertCanSeeTableRecords($posts->sortByDesc('author.json.bar.baz'), inOrder: true);
});

it('can search records', function () {
$posts = Post::factory()->count(10)->create();

Expand Down Expand Up @@ -77,6 +127,82 @@
->assertCanNotSeeTableRecords($posts->where('author.name', '!=', $author));
});

it('can search posts with JSON column', function () {
$search = Str::random();

$matchingPosts = Post::factory()->count(5)->create([
'json' => ['foo' => $search],
]);

$notMatchingPosts = Post::factory()->count(5)->create([
'json' => ['foo' => Str::random()],
]);

livewire(PostsTable::class)
->searchTable($search)
->assertCanSeeTableRecords($matchingPosts)
->assertCanNotSeeTableRecords($notMatchingPosts);
});

it('can search posts with nested JSON column', function () {
$search = Str::random();

$matchingPosts = Post::factory()->count(5)->create([
'json' => ['bar' => ['baz' => $search]],
]);

$notMatchingPosts = Post::factory()->count(5)->create([
'json' => ['bar' => ['baz' => Str::random()]],
]);

livewire(PostsTable::class)
->searchTable($search)
->assertCanSeeTableRecords($matchingPosts)
->assertCanNotSeeTableRecords($notMatchingPosts);
});

it('can search posts with relationship JSON column', function () {
$search = Str::random();

$matchingAuthor = User::factory()
->create(['json' => ['foo' => $search]]);

$matchingPosts = Post::factory()
->for($matchingAuthor, 'author')
->count(5)
->create();

$notMatchingPosts = Post::factory()
->count(5)
->create();

livewire(PostsTable::class)
->searchTable($search)
->assertCanSeeTableRecords($matchingPosts)
->assertCanNotSeeTableRecords($notMatchingPosts);
});

it('can search posts with relationship nested JSON column', function () {
$search = Str::random();

$matchingAuthor = User::factory()
->create(['json' => ['bar' => ['baz' => $search]]]);

$matchingPosts = Post::factory()
->for($matchingAuthor, 'author')
->count(5)
->create();

$notMatchingPosts = Post::factory()
->count(5)
->create();

livewire(PostsTable::class)
->searchTable($search)
->assertCanSeeTableRecords($matchingPosts)
->assertCanNotSeeTableRecords($notMatchingPosts);
});

it('can search individual column records with relationship', function () {
$posts = Post::factory()->count(10)->create();

Expand Down Expand Up @@ -141,6 +267,46 @@
->assertTableColumnFormattedStateNotSet('formatted_state', 'incorrect formatted state', $post);
});

it('can output JSON values', function () {
$post = Post::factory()->create([
'json' => ['foo' => 'bar'],
]);

livewire(PostsTable::class)
->assertTableColumnStateSet('json.foo', 'bar', $post);
});

it('can output nested JSON values', function () {
$post = Post::factory()->create([
'json' => ['bar' => ['baz' => 'qux']],
]);

livewire(PostsTable::class)
->assertTableColumnStateSet('json.bar.baz', 'qux', $post);
});

it('can output relationship JSON values', function () {
$post = Post::factory()->create([
'author_id' => User::factory()->state([
'json' => ['foo' => 'bar'],
]),
]);

livewire(PostsTable::class)
->assertTableColumnStateSet('author.json.foo', 'bar', $post);
});

it('can output relationship nested JSON values', function () {
$post = Post::factory()->create([
'author_id' => User::factory()->state([
'json' => ['bar' => ['baz' => 'qux']],
]),
]);

livewire(PostsTable::class)
->assertTableColumnStateSet('author.json.bar.baz', 'qux', $post);
});

it('can output values in a JSON array column of objects', function () {
$post = Post::factory()->create([
'json_array_of_objects' => [
Expand Down

0 comments on commit 86cd634

Please sign in to comment.