Skip to content

Conversation

gkbishnoi07
Copy link
Collaborator

@gkbishnoi07 gkbishnoi07 commented Oct 5, 2025

Description

Add Integration Tests for Create-Client.Component.ts file

Related issues and discussion

WEB-341

Summary by CodeRabbit

  • Tests

    • Added comprehensive integration tests for client creation covering validation, data aggregation, edge cases, and full submission flow.
    • Introduced a unified Jest test setup with environment and browser-API mocks, expanded test discovery to include .test files, and switched typings to Jest.
  • Chores

    • Standardized Jest configuration with explicit options for coverage, transforms, module resolution, and test matching.
    • Aligned test scripts for local and CI workflows and upgraded test tooling (ts-node) for compatibility.

Copy link

coderabbitai bot commented Oct 5, 2025

Walkthrough

Centralizes and explicitizes Jest configuration, adds a global Angular/Jest setup file, adjusts TypeScript testing paths/types, updates npm test scripts and ts-node, and introduces an extensive integration test suite for CreateClientComponent.

Changes

Cohort / File(s) Summary
Jest configuration overhaul
jest.config.ts
Replaces commented defaults with explicit config: preset, transform (jest-preset-angular + tsconfig.spec.json), transformIgnorePatterns, collectCoverageFrom, moduleNameMapper, moduleDirectories, moduleFileExtensions, testMatch, testEnvironment, globalSetup, setupFilesAfterEnv.
Jest runtime setup for Angular
src/setup-jest.ts
New setup file invoking jest-preset-angular setupZoneTestEnv and adding globals/mocks for window.getComputedStyle, localStorage/sessionStorage, matchMedia, CSS.supports, and window.env.
TypeScript test config updates
src/tsconfig.spec.json
Adjusts baseUrl, adds path aliases (@pipes/*, app/*), switches test types to jest, and includes **/*.test.ts alongside **/*.spec.ts.
NPM scripts and ts-node upgrade
package.json
Test scripts now point to --config jest.config.ts (including watch/coverage/ci variants); ts-node bumped to ^10.9.2 in deps/devDeps.
CreateClientComponent integration tests
src/app/clients/create-client/create-client.component.spec.ts
Adds a comprehensive integration test suite: TestBed setup with TranslateModule and routing, mocked ClientsService/SettingsService, BehaviorSubject-driven ActivatedRoute data, many scenarios covering initialization, validation, datatable interactions, submission, navigation, and edge cases.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor U as User
  participant AR as ActivatedRoute
  participant C as CreateClientComponent
  participant SS as SettingsService
  participant CS as ClientsService
  participant R as Router

  note right of AR #DFF2E1: Route-resolved data provided\n(clientTemplate, addressConfig)
  AR-->>C: provide resolved data
  SS-->>C: getSettings() -> locale/dateFormat

  U->>C: fill forms (general, family, address, datatables)
  C->>C: validate sections & datatables
  alt valid
    C->>CS: createClient(aggregatedPayload)
    CS-->>C: createdClient (id)
    C->>R: navigate to ['clients', id]
  else invalid
    C->>U: display validation errors
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly summarizes the primary change by stating the addition of integration tests for the CreateClientComponent and directly matches the changes in the PR.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

@gkbishnoi07 gkbishnoi07 force-pushed the WEB-341-add-integration-tests-for-create-client-component branch from a918073 to 7a1bcbc Compare October 6, 2025 06:06
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/app/clients/create-client/create-client.component.spec.ts (1)

58-110: Trigger change detection during setup

Right after creating the component we never call fixture.detectChanges(), so Angular lifecycle hooks (e.g., ngOnInit) stay dormant. The suite currently leans on constructor state, but any future move of init logic into lifecycle hooks will quietly break these tests. Please run an initial fixture.detectChanges() in the shared beforeEach to keep the tests aligned with real runtime behavior. As per coding guidelines.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a918073 and 7a1bcbc.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json and included by **/*
📒 Files selected for processing (5)
  • jest.config.ts (1 hunks)
  • package.json (2 hunks)
  • src/app/clients/create-client/create-client.component.spec.ts (1 hunks)
  • src/setup-jest.ts (1 hunks)
  • src/tsconfig.spec.json (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/app/**

⚙️ CodeRabbit configuration file

src/app/**: For Angular code: verify component separation, trackBy on *ngFor,
strict type safety, and clean observable patterns.

Files:

  • src/app/clients/create-client/create-client.component.spec.ts
**/*.spec.ts

⚙️ CodeRabbit configuration file

**/*.spec.ts: For tests: encourage clear Arrange-Act-Assert structure,
stable selectors, and minimal brittle timing dependencies.

Files:

  • src/app/clients/create-client/create-client.component.spec.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Run Lint, Build and Deploy

Comment on lines +13 to +29
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
length: 0,
key: jest.fn()
};

Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});

// Mock sessionStorage
Object.defineProperty(window, 'sessionStorage', {
value: localStorageMock
});
Copy link

@coderabbitai coderabbitai bot Oct 6, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Separate the storage mocks to mirror browser behavior

Both localStorage and sessionStorage are wired to the same backing object, so writes to one instantly appear in the other. That diverges from the browser contract and can mask regressions when code relies on the storages being isolated. Please create independent mocks (or factory per storage) before defining them on window.

-const localStorageMock = {
-  getItem: jest.fn(),
-  setItem: jest.fn(),
-  removeItem: jest.fn(),
-  clear: jest.fn(),
-  length: 0,
-  key: jest.fn()
-};
+const createStorageMock = () => ({
+  getItem: jest.fn(),
+  setItem: jest.fn(),
+  removeItem: jest.fn(),
+  clear: jest.fn(),
+  length: 0,
+  key: jest.fn()
+});
+
+const localStorageMock = createStorageMock();
 
 Object.defineProperty(window, 'localStorage', {
-  value: localStorageMock
+  value: localStorageMock
 });
 
-// Mock sessionStorage
-Object.defineProperty(window, 'sessionStorage', {
-  value: localStorageMock
-});
+const sessionStorageMock = createStorageMock();
+
+Object.defineProperty(window, 'sessionStorage', {
+  value: sessionStorageMock
+});
📝 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
const localStorageMock = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
length: 0,
key: jest.fn()
};
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
// Mock sessionStorage
Object.defineProperty(window, 'sessionStorage', {
value: localStorageMock
});
const createStorageMock = () => ({
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
length: 0,
key: jest.fn()
});
const localStorageMock = createStorageMock();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
const sessionStorageMock = createStorageMock();
Object.defineProperty(window, 'sessionStorage', {
value: sessionStorageMock
});
🤖 Prompt for AI Agents
In src/setup-jest.ts around lines 13 to 29, both localStorage and sessionStorage
are assigned the same mock object so mutations to one affect the other; create
independent mocks (or call a mock factory twice) so each storage has its own
getItem/setItem/removeItem/clear/length/key implementations and then define them
separately on window to mirror real browser isolation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@coderabbitai is this a problem or just a suggestion?

Copy link

Choose a reason for hiding this comment

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

@gkbishnoi07, it's a potential problem that can manifest as an actual bug in specific scenarios. Let me explain:

Why it's problematic:

Since both storages point to the same mock object, they share:

  • Mock call history: If you verify localStorage.setItem was called, you can't distinguish whether it was actually localStorage or sessionStorage
  • Spies and implementations: Any jest.spyOn() or mock implementation set on one affects the other
  • Mock function state: Call counts, return values, and implementations are shared

When it causes real issues:

  1. Tests that use both storages: If your component uses both localStorage and sessionStorage, your tests won't catch bugs where the wrong storage is used
  2. Mock verification: When asserting expect(localStorage.setItem).toHaveBeenCalledWith(...), calls to sessionStorage.setItem will also count
  3. Different implementations: If you need different behavior for each storage in a test, you can't achieve that

When it might be acceptable:

  • If your current tests only interact with one storage at a time
  • If you never verify mock calls on these storages
  • If your components don't rely on the isolation between storages

Recommendation:

Given that the fix is simple (just create independent mocks), I recommend applying it to prevent future headaches. The suggested change ensures your test environment accurately mirrors browser behavior, making your tests more reliable and easier to reason about.

Would you like to apply the fix, or do you have specific reasons to keep them shared?

@gkbishnoi07
Copy link
Collaborator Author

@IOhacker @alberto-art3ch @steinwinde please review my test file

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.

1 participant