scratch-www/test/unit/redux/session.test.js

49 lines
2 KiB
JavaScript
Raw Normal View History

2021-03-23 15:18:36 -04:00
import {
getInitialState, selectIsAdmin, selectIsSocial, selectUserId,
2021-04-21 14:51:29 -04:00
selectIsLoggedIn, selectUsername, selectToken, sessionReducer, setSession
2021-03-23 15:18:36 -04:00
} from '../../../src/redux/session';
import {sessions} from '../../helpers/state-fixtures.json';
2021-03-23 15:18:36 -04:00
describe('session selectors', () => {
test('logged out', () => {
const state = {session: getInitialState()};
expect(selectIsAdmin(state)).toBe(false);
expect(selectIsSocial(state)).toBe(false);
2021-04-21 14:51:29 -04:00
expect(selectIsLoggedIn(state)).toBe(false);
expect(selectUserId(state)).toBeNaN();
expect(selectToken(state)).toBeNull();
expect(selectUsername(state)).toBeNull();
});
test('user data', () => {
const state = {session: getInitialState()};
const newSession = sessions.user1.session;
state.session = sessionReducer(state.session, setSession(newSession));
expect(selectUserId(state)).toBe(1);
expect(selectUsername(state)).toBe('user1-username');
expect(selectToken(state)).toBe('user1-token');
2021-04-21 14:51:29 -04:00
expect(selectIsLoggedIn(state)).toBe(true);
});
2021-03-23 15:18:36 -04:00
describe('permissions', () => {
test('selectIsAdmin', () => {
const state = {session: getInitialState()};
const newSession = sessions.user1Admin.session;
state.session = sessionReducer(state.session, setSession(newSession));
expect(selectIsAdmin(state)).toBe(true);
// Confirm that admin/social are totally separate and just read directly from the state
expect(selectIsSocial(state)).toBe(false);
2021-03-23 15:18:36 -04:00
});
test('selectIsSocial', () => {
const state = {session: getInitialState()};
const newSession = sessions.user1Social.session;
2021-03-23 15:18:36 -04:00
state.session = sessionReducer(state.session, setSession(newSession));
expect(selectIsSocial(state)).toBe(true);
// Confirm that admin/social are totally separate and just read directly from the state
expect(selectIsAdmin(state)).toBe(false);
2021-03-23 15:18:36 -04:00
});
});
});