Skip to content

Commit af9e9b5

Browse files
authored
πŸ› fix(tests): tighten ComplianceScoreBreakdownModal test scoping (kubestellar#7908) (kubestellar#7909)
Two Copilot review comments on merged PR kubestellar#7905: 1. **Line 400** β€” `getAllByText('Kubescape').length >= 2` / `getAllByText('Kyverno').length >= 2` are too loose. Any extra rendering of those names anywhere in the modal would satisfy the assertion, and the check doesn't actually verify the per-tool bars exist. Replaced with a scoped lookup via the "By tool" heading: grab the section container and use `within(section).getByText(...)` so the assertion only accepts matches inside the per-tool list. 2. **Line 512** β€” `getByText('100')` is a broad assertion; any element rendering "100" anywhere (score %, count, label) would pass. Replaced with a value-label pairing check: locate the "Total Checks" label, walk up to its StatBox parent, and assert "100" appears inside that StatBox. Added `within` to the @testing-library/react import. All 12 tests in the file continue to pass locally; `npm run build` green. Fixes kubestellar#7908 Signed-off-by: Andy Anderson <andy@clubanderson.com>
1 parent a51b693 commit af9e9b5

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

β€Žweb/src/components/cards/ComplianceCards.test.tsxβ€Ž

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { describe, it, expect, vi, beforeEach } from 'vitest'
9-
import { render, screen} from '@testing-library/react'
9+
import { render, screen, within } from '@testing-library/react'
1010
import userEvent from '@testing-library/user-event'
1111
import { ComplianceScore } from './ComplianceCards'
1212
import { ComplianceScoreBreakdownModal } from './compliance/ComplianceScoreBreakdownModal'
@@ -394,11 +394,15 @@ describe('ComplianceScoreBreakdownModal', () => {
394394
expect(tabs[1]).toHaveTextContent('82%')
395395
expect(tabs[2]).toHaveTextContent('78%')
396396

397-
// Overview tab content: per-tool bars. "Kubescape" and "Kyverno" each appear
398-
// twice now β€” once as the tab label and once in the per-tool bar list β€” so
399-
// use getAllByText.
400-
expect(screen.getAllByText('Kubescape').length).toBeGreaterThanOrEqual(2)
401-
expect(screen.getAllByText('Kyverno').length).toBeGreaterThanOrEqual(2)
397+
// Overview tab content: per-tool bars. Scope the assertion to the "By tool"
398+
// section so it survives refactors that change other parts of the modal
399+
// (#7908 β€” Copilot review on #7905 flagged the prior getAllByText as too
400+
// broad). The `<h4>By tool</h4>` heading sits next to the bar list inside
401+
// the same parent container.
402+
const byToolHeading = screen.getByText('By tool')
403+
const byToolSection = byToolHeading.parentElement!
404+
expect(within(byToolSection).getByText('Kubescape')).toBeInTheDocument()
405+
expect(within(byToolSection).getByText('Kyverno')).toBeInTheDocument()
402406
})
403407

404408
it('renders Kubescape tab with controls stats and framework scores', async () => {
@@ -508,9 +512,13 @@ describe('ComplianceScoreBreakdownModal', () => {
508512
expect(tabs[1]).toHaveTextContent('Kubescape')
509513

510514
// Overview tab is active by default β€” it shows aggregate stats derived
511-
// from kubescapeData (100 controls / 82 passed / 18 failed).
512-
expect(screen.getByText('Total Checks')).toBeInTheDocument()
513-
expect(screen.getByText('100')).toBeInTheDocument()
515+
// from kubescapeData (100 controls / 82 passed / 18 failed). Assert the
516+
// value-label pairing inside the Total Checks StatBox rather than a bare
517+
// `getByText('100')` (#7908 β€” Copilot review on #7905 flagged the bare
518+
// match as too broad; many elements could render "100").
519+
const totalChecksLabel = screen.getByText('Total Checks')
520+
const totalChecksStatBox = totalChecksLabel.parentElement!
521+
expect(within(totalChecksStatBox).getByText('100')).toBeInTheDocument()
514522
})
515523

516524
it('does not render anything when isOpen is false', () => {

0 commit comments

Comments
Β (0)