test(auth, shopping): add unit tests for services#134
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR adds Jasmine unit tests for AuthService (login success and failure, logout and token persistence) and ShoppingListService (getAllShoppingLists and addListItem), with HttpClientTestingModule setup and httpMock.verify() cleanup. ChangesCore Service Unit Tests
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
frontend/src/app/shared/services/shopping-list.service.spec.ts (2)
57-59: ⚡ Quick winAssert POST payload to lock the API contract.
Line 57-59 validate URL/method, but not request body. Add a body assertion so the test fails if DTO mapping regresses.
Suggested patch
const req = httpMock.expectOne(`${apiUrl}/1/items`); expect(req.request.method).toBe('POST'); + expect(req.request.body).toEqual(mockItemRequest); req.flush(mockItemResponse);🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@frontend/src/app/shared/services/shopping-list.service.spec.ts` around lines 57 - 59, The test currently checks the URL and method but not the POST body; after calling httpMock.expectOne(`${apiUrl}/1/items`) and before req.flush(mockItemResponse) add an assertion that req.request.body strictly equals the DTO you send in the service call (e.g., the newItem or mockItemRequest object used in the spec) so changes to the request mapping will fail the test; update the spec in shopping-list.service.spec.ts around the existing expectOne/req.sequence to assert req.request.body === expectedPayload.
22-60: ⚡ Quick winAdd error-path specs for both service methods.
Current tests only cover success responses. Add HTTP error cases (e.g., 400/500) for
getAllShoppingLists()andaddListItem()to validate behavior throughcatchError(...)in the service and improve confidence in failure handling.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@frontend/src/app/shared/services/shopping-list.service.spec.ts` around lines 22 - 60, Add unit tests that exercise the error paths for service.getAllShoppingLists() and service.addListItem(): simulate HTTP errors (e.g., 400 and 500) via httpMock.expectOne(...).flush(..., { status, statusText }) and assert the Observables emit the expected error handling behavior (subscribe error callback or transformed value returned by the service's catchError). Locate tests around the existing specs for getAllShoppingLists and addListItem and add one failing-case test for each that verifies the service's catchError logic (e.g., error message, rethrow, or fallback value) for getAllShoppingLists() and for addListItem() using the same request URLs used in the success tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@frontend/src/app/shared/services/auth.service.spec.ts`:
- Around line 12-16: Replace the inline jasmine spy "routerSpy" with the
project's shared Router mock: import the shared Router mock (e.g., mockRouter)
and use it in TestBed.configureTestingModule providers instead of creating
routerSpy, keeping AuthService in providers; remove the local
jasmine.createSpyObj call and update any references to routerSpy in the spec to
use the shared mock instance.
---
Nitpick comments:
In `@frontend/src/app/shared/services/shopping-list.service.spec.ts`:
- Around line 57-59: The test currently checks the URL and method but not the
POST body; after calling httpMock.expectOne(`${apiUrl}/1/items`) and before
req.flush(mockItemResponse) add an assertion that req.request.body strictly
equals the DTO you send in the service call (e.g., the newItem or
mockItemRequest object used in the spec) so changes to the request mapping will
fail the test; update the spec in shopping-list.service.spec.ts around the
existing expectOne/req.sequence to assert req.request.body === expectedPayload.
- Around line 22-60: Add unit tests that exercise the error paths for
service.getAllShoppingLists() and service.addListItem(): simulate HTTP errors
(e.g., 400 and 500) via httpMock.expectOne(...).flush(..., { status, statusText
}) and assert the Observables emit the expected error handling behavior
(subscribe error callback or transformed value returned by the service's
catchError). Locate tests around the existing specs for getAllShoppingLists and
addListItem and add one failing-case test for each that verifies the service's
catchError logic (e.g., error message, rethrow, or fallback value) for
getAllShoppingLists() and for addListItem() using the same request URLs used in
the success tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 030b131d-43b1-4b11-8c3b-d1923bea1a4e
📒 Files selected for processing (2)
frontend/src/app/shared/services/auth.service.spec.tsfrontend/src/app/shared/services/shopping-list.service.spec.ts
3bfafc4 to
c87de2a
Compare
|
LGTM! @varshapandiann squash commit please! |
test(auth): use shared MockRouter from shared mocks style: fix formatting issues
c87de2a to
9cde199
Compare
Closes #34
Added unit tests for AuthService and ShoppingListService to achieve 80% code coverage. Verified that all tests pass and follow project standards by running npm run test:ci, npm run lint, and npm run prettier.
Summary by CodeRabbit