Problem
When testing components that useuseCatMode() (or any context hook):
- Components throw “hook called outside provider” errors
- Tests need to wrap component in provider
- Setting up localStorage before test adds ceremony
- Easy to forget to test both modes (cat and pro)
Solution: Wrapper Helper + localStorage Setup
Pattern: renderWithContext()
Usage in Tests
Testing the Context Itself
Snapshot Testing (Optional)
For visual components, capture both modes:Cleanup Strategy
Always clear localStorage inbeforeEach:
Extending to Other Contexts
This pattern works for any context-dependent component:What to Test
✅ Do test:- Context provides correct default value
- Context reads from localStorage
- Context updates localStorage on toggle
- Component renders correct labels in both modes
- Toggle button changes the mode
- React internals (Context implementation is tested by React)
- localStorage implementation itself
- Individual label string values (that’s business logic, not testing concern)
SMO1 Test Results
- 5 tests in
cat-mode.test.tsx(context logic) - 5 tests in
scoring-chips.test.tsx(cat labels) - 8 tests in
cat-status-badge.test.tsx(pro labels + emojis) - 18 total tests, all passing
A2A Prompt Template
When continuing work on SMO1 or similar multi-modal UI projects:“I’m testing React components that depend on a Context hook (This template accelerates future context-dependent component testing across all projects.useCatMode()). The context providesisCatModeboolean and persists to localStorage. Create a helper functionrenderWithCatMode()that:Then write tests that verify both cat mode and professional mode labels render correctly.”
- Sets localStorage
appearance-cat-modebefore rendering- Wraps component in CatModeProvider
- Accepts an option
{ catMode: boolean }to set the mode- Returns standard RTL render result