Pest Test and sending invalid $data to form?

So I have
$data = [
'name' => fake()->name,
'email' => fake()->email,
'phone' => fake()->numberBetween(2000000000, 9999999999),
'role' => $userToCreateRole,
];

$result = Livewire::test(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->callTableAction('create_housing_locator', data: $data);

if ($shouldPass) {
$result->assertHasNoErrors();
$user = User::where('email', $data['email'])->first();
expect($user)->not->toBeNull()
->and($user->name)->toBe($data['name'])
->and($user->role)->toBe($data['role']);
} else {
$user = User::where('email', $data['email'])->first();
expect($user)->toBeNull();
}
$data = [
'name' => fake()->name,
'email' => fake()->email,
'phone' => fake()->numberBetween(2000000000, 9999999999),
'role' => $userToCreateRole,
];

$result = Livewire::test(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->callTableAction('create_housing_locator', data: $data);

if ($shouldPass) {
$result->assertHasNoErrors();
$user = User::where('email', $data['email'])->first();
expect($user)->not->toBeNull()
->and($user->name)->toBe($data['name'])
->and($user->role)->toBe($data['role']);
} else {
$user = User::where('email', $data['email'])->first();
expect($user)->toBeNull();
}
. right now i get the error ErrorException: Attempt to read property "mountedTableActions" on null if i try to pass a role value that doesnt exist in the Select::make('role') field. This worked fine with livewire 2 and earlier pests as i would just check after the fact if the user exists. Right now it fails with that error. How should i instead test this properly. There isn't a test helper method to see what options are in the Select is there? This is actually multi check test there im testing about 6 different roles variations to make sure certain roles can or cannot create other users with role X, etc.
8 Replies
Mark Chaney
Mark Chaney5mo ago
well i think this might be the final pest test i have to figure out
test('test', function ($userFactoryState, $userToCreateRole, $shouldPass) {
/* @phpstan-ignore-next-line */
login(User::factory()->$userFactoryState()->create());

$data = [
'name' => fake()->name,
'email' => fake()->email,
'phone' => fake()->numberBetween(2000000000, 9999999999),
'role' => $userToCreateRole,
];

$component = livewire(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->callTableAction('create_housing_locator', data: $data);

if ($shouldPass) {
$component->assertHasNoErrors();
} else {
$component->assertHasErrors();
}

})->with([
['housingLocatorManager', 'Housing Locator Staff', true],
// this role should not exist in the options for the user to select
['housingLocatorManager', 'Housing Locator Admin', false],
]);
test('test', function ($userFactoryState, $userToCreateRole, $shouldPass) {
/* @phpstan-ignore-next-line */
login(User::factory()->$userFactoryState()->create());

$data = [
'name' => fake()->name,
'email' => fake()->email,
'phone' => fake()->numberBetween(2000000000, 9999999999),
'role' => $userToCreateRole,
];

$component = livewire(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->callTableAction('create_housing_locator', data: $data);

if ($shouldPass) {
$component->assertHasNoErrors();
} else {
$component->assertHasErrors();
}

})->with([
['housingLocatorManager', 'Housing Locator Staff', true],
// this role should not exist in the options for the user to select
['housingLocatorManager', 'Housing Locator Admin', false],
]);
ErrorException: Attempt to read property "mountedTableActions" on null
Tests: 1 failed, 1 passed (12 assertions)
Duration: 4.64s
ErrorException: Attempt to read property "mountedTableActions" on null
Tests: 1 failed, 1 passed (12 assertions)
Duration: 4.64s
so the first passes as it should, the second one fails. When an Action is loading a form, i dont really see a way to test much in that form. You pretty much have to send good data or else you cant test it.
Dan Harrin
Dan Harrin5mo ago
its like Livewire is crashing and removing itself from the test i dont know if i understand the point of the test though?
Mark Chaney
Mark Chaney5mo ago
Well is there a way to test that a role value doesn’t exist the select options when the form is in a modal? Right now I’m just sending a select option in data that doesn’t exist and expecting it not the submit, worked with alpha
Dan Harrin
Dan Harrin5mo ago
even if it was in the options, the user could still submit it via js so can you try and replicate that and see what might be happening to livewire when you select an option that doesnt exist i dont understand how the error is related, otherwise
Mark Chaney
Mark Chaney5mo ago
@Dan Harrin well i guess the key thing that would solve most of this would be a way to test forms in a modal. Right now thats not possible, correct? You can send the data to it as shown above, but you cant do any of the typical form tests with individual fields or even really assertSee() with a form that is launched in a modal, right?
Dan Harrin
Dan Harrin5mo ago
it theoretically is no different, as long as you mount the modal assertSee should work if you mount it mountTableAction(...) mount is different to call, since call will execute the action and close the modal, mount will just open it
Mark Chaney
Mark Chaney5mo ago
@Dan Harrin but $component->assertSee() then would on the whole usertable and not just the modal from what i can see when i do
$component = livewire(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->mountTableAction('create_housing_locator')
->assertSee('Housing Locator Staff');
$component = livewire(UsersTable::class)
->assertTableActionExists('create_housing_locator')
->mountTableAction('create_housing_locator')
->assertSee('Housing Locator Staff');
. How can I isolate just to the modal? Right now I am just testing first to see if it can find something
Dan Harrin
Dan Harrin5mo ago
you could assertSeeHtml on the <option tag maybe?
Want results from more Discord servers?
Add your server
More Posts
White screen when in SPA modeIs anyone experiencing a random white screen issue on Safari when running in SPA mode? I dont see anCustom requiresConfirmation() dialog in table ActionCan someone help me with this? And also I want the confirmation button to be customed. When user clhow to customize fi-sidebar-nav-groupsTo reduce the spacing between the NavigationGroup's label and regular navigation items's label, I neImplement Pending Approval Page For Newly Signed up UsersHi guys, I have been working diligently on the implementation of a feature that will display a pendiControlling edit access to resource form based on column dataI have an resources, i wanna limit edit access to data, the condition is if column a in that data trI'm trying to add a download button in a extended Custom FieldHello, I'm trying to add a download button for a media that use Spatie Media Library, I have the funHow to make table update after a livewire variable update outside of filamentI have a filament table inside a livewire component, with a search input and a button with wire:clicBringing and Saving Previous Selections When Editing Page with 'Select' FieldHello good afternoon, I needed help On one of my screens I have a Select field where your options Display TextColumn based on model functionWhat is the best apporach for displaying a TextColumn in my table that references a function on my mAction in panels::sidebar.footer opens modal in sidebar instead of center of screenHi all I've added an action to the bottom of the sidebar using the `panels::sidebar.footer` render