| import userReducer, { setCurrentUser, addUser, removeUser, setLoading, setError } from './userSlice'; | |
| import { User } from './userSlice'; | |
| describe('userSlice', () => { | |
| const mockUser: User = { | |
| id: '1', | |
| name: 'John Doe', | |
| email: 'john@example.com', | |
| }; | |
| it('should return the initial state', () => { | |
| expect(userReducer(undefined, { type: 'unknown' })).toEqual({ | |
| currentUser: null, | |
| users: [], | |
| loading: false, | |
| error: null, | |
| }); | |
| }); | |
| it('should handle setCurrentUser', () => { | |
| const actual = userReducer(undefined, setCurrentUser(mockUser)); | |
| expect(actual.currentUser).toEqual(mockUser); | |
| }); | |
| it('should handle addUser', () => { | |
| const actual = userReducer(undefined, addUser(mockUser)); | |
| expect(actual.users).toContainEqual(mockUser); | |
| }); | |
| it('should handle removeUser', () => { | |
| const state = { | |
| currentUser: null, | |
| users: [mockUser], | |
| loading: false, | |
| error: null, | |
| }; | |
| const actual = userReducer(state, removeUser('1')); | |
| expect(actual.users).not.toContainEqual(mockUser); | |
| }); | |
| it('should handle setLoading', () => { | |
| const actual = userReducer(undefined, setLoading(true)); | |
| expect(actual.loading).toBe(true); | |
| }); | |
| it('should handle setError', () => { | |
| const errorMessage = 'An error occurred'; | |
| const actual = userReducer(undefined, setError(errorMessage)); | |
| expect(actual.error).toBe(errorMessage); | |
| }); | |
| }); | |