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

279 lines
9.9 KiB
JavaScript
Raw Permalink Normal View History

2019-08-26 15:10:59 -04:00
const {
handleToggleAccountNav,
navigationReducer,
setAccountNavOpen,
setCanceledDeletionOpen,
setLoginError,
setLoginOpen,
setRegistrationOpen,
setSearchTerm,
2019-10-07 16:20:29 -04:00
toggleLoginOpen,
handleRegistrationRequested
2019-08-26 15:10:59 -04:00
} = require('../../../src/redux/navigation');
describe('unit test lib/validate.js', () => {
2024-02-26 19:13:36 -05:00
const realLocation = global.window.location;
2019-10-07 16:20:29 -04:00
beforeEach(() => {
2019-10-18 11:54:34 -04:00
// mock window navigation
2024-02-26 19:13:36 -05:00
// see https://stackoverflow.com/q/46169824
delete global.window.location;
global.window.location = {
...realLocation,
assign: jest.fn()
};
2019-10-07 16:20:29 -04:00
});
2019-08-26 15:10:59 -04:00
test('initialState', () => {
let defaultState;
/* navigationReducer(state, action) */
expect(navigationReducer(defaultState, {type: 'anything'})).toBeDefined();
expect(navigationReducer(defaultState, {type: 'anything'}).accountNavOpen).toBe(false);
expect(navigationReducer(defaultState, {type: 'anything'}).canceledDeletionOpen).toBe(false);
expect(navigationReducer(defaultState, {type: 'anything'}).loginError).toBe(null);
expect(navigationReducer(defaultState, {type: 'anything'}).loginOpen).toBe(false);
expect(navigationReducer(defaultState, {type: 'anything'}).registrationOpen).toBe(false);
expect(navigationReducer(defaultState, {type: 'anything'}).searchTerm).toBe('');
2019-11-15 11:28:10 -05:00
expect(navigationReducer(defaultState, {type: 'anything'}).useScratch3Registration).toBe(true);
2019-08-26 15:10:59 -04:00
});
// handleToggleAccountNav
test('handleToggleAccountNav can toggle on', () => {
const initialState = {
accountNavOpen: false
};
const action = handleToggleAccountNav();
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(true);
});
test('handleToggleAccountNav can toggle off', () => {
const initialState = {
accountNavOpen: true
};
const action = handleToggleAccountNav();
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(false);
});
// setAccountNavOpen
test('setAccountNavOpen opens account nav, if it is closed', () => {
const initialState = {
accountNavOpen: false
};
const action = setAccountNavOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(true);
});
test('setAccountNavOpen leaves account nav open, if it is already open', () => {
const initialState = {
accountNavOpen: true
};
const action = setAccountNavOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(true);
});
test('setAccountNavOpen closes account nav, if it is open', () => {
const initialState = {
accountNavOpen: true
};
const action = setAccountNavOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(false);
});
test('setAccountNavOpen leaves account nav closed, if it is already closed', () => {
const initialState = {
accountNavOpen: false
};
const action = setAccountNavOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.accountNavOpen).toBe(false);
});
// setCanceledDeletionOpen
test('setCanceledDeletionOpen opens account nav, if it is closed', () => {
const initialState = {
canceledDeletionOpen: false
};
const action = setCanceledDeletionOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.canceledDeletionOpen).toBe(true);
});
test('setCanceledDeletionOpen leaves account nav open, if it is already open', () => {
const initialState = {
canceledDeletionOpen: true
};
const action = setCanceledDeletionOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.canceledDeletionOpen).toBe(true);
});
test('setCanceledDeletionOpen closes account nav, if it is open', () => {
const initialState = {
canceledDeletionOpen: true
};
const action = setCanceledDeletionOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.canceledDeletionOpen).toBe(false);
});
test('setCanceledDeletionOpen leaves account nav closed, if it is already closed', () => {
const initialState = {
canceledDeletionOpen: false
};
const action = setCanceledDeletionOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.canceledDeletionOpen).toBe(false);
});
// setLoginError
test('setLoginError sets login error', () => {
const initialState = {
loginError: null
};
const action = setLoginError('Danger! Error! Mistake!');
const resultState = navigationReducer(initialState, action);
expect(resultState.loginError).toBe('Danger! Error! Mistake!');
});
// setLoginOpen
test('setLoginOpen opens account nav, if it is closed', () => {
const initialState = {
loginOpen: false
};
const action = setLoginOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(true);
});
test('setLoginOpen leaves account nav open, if it is already open', () => {
const initialState = {
loginOpen: true
};
const action = setLoginOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(true);
});
test('setLoginOpen closes account nav, if it is open', () => {
const initialState = {
loginOpen: true
};
const action = setLoginOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(false);
});
test('setLoginOpen leaves account nav closed, if it is already closed', () => {
const initialState = {
loginOpen: false
};
const action = setLoginOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(false);
});
// setRegistrationOpen
test('setRegistrationOpen opens account nav, if it is closed', () => {
const initialState = {
registrationOpen: false
};
const action = setRegistrationOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(true);
});
test('setRegistrationOpen leaves account nav open, if it is already open', () => {
const initialState = {
registrationOpen: true
};
const action = setRegistrationOpen(true);
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(true);
});
test('setRegistrationOpen closes account nav, if it is open', () => {
const initialState = {
registrationOpen: true
};
const action = setRegistrationOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(false);
});
test('setRegistrationOpen leaves account nav closed, if it is already closed', () => {
const initialState = {
registrationOpen: false
};
const action = setRegistrationOpen(false);
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(false);
});
// setSearchTerm
test('setSearchTerm sets search term', () => {
const initialState = {
searchTerm: null
};
const action = setSearchTerm('outer space');
const resultState = navigationReducer(initialState, action);
expect(resultState.searchTerm).toBe('outer space');
});
// toggleLoginOpen
test('toggleLoginOpen can toggle on', () => {
const initialState = {
loginOpen: false
};
const action = toggleLoginOpen();
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(true);
});
test('toggleLoginOpen can toggle off', () => {
const initialState = {
loginOpen: true
};
const action = toggleLoginOpen();
const resultState = navigationReducer(initialState, action);
expect(resultState.loginOpen).toBe(false);
});
2019-10-07 16:20:29 -04:00
test('handleRegistrationRequested with useScratch3Registration true navigates user to /join, ' +
'and does NOT open scratch 2 registration', () => {
const initialState = {
registrationOpen: false,
useScratch3Registration: true
};
const action = handleRegistrationRequested();
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(false);
2019-10-18 11:54:34 -04:00
expect(global.window.location.assign).toHaveBeenCalledWith('/join');
2019-10-07 16:20:29 -04:00
});
test('handleRegistrationRequested with useScratch3Registration false does NOT navigate user away, ' +
'DOES open scratch 2 registration', () => {
const initialState = {
registrationOpen: false,
useScratch3Registration: false
};
const action = handleRegistrationRequested();
const resultState = navigationReducer(initialState, action);
expect(resultState.registrationOpen).toBe(true);
2019-10-18 11:54:34 -04:00
expect(global.window.location.assign).not.toHaveBeenCalled();
2019-10-07 16:20:29 -04:00
});
2019-08-26 15:10:59 -04:00
});