Overview
All examples in this guide will be written using Pest. To use Pest’s Livewire plugin for testing, you can follow the installation instructions in the Pest documentation on plugins: Livewire plugin for Pest. However, you can easily adapt this to PHPUnit. Since the Form Builder works on Livewire components, you can use the Livewire testing helpers. However, we have custom testing helpers that you can use with forms:Filling a form
To fill a form with data, pass the data tofillForm():
If you have multiple forms on a Livewire component, you can specify which form you want to fill using fillForm([...], 'createPostForm').
To check that a form has data, use assertFormSet():
If you have multiple forms on a Livewire component, you can specify which form you want to check using assertFormSet([...], 'createPostForm').
You may also find it useful to pass a function to the assertFormSet() method, which allows you to access the form $state and perform additional assertions:
Validation
UseassertHasFormErrors() to ensure that data is properly validated in a form:
assertHasNoFormErrors() to ensure there are no validation errors:
If you have multiple forms on a Livewire component, you can pass the name of a specific form as the second parameter likeassertHasFormErrors(['title' => 'required'], 'createPostForm')orassertHasNoFormErrors([], 'createPostForm').
Form existence
To check that a Livewire component has a form, useassertFormExists():
If you have multiple forms on a Livewire component, you can pass the name of a specific form like assertFormExists('createPostForm').
Fields
To ensure that a form has a given field, pass the field name toassertFormFieldExists():
assertFormFieldDoesNotExist():
If you have multiple forms on a Livewire component, you can specify which form you want to check for the existence of the field like assertFormFieldExists('title', 'createPostForm').
Hidden fields
To ensure that a field is visible, pass the name toassertFormFieldIsVisible():
assertFormFieldIsHidden():
For bothassertFormFieldIsHidden()andassertFormFieldIsVisible()you can pass the name of a specific form the field belongs to as the second argument likeassertFormFieldIsHidden('title', 'createPostForm').
Disabled fields
To ensure that a field is enabled, pass the name toassertFormFieldIsEnabled():
assertFormFieldIsDisabled():
For bothassertFormFieldIsEnabled()andassertFormFieldIsDisabled()you can pass the name of a specific form the field belongs to as the second argument likeassertFormFieldIsEnabled('title', 'createPostForm').
Layout components
If you need to check if a particular layout component exists rather than a field, you may useassertFormComponentExists(). As layout components do not have names, this method uses the key() provided by the developer:
assertFormComponentDoesNotExist():
assertFormComponentExists(), returning true or false if the component passes the test or not:
Wizard
To go to a wizard’s next step, usegoToNextWizardStep():
goToPreviousWizardStep():
goToWizardStep(), then the assertWizardCurrentStep method which can ensure you are on the desired step without validation errors from the previous:
formName parameter:
Actions
You can call an action by passing its form component name, and then the name of the action tocallFormComponentAction():
data parameter:
setFormComponentActionData():
Execution
To check if an action has been halted, you can useassertFormComponentActionHalted():
Errors
assertHasNoFormComponentActionErrors() is used to assert that no validation errors occurred when submitting the action form.
To check if a validation error has occurred with the data, use assertHasFormComponentActionErrors(), similar to assertHasErrors() in Livewire:
assertFormComponentActionDataSet() method:
Action state
To ensure that an action exists or doesn’t in a form, you can use theassertFormComponentActionExists() or assertFormComponentActionDoesNotExist() method:
assertFormComponentActionHidden() or assertFormComponentActionVisible() methods:
assertFormComponentActionEnabled() or assertFormComponentActionDisabled() methods:
assertFormComponentActionHidden() method:
Button appearance
To ensure an action has the correct label, you can useassertFormComponentActionHasLabel() and assertFormComponentActionDoesNotHaveLabel():
assertFormComponentActionHasIcon() or assertFormComponentActionDoesNotHaveIcon():
assertFormComponentActionHasColor() or assertFormComponentActionDoesNotHaveColor():
URL
To ensure an action has the correct URL, you can useassertFormComponentActionHasUrl(), assertFormComponentActionDoesNotHaveUrl(), assertFormComponentActionShouldOpenUrlInNewTab(), and assertFormComponentActionShouldNotOpenUrlInNewTab():