Summer Sale! All accounts are 50% off this week.

El Klo's avatar
Level 11

What should I be testing for?

I'm quite new to testing. I'm liking it so far but i'm struggling to see what i should be testing for and what not. For example: I have the following functionality in my Filament ReportResource inside of my infolist function:

Section::make('Protocol')
				       ->columnSpan(1)
				       ->icon('heroicon-o-chat-bubble-oval-left-ellipsis')
				       ->iconColor('primary')
				       ->schema([
					       livewire::make(ShowProtocolText::class),

This shows a custom livewire component which fetches some data from a relationship (client->protocols) and shows it on the component view:

class ShowProtocolText extends Component
{
	public $protocol;

	public function mount(Report $record) {
		//Fetch the current record and pass it to the address var to use in the view
		$this->protocol = $record->client->protocols->protocol
		;
	}


    public function render()
    {
        return view('livewire.show-protocol-text');
    }
}

Should i be testing if this data shows on the view? Or would that be too much?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

When it comes to testing, especially in web development, it's important to focus on testing the behavior of your application rather than its implementation details. For your ShowProtocolText Livewire component, here are some aspects you might consider testing:

  1. Component Rendering: Ensure that the component actually renders without throwing any errors.
  2. Data Loading: Verify that the correct data is being loaded by the component. This means checking if the mount method is setting the protocol property correctly.
  3. Data Display: Confirm that the data is correctly displayed in the view. This is often done by asserting that certain text or elements are present in the rendered output.
  4. User Interaction: If there are any user interactions (like buttons or forms) within the component, you should test the expected behavior when these interactions occur.

Here's an example of how you might write tests for the ShowProtocolText component using PHPUnit and Laravel's testing utilities:

class ShowProtocolTextTest extends TestCase
{
    /** @test */
    public function the_component_renders_correctly()
    {
        $report = Report::factory()->create();
        $view = Livewire::test(ShowProtocolText::class, ['record' => $report])
            ->assertStatus(200); // Assert that the component renders with an HTTP 200 status code.
    }

    /** @test */
    public function the_protocol_data_is_loaded()
    {
        $report = Report::factory()->create();
        $expectedProtocol = $report->client->protocols->protocol;

        Livewire::test(ShowProtocolText::class, ['record' => $report])
            ->assertSet('protocol', $expectedProtocol); // Assert that the 'protocol' property is set correctly.
    }

    /** @test */
    public function the_protocol_data_is_displayed_in_the_view()
    {
        $report = Report::factory()->create();
        $expectedProtocolText = $report->client->protocols->protocol;

        Livewire::test(ShowProtocolText::class, ['record' => $report])
            ->assertSee($expectedProtocolText); // Assert that the protocol text is displayed in the view.
    }
}

Remember to replace Report::factory()->create(); with the appropriate way to create a Report instance in your tests, depending on your application's setup.

Testing if the data shows on the view is not too much; it's actually a good practice to ensure that your users are seeing what they are supposed to see. However, you should avoid testing the exact HTML output unless it's critical to the functionality, as this can make your tests brittle to changes in the markup. Instead, focus on the presence and correctness of the data within the rendered view.

1 like

Please or to participate in this conversation.