Skip to content

test: add unit tests for GeneralCompliantComponent #2018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { IconProp } from '@fortawesome/fontawesome-svg-core'
import { faCertificate } from '@fortawesome/free-solid-svg-icons'
import { render, screen } from '@testing-library/react'
import React from 'react'

import '@testing-library/jest-dom'

// Mock the Tooltip component to prevent async state update warnings
jest.mock('@heroui/tooltip', () => ({
Tooltip: ({ children, content }: { children: React.ReactNode; content: string }) => (
<div data-testid="tooltip" title={content}>
{children}
</div>
),
}))

import GeneralCompliantComponent from 'components/GeneralCompliantComponent'

type GeneralCompliantComponentProps = {
compliant: boolean
icon: IconProp
title: string
}

describe('GeneralCompliantComponent', () => {
const baseProps: GeneralCompliantComponentProps = {
compliant: true,
icon: faCertificate,
title: 'Test Title',
}

it('renders successfully with minimal required props', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} />)
expect(container).toBeInTheDocument()
})

it('applies correct color for compliant=true', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={true} />)
const svg = container.querySelector('svg')
expect(svg).toBeInTheDocument()
expect(svg).toHaveClass('text-green-400/80')
})

it('applies correct color for compliant=false', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} compliant={false} />)
const svg = container.querySelector('svg')
expect(svg).toBeInTheDocument()
expect(svg).toHaveClass('text-red-400/80')
})

it('renders the correct icon structure', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} />)
const icons = container.querySelectorAll('svg')
expect(icons).toHaveLength(2)
})

it('renders tooltip wrapper with title attribute', () => {
render(<GeneralCompliantComponent {...baseProps} title="Tooltip Title" />)
const tooltip = screen.getByTestId('tooltip')
expect(tooltip).toBeInTheDocument()
expect(tooltip).toHaveAttribute('title', 'Tooltip Title')
})

it('handles edge case: empty title', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />)
expect(container).toBeInTheDocument()
})
Comment on lines +64 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Edge case: empty title should define behavior (tooltip rendered or not?)

Currently this test only checks that render doesn’t crash. Consider asserting the intended behavior: either no tooltip wrapper is rendered for empty title, or it is rendered with an empty title. Pick one based on the component’s spec and enforce it.

-  const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />)
-  expect(container).toBeInTheDocument()
+  render(<GeneralCompliantComponent {...baseProps} title="" />)
+  // If empty title means "no tooltip":
+  expect(screen.queryByTestId('tooltip')).not.toBeInTheDocument()
+  // Otherwise (if it should be rendered), assert the wrapper exists and has an empty title:
+  // const tooltip = screen.getByTestId('tooltip')
+  // expect(tooltip).toHaveAttribute('title', '')

To confirm the intended behavior in code, you can run:


🏁 Script executed:

#!/bin/bash
# Inspect how Tooltip is used in the component to determine if it's conditional on title.
fd --hidden --strip-cwd-prefix --full-path 'GeneralCompliantComponent' -t f \
  | xargs -I {} rg -n -C3 -e 'Tooltip' -e 'title' {}

Length of output: 2228


Enforce tooltip wrapper for empty title

Update the test at frontend/__tests__/unit/components/GeneralCompliantComponent.test.tsx to assert that the tooltip wrapper is rendered with an empty title attribute when title="":

 it('handles edge case: empty title', () => {
-  const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />)
-  expect(container).toBeInTheDocument()
+  render(<GeneralCompliantComponent {...baseProps} title="" />)
+  const tooltip = screen.getByTestId('tooltip')
+  expect(tooltip).toBeInTheDocument()
+  expect(tooltip).toHaveAttribute('title', '')
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('handles edge case: empty title', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} title="" />)
expect(container).toBeInTheDocument()
})
it('handles edge case: empty title', () => {
render(<GeneralCompliantComponent {...baseProps} title="" />)
const tooltip = screen.getByTestId('tooltip')
expect(tooltip).toBeInTheDocument()
expect(tooltip).toHaveAttribute('title', '')
})
🤖 Prompt for AI Agents
In frontend/__tests__/unit/components/GeneralCompliantComponent.test.tsx around
lines 64 to 67, the test only checks that the component renders when title=""
but does not assert the tooltip wrapper is present or that it carries an empty
title attribute; update the test to query the tooltip wrapper element (the
component's tooltip wrapper element or the element that receives the title
attribute) and add an assertion that its title attribute strictly equals an
empty string (title === "") to enforce the tooltip wrapper is rendered with an
empty title when title="".


it('has accessible SVG icons', () => {
const { container } = render(<GeneralCompliantComponent {...baseProps} />)
const icons = container.querySelectorAll('svg[role="img"]')
expect(icons).toHaveLength(2)
expect(icons[0]).toHaveAttribute('aria-hidden', 'true')
expect(icons[1]).toHaveAttribute('aria-hidden', 'true')
})

it('renders with custom icon', () => {
const customIcon = faCertificate
const { container } = render(<GeneralCompliantComponent {...baseProps} icon={customIcon} />)
expect(container.querySelector('svg')).toBeInTheDocument()
})
})