Skip to content

Fixes #39439 - replace AuditsPage tests with RTL testing#11032

Merged
Lukshio merged 1 commit into
theforeman:developfrom
andreilakatos:fixes-39439-audits-page-test
Jul 1, 2026
Merged

Fixes #39439 - replace AuditsPage tests with RTL testing#11032
Lukshio merged 1 commit into
theforeman:developfrom
andreilakatos:fixes-39439-audits-page-test

Conversation

@andreilakatos

@andreilakatos andreilakatos commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #39439

Summary

Replaces brittle snapshot-based tests for AuditsPage with React Testing Library (RTL) behavioral tests, and fixes the shared renderWithStoreAndI18n / renderWithI18n helpers so they can be used as the standard test setup going forward.

Changes

AuditsPage tests (AuditsPage.test.js)

  • Removed snapshot tests and the 311-line AuditsPage.test.js.snap file.
  • Added RTL tests that assert on rendered output instead of component trees:
    • Happy path with audit rows, documentation link, search input, and host data
    • Empty data state (no audit rows, data list still present)
    • Loading state (no data list, no error)
    • Error state with a generic message
    • Error state with a "no audits" message (restores coverage dropped during the migration)
  • Uses rtlHelpers.renderWithStoreAndI18n() for Redux + i18n wrapping.
  • Centralizes the i18n readiness wait in renderAuditsPage() via await intl.ready, so individual tests can use synchronous RTL queries afterward.

Fixtures (AuditsPage.fixtures.js)

  • Adds default audits and itemCount to auditsPageProps, since they are required by AuditsTable and should not need to be repeated in every test.

RTL test helpers (rtlTestHelpers.js)

  • Fixes renderWithI18n() and renderWithStoreAndI18n(), which previously treated i18nProviderWrapperFactory() as a React component:

    // Before (broken)
    const IntlWrapper = i18nProviderWrapperFactory(mockDate, timezone);
    React.createElement(IntlWrapper, {}, component);

@github-actions github-actions Bot added the UI label Jun 19, 2026
@andreilakatos andreilakatos force-pushed the fixes-39439-audits-page-test branch 2 times, most recently from e5b2e10 to 679a598 Compare June 19, 2026 16:02
@ofedoren

Copy link
Copy Markdown
Member

Review: Fixes #39439 - replace AuditsPage tests with RTL testing

Good direction — snapshot tests were brittle and the 311-line snap file was pure noise. A few things to address:

Use rtlHelpers.renderWithStoreAndI18n() instead of manual i18n setup

The current approach:

const I18nProvider = i18nProviderWrapperFactory(new Date(), 'UTC')(
  ({ children }) => children
);

works accidentally — i18nProviderWrapperFactory returns an HOC, not a provider. It also introduces an async intl.ready gate, which is why every test needs await screen.findByText('Audits') even though AuditsPage renders synchronously.

rtlHelpers.renderWithStoreAndI18n() already handles both Redux and i18n wrapping and is the established pattern in this codebase. Using it would simplify renderAuditsPage and likely remove the need for async queries.

Prefer RTL queries over document.getElementById

expect(document.getElementById('audits-empty-table')).toBeInTheDocument();

AuditsLoadingPage only renders a <div id="..."> with skeletons inside — no text, roles, or labels — so there's no clean RTL query available. Two options:

  1. Add data-testid="audits-loading" to AuditsLoadingPage and use screen.getByTestId()
  2. Assert the absence of the data list instead: expect(screen.queryByRole('list', { name: 'Audits data list' })).not.toBeInTheDocument() (which the test already does)

Option 2 requires no component changes.

Dropped test scenario

The old "render audits page w/empty audits" fixture was { hasError: true, message: 'no audits' } — an error state. The new "renders audits page with no audit rows" test uses { hasError: false, hasData: true, audits: [], itemCount: 0 } — a legitimate empty-data state, which is a different withRenderHandler branch. Both are worth testing; the error-with-"no audits" case is currently missing.

Consider adding audits and itemCount to the default fixture

auditsPageProps lacks audits and itemCount, which are required props for AuditsTable. The happy-path test adds them manually, but the loading and error tests rely on withRenderHandler short-circuiting before AuditsTable mounts. Adding sensible defaults to auditsPageProps would make the tests less coupled to the HOC branching order.


Generated by Claude Opus 4.6

@andreilakatos

Copy link
Copy Markdown
Contributor Author

Done @ofedoren
Had to fix renderWithStoreAndI18n though.

@ofedoren

Copy link
Copy Markdown
Member

Re-review after update

All four points from the previous review are addressed — renderWithStoreAndI18n adopted, document.getElementById removed, dropped error scenario restored, defaults added to fixture. The rtlTestHelpers.js fix is also correct — the old renderWithI18n and renderWithStoreAndI18n were passing the HOC function directly to React.createElement instead of calling it with a component first. Two remaining items:

intl.ready handling should move into renderWithStoreAndI18n

The test imports intl directly and wraps every render with:

await act(async () => {
  await intl.ready;
});

Since renderWithStoreAndI18n now correctly uses the i18n wrapper (which gates on intl.ready internally), this async wait is a concern of the helper, not of each individual test. Moving the act + intl.ready await into renderWithStoreAndI18n (and making it async) would prevent every future test from having to repeat this boilerplate.

Two error tests cover the same code path

'renders audits page with empty audits error' and 'renders audits page with error message' both pass { hasError: true, message: { type: 'error', text: '...' } } — they hit the same withRenderHandler branch and the same EmptyPage rendering with different text strings. One of these is enough; consider dropping the duplicate and keeping the one with the more descriptive name.


Generated by Claude Opus 4.6

@Lukshio

Lukshio commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

lgtm from my side, any objections @ofedoren ?

@Lukshio Lukshio requested a review from ofedoren June 29, 2026 15:23

@MariaAga MariaAga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tests look good to me, @andreilakatos Can you squash please?

@andreilakatos andreilakatos force-pushed the fixes-39439-audits-page-test branch from d70e773 to 6cf5676 Compare July 1, 2026 07:06
@andreilakatos andreilakatos force-pushed the fixes-39439-audits-page-test branch from 6cf5676 to 242edad Compare July 1, 2026 09:25

@Lukshio Lukshio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@Lukshio Lukshio merged commit 0ab58ce into theforeman:develop Jul 1, 2026
40 checks passed
@Lukshio

Lukshio commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Thanks @andreilakatos

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants