Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tommyboy29's avatar

How to test forms on a resources created with --simple flag?

I have a resource EquipmentOwnersResource that was created using the command sail artisan make:filament-resource EquipmentOwner --simple. This generates a single page for the resource instead of the typical 3 and puts the create/edit form in a modal instead of loading a new page.

I've create a Pest test as follows:

it('can create equipment owners', function () {
    
    $equipmentOwner = EquipmentOwner::factory()->make();
    
    livewire(ManageEquipmentOwners::class)
        ->set('name', $equipmentOwner->name)
        ->call('save')
        ->assertHasNoFormErrors();
});

However, when running the test I get an error "Unable to call component method. Public method [save] not found on component." I know this is because I'm not using the standard create/edit pages, but the Filament docs don't have an example of testing these "simple" resources.

0 likes
4 replies
LaryAI's avatar
Level 58

To test a Livewire component that is part of a Filament resource created with the --simple flag, you need to ensure that you are calling the correct method that handles the form submission. In the case of Filament, the method might not be named save. Instead, it could be createRecord or updateRecord depending on whether you are creating or updating a record.

Here's how you can adjust your test to work with the simple Filament resource:

it('can create equipment owners', function () {
    $equipmentOwnerData = EquipmentOwner::factory()->make()->toArray();

    // Assuming that the Livewire component is named `EquipmentOwnersResource`
    // and the method to create a record is `createRecord`.
    livewire(EquipmentOwnersResource::class)
        ->set('data', $equipmentOwnerData)
        ->call('createRecord')
        ->assertHasNoErrors();

    // Optionally, you can assert that the record was created in the database.
    $this->assertDatabaseHas('equipment_owners', [
        'name' => $equipmentOwnerData['name'],
        // ... other fields
    ]);
});

Please note the following:

  1. You need to use the actual name of the Livewire component that corresponds to your EquipmentOwnersResource. Replace EquipmentOwnersResource::class with the correct class name if it's different.
  2. The ->set('data', $equipmentOwnerData) line assumes that your Livewire component uses an array called data to store the form input. Adjust this to match the actual property names used in your component.
  3. The ->call('createRecord') line assumes that the method to create a new record is named createRecord. This is a common convention in Filament, but you should verify the actual method name in your Livewire component.
  4. The ->assertHasNoErrors() method asserts that there are no validation errors. If your component uses a different method to handle errors, you may need to adjust this assertion accordingly.
  5. The assertDatabaseHas method is optional and is used to verify that the record was indeed created in the database. Make sure to include all relevant fields that you expect to be present in the database record.

If you're still encountering issues, double-check the Livewire component for the correct method names and properties, and ensure that your test is set up to interact with those correctly.

tommyboy29's avatar

@LaryAI This generates an error "Unable to call component method. Public method [createRecord] not found on component"

jamacdon's avatar

Instead of ->call you need to use ->callAction to trigger the Filament action.

The create action also requires an argument of data to save

livewire(EquipmentOwnersResource::class)
        ->call('create', $equipmentOwnerData)
        ->assertHasNoErrors();
1 like
Barbapapazes's avatar

@jamacdon This works perfectly! Thank you!

<?php

use App\Filament\Resources\EmojiResource;
use App\Models\Emoji;
use Livewire\Livewire;

it('can create', function () {
  $newData = Emoji::factory()->make();

  Livewire::test(EmojiResource\Pages\ManageEmoji::class)
    ->callAction('create', [
      'name' => $newData->name,
      'emoji' => $newData->emoji,
    ])
    ->assertHasNoErrors();

  $this->assertDatabaseHas(Emoji::class, [
    'name' => $newData->name,
    'emoji' => $newData->emoji,
  ]);
});

Please or to participate in this conversation.