Skip to content

test(frontend): comprehensive snapshot tests for all screens - #64

Merged
hoangsonww merged 1 commit into
masterfrom
feat/frontend-snapshot-tests
Jun 13, 2026
Merged

hoangsonww merged 1 commit into
masterfrom
feat/frontend-snapshot-tests

Conversation

@hoangsonww

Copy link
Copy Markdown
Owner

What

The frontend had behavioral tests but no snapshot coverage. This adds Jest + React Testing Library snapshot tests for all 17 screens:

LandingPage, Login, Register, ResetPassword, VerifyUsername, Dashboard, EmployeeList, EmployeeForm, DepartmentList, DepartmentForm, NewDepartmentForm, Profile, Passkeys, NotFoundPage, QuickActions, Navbar, Footer.

Each test renders the screen inside a MemoryRouter with its services mocked and asserts the rendered markup with toMatchSnapshot() — so unintended UI changes surface as a diff. Tests live in frontend/__tests__/snapshots/.

Harness

jest.setup.js gains jsdom polyfills (matchMedia, scrollTo, scrollIntoView, Resize/IntersectionObserver) and a global autoFocus neutralization (jsdom applies focus inconsistently across environments). No config or dependency changes otherwise.

Determinism — passes identically on local + CI regardless of when/where

  • LandingPage — WebGL 3D hero stubbed.
  • Dashboard — react-chartjs-2 stubbed (charts need a canvas/GPU).
  • Footer — Date frozen (renders the current year).
  • Data screens — services mocked as pending, so they render their deterministic loading state offline (no async act races).

Verification

Test Suites: 25 passed, 25 total   (17 snapshot + 8 existing behavioral)
Tests:       37 passed, 37 total
Snapshots:   17 passed, 17 total

Snapshot suites verified against the committed baselines under UTC, Pacific/Kiritimati (UTC+14), and Pacific/Pago_Pago (UTC-11) — all pass. The 8 existing behavioral suites still pass with the updated setup.

Docs updated: root README.md and frontend/README.md testing sections now document the snapshot suites and how to run/update them. No new dependencies.

🤖 Generated with Claude Code

The frontend had behavioral tests but no snapshot coverage. Add Jest +
React Testing Library snapshot tests for every screen (17): LandingPage,
Login, Register, ResetPassword, VerifyUsername, Dashboard, EmployeeList,
EmployeeForm, DepartmentList, DepartmentForm, NewDepartmentForm, Profile,
Passkeys, NotFoundPage, QuickActions, Navbar, and Footer.

Each test renders the screen inside a MemoryRouter with its services
mocked and asserts the rendered markup with toMatchSnapshot(), so
unintended UI changes surface as a diff.

Harness (jest.setup.js): add jsdom polyfills (matchMedia, scrollTo,
scrollIntoView, Resize/IntersectionObserver) and neutralize autoFocus so
focus-sensitive markup is stable across environments.

Determinism (passes identically on local + CI regardless of when/where):
- LandingPage: stub the WebGL 3D hero background.
- Dashboard: stub react-chartjs-2 (charts need a canvas/GPU).
- Footer: freeze Date (renders the current year).
- Data screens: services mocked as pending so they render their
  deterministic loading state offline (no async act races).

Verified: full suite 25 suites / 37 tests / 17 snapshots green in CI mode
(the existing 8 behavioral suites still pass), and the snapshot suites pass
against the committed baselines under UTC, Pacific/Kiritimati (UTC+14), and
Pacific/Pago_Pago (UTC-11). No new dependencies.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 13, 2026 07:14
@netlify

netlify Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for employee-management-fullstack-system ready!

Name Link
🔨 Latest commit b373a61
🔍 Latest deploy log https://app.netlify.com/projects/employee-management-fullstack-system/deploys/6a2d034caf68640008684d07
😎 Deploy Preview https://deploy-preview-64--employee-management-fullstack-system.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 35
Accessibility: 96
Best Practices: 100
SEO: 100
PWA: 80
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@vercel

vercel Bot commented Jun 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
employee-management-fullstack-app Ignored Ignored Jun 13, 2026 7:14am

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hoangsonww hoangsonww self-assigned this Jun 13, 2026
@hoangsonww hoangsonww added bug Something isn't working documentation Improvements or additions to documentation enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers dependencies Pull requests that update a dependency file labels Jun 13, 2026
@hoangsonww hoangsonww added this to the v2.x.x - Stable Release milestone Jun 13, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces comprehensive frontend snapshot testing using Jest and React Testing Library, adding snapshot suites and baseline files for all major screens, updating README files with instructions, and configuring global jsdom polyfills in 'jest.setup.js'. The review feedback recommends three key improvements: conditionally mocking 'HTMLElement.prototype.focus' in 'jest.setup.js' to prevent polluting behavioral tests, using Jest's native fake timers and system time APIs in the Footer snapshot test instead of manually mutating the global 'Date' class, and mocking 'authService' in the Employee and Department list snapshot tests to ensure determinism.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread frontend/jest.setup.js
Comment on lines +26 to +30
// autoFocus toggles MUI focus classes, and jsdom applies it inconsistently
// across environments (focuses locally but not on CI), which would make
// focus-sensitive snapshots flaky. Neutralize focus so the rendered markup
// is the unfocused state everywhere.
window.HTMLElement.prototype.focus = () => {};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Neutralizing HTMLElement.prototype.focus globally for all tests can cause silent failures or unexpected behaviors in behavioral tests that rely on focus state or user interactions (e.g., checking expect(element).toHaveFocus() or using userEvent which triggers focus events).

To prevent test pollution and keep behavioral tests working correctly, we can conditionally apply this mock only for snapshot tests by checking the current test file path using expect.getState().testPath.

    // autoFocus toggles MUI focus classes, and jsdom applies it inconsistently
    // across environments (focuses locally but not on CI), which would make
    // focus-sensitive snapshots flaky. Neutralize focus so the rendered markup
    // is the unfocused state everywhere.
    const originalFocus = window.HTMLElement.prototype.focus;
    beforeEach(() => {
      const testPath = expect.getState().testPath;
      if (testPath && testPath.includes('.snapshot.test.')) {
        window.HTMLElement.prototype.focus = () => {};
      } else {
        window.HTMLElement.prototype.focus = originalFocus;
      }
    });

Comment on lines +8 to +24
const RealDate = Date;
const FIXED_ISO = '2024-06-15T12:00:00.000Z';

beforeAll(() => {
global.Date = class extends RealDate {
constructor(...args) {
super(...(args.length ? args : [FIXED_ISO]));
}
static now() {
return new RealDate(FIXED_ISO).getTime();
}
};
});

afterAll(() => {
global.Date = RealDate;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Manually overriding the global Date class can be risky and may lead to subtle bugs or prototype pollution if not cleaned up perfectly.

Since you are using Jest 27, you can use the built-in, standard, and safe jest.useFakeTimers() and jest.setSystemTime() APIs to freeze time deterministically without mutating the global Date constructor.

beforeAll(() => {
  jest.useFakeTimers();
  jest.setSystemTime(new Date('2024-06-15T12:00:00.000Z'));
});

afterAll(() => {
  jest.useRealTimers();
});

Comment on lines +12 to +18
jest.mock('../../src/services/departmentService', () => ({
getAllDepartments: () => new Promise(() => {}),
getDepartmentById: () => new Promise(() => {}),
addDepartment: jest.fn(),
updateDepartment: jest.fn(),
deleteDepartment: jest.fn(),
}));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Unlike Dashboard.snapshot.test.jsx and Profile.snapshot.test.jsx, this test suite does not mock authService.

To ensure test determinism, consistency, and to prevent potential flakiness or dependency on the real authService implementation (which might access localStorage or make network calls), please mock authService here as well. The same applies to DepartmentList.snapshot.test.jsx.

jest.mock('../../src/services/departmentService', () => ({
  getAllDepartments: () => new Promise(() => {}),
  getDepartmentById: () => new Promise(() => {}),
  addDepartment: jest.fn(),
  updateDepartment: jest.fn(),
  deleteDepartment: jest.fn(),
}));
jest.mock('../../src/services/authService', () => ({
  getToken: () => null,
  getUsername: () => '',
  isAuthenticated: () => false,
  setSession: jest.fn(),
  clearSession: jest.fn(),
  subscribeAuth: () => () => {},
}));

@hoangsonww
hoangsonww merged commit 991aced into master Jun 13, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Projects

Development

Successfully merging this pull request may close these issues.

2 participants