Testing & Performance
This page covers testing strategies for Redux/RTK Query applications and performance optimization techniques.
Testing Redux Slices
Basic Reducer Tests
// user.slice.test.ts
import reducer, { userLoggedIn, userLoggedOut } from './user.slice';
describe('user slice', () => {
const initialState = {
account: null,
isAuthenticated: false,
};
it('should handle userLoggedIn', () => {
const account = '0x123...';
const actual = reducer(initialState, userLoggedIn({ account }));
expect(actual.account).toBe(account);
expect(actual.isAuthenticated).toBe(true);
});
it('should handle userLoggedOut', () => {
const loggedInState = {
account: '0x123...',
isAuthenticated: true,
};
const actual = reducer(loggedInState, userLoggedOut());
expect(actual.account).toBeNull();
expect(actual.isAuthenticated).toBe(false);
});
});Testing Entity Adapters
Testing Selectors
Simple Selectors
Memoized Selectors
Testing RTK Query with MSW
Setup MSW
Testing Queries
Testing Mutations
Testing Optimistic Updates
Performance Optimization
Use selectFromResult to Prevent Re-renders
selectFromResult to Prevent Re-rendersAvoid Selecting Entire State
Memoize Expensive Selectors
Use Entity Adapters for Normalized Data
Tune RTK Query Cache Settings
Prefetch for Better UX
Polling Strategy
Redux DevTools
Enable in Development
Action Sanitizer
Sanitize sensitive data in DevTools:
Best Practices Checklist
Performance
Testing
Code Quality
Anti-Patterns to Avoid
Don't do these:
Store non-serializable objects (providers, signers) in Redux
Duplicate data in both slices and RTK Query
Dispatch actions during render
Create selectors that return new objects without memoization
Ignore loading and error states
Fetch the same data in multiple components without RTK Query
Over-poll or poll without
skipPollingIfUnfocused
Monitoring Performance
Track Selector Calls
Monitor Re-renders
Next Steps
Review Component Patterns for usage examples
See RTK Query for caching strategies
Understand State Management for slice optimization
Last updated