Skip to content

Commit

Permalink
Added tests for redux slices
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarley7 committed Apr 30, 2023
1 parent 4201660 commit f4be833
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions client/test/unitTests/Util/Store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import menuReducer, { openMenu, closeMenu } from 'store/menu.slice';
import authReducer, { setUserName } from 'store/auth.slice';

describe('reducers', () => {
let initialState: {
menu: {
isOpen: boolean;
};
auth: {
userName: string | undefined;
};
};

beforeEach(() => {
initialState = {
menu: {
isOpen: false,
},
auth: {
userName: undefined,
},
};
});

describe('menu reducer', () => {
const itShouldHandleMenuActions = (
actionCreator: () => { type: string },
expectedValue: boolean
) => {
const newState = menuReducer(initialState.menu, actionCreator());
expect(newState.isOpen).toBe(expectedValue);
};

it('menuReducer should return the initial menu state when an unknown action type is passed with an undefined state', () => {
expect(menuReducer(undefined, { type: 'unknown' })).toEqual(
initialState.menu
);
});

it('should handle openMenu', () => {
itShouldHandleMenuActions(openMenu, true);
});

it('should handle closeMenu', () => {
initialState.menu.isOpen = true;
itShouldHandleMenuActions(closeMenu, false);
});
});

describe('auth reducer', () => {
it('authReducer should return the initial menu state when an unknown action type is passed with an undefined state', () => {
expect(authReducer(undefined, { type: 'unknown' })).toEqual(
initialState.auth
);
});

it('should handle setUserName', () => {
const newState = authReducer(initialState.auth, setUserName('user1'));
expect(newState.userName).toBe('user1');
});
});
});

0 comments on commit f4be833

Please sign in to comment.