Skip to content

test: add tests for GadgetWithDataSource component - #56

Open
mrhapile wants to merge 1 commit into
inspektor-gadget:mainfrom
mrhapile:test/gadget-with-datasource
Open

test: add tests for GadgetWithDataSource component#56
mrhapile wants to merge 1 commit into
inspektor-gadget:mainfrom
mrhapile:test/gadget-with-datasource

Conversation

@mrhapile

@mrhapile mrhapile commented Mar 7, 2026

Copy link
Copy Markdown

Add tests for GadgetWithDataSource component

ref #20

This PR adds a test file for the GadgetWithDataSource component located in src/common/GadgetWithDataSource/index.tsx.

The tests verify that the component renders correctly and that it properly handles the provided data source. The tests also ensure that conditional rendering works as expected when data is present or absent.

How to use

Reviewers can validate this PR by running the test suite locally.

Steps:

  1. Checkout this branch.
  2. Install dependencies if necessary.
  3. Run the test suite.

Testing done

Commands executed:

npm install
npm test

Result:

Screenshot 2026-03-08 at 3 04 21 AM

All tests passed successfully and the new test file src/common/GadgetWithDataSource/index.test.tsx executed without errors.

Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
Copilot AI review requested due to automatic review settings March 7, 2026 21:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Vitest + React Testing Library coverage for GadgetWithDataSource to improve confidence in rendering and interaction behavior (Issue #20).

Changes:

  • Introduces a new index.test.tsx suite covering instant-run/error rendering, filter application, and start/stop behaviors.
  • Adds coverage for metric vs. table rendering paths and loading/empty-data edge cases.
  • Mocks external UI dependencies (Headlamp components, Iconify, MetricChart, GadgetFilters) to isolate component logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +4 to +5
import React from 'react';
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React and waitFor are imported but not used in this test file. Unused imports can cause lint failures and add noise—please remove them (or start using waitFor if it was intended for async assertions).

Suggested change
import React from 'react';
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';
import { render, screen, fireEvent, cleanup } from '@testing-library/react';

Copilot uses AI. Check for mistakes.
Comment on lines +284 to +295
test('renders with fallback default callbacks if not provided', () => {
const propsWithoutCallbacks = { ...defaultProps };
delete propsWithoutCallbacks.headlessGadgetDeleteCallback;
delete propsWithoutCallbacks.headlessGadgetRunCallback;
delete propsWithoutCallbacks.handleRun;

render(<GadgetWithDataSource {...propsWithoutCallbacks} gadgetInstance={{ id: '123' }} gadgetRunningStatus={true} />);
const stopBtn = screen.getByRole('button', { name: /Stop/i });
fireEvent.click(stopBtn);
// It shouldn't crash
expect(true).toBe(true);
});

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests use expect(true).toBe(true), which will pass even if the component behavior regresses. Replace with a meaningful assertion (e.g., assert the click does not throw and/or verify that the expected callback mocks were/weren’t called).

Copilot uses AI. Check for mistakes.
Comment on lines +297 to +308
test('renders with fallback default handleRun if not provided', () => {
const propsWithoutCallbacks = { ...defaultProps };
delete propsWithoutCallbacks.headlessGadgetDeleteCallback;
delete propsWithoutCallbacks.headlessGadgetRunCallback;
delete propsWithoutCallbacks.handleRun;

render(<GadgetWithDataSource {...propsWithoutCallbacks} gadgetRunningStatus={false} />);
const startBtn = screen.getByRole('button', { name: /Start/i });
fireEvent.click(startBtn);
// It shouldn't crash
expect(true).toBe(true);
});

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also uses a no-op assertion (expect(true).toBe(true)), so it won’t catch failures. Please assert observable behavior (e.g., the button remains in the document after clicking, or callback mocks are not invoked, and/or the click does not throw).

Copilot uses AI. Check for mistakes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@illume illume left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions!

Please let us know if you want to continue this by addressing the review comments? If not that's ok, we can take over the PR (and finish it or close it ourselves).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants