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 ChaneyOP11mo 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 Harrin11mo 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 ChaneyOP11mo 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 Harrin11mo 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 ChaneyOP11mo 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 Harrin11mo 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 ChaneyOP11mo 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 Harrin11mo ago
you could assertSeeHtml on the <option tag maybe?
Want results from more Discord servers?
Add your server