Clean code and fix tests

This commit is contained in:
soupette 2019-09-10 10:37:02 +02:00
parent b7b8e368a2
commit 1e26065d95
10 changed files with 31 additions and 962 deletions

View File

@ -4,20 +4,7 @@
*
*/
import {
EMIT_EVENT,
GET_INIT_DATA,
GET_INIT_DATA_SUCCEEDED,
GET_SECURED_DATA,
GET_SECURED_DATA_SUCCEEDED,
HIDE_LEFT_MENU,
HIDE_LOGOUT,
SET_APP_ERROR,
SET_APP_SECURED,
SHOW_LEFT_MENU,
SHOW_LOGOUT,
UNSET_APP_SECURED,
} from './constants';
import { EMIT_EVENT, SET_APP_ERROR } from './constants';
export function emitEvent(event, properties) {
return {
@ -27,70 +14,8 @@ export function emitEvent(event, properties) {
};
}
export function getInitData() {
return {
type: GET_INIT_DATA,
};
}
export function getInitDataSucceeded(data) {
return {
type: GET_INIT_DATA_SUCCEEDED,
data,
};
}
export function getSecuredData() {
return {
type: GET_SECURED_DATA,
};
}
export function getSecuredDataSucceeded(data) {
return {
type: GET_SECURED_DATA_SUCCEEDED,
data,
};
}
export function hideLeftMenu() {
return {
type: HIDE_LEFT_MENU,
};
}
export function hideLogout() {
return {
type: HIDE_LOGOUT,
};
}
export function setAppError() {
return {
type: SET_APP_ERROR,
};
}
export function setAppSecured() {
return {
type: SET_APP_SECURED,
};
}
export function showLeftMenu() {
return {
type: SHOW_LEFT_MENU,
};
}
export function showLogout() {
return {
type: SHOW_LOGOUT,
};
}
export function unsetAppSecured() {
return {
type: UNSET_APP_SECURED,
};
}

View File

@ -5,16 +5,4 @@
*/
export const EMIT_EVENT = 'app/Admin/EMIT_EVENT';
export const GET_INIT_DATA = 'StrapiAdmin/Admin/GET_INIT_DATA';
export const GET_INIT_DATA_SUCCEEDED =
'StrapiAdmin/Admin/GET_INIT_DATA_SUCCEEDED';
export const GET_SECURED_DATA = 'StrapiAdmin/Admin/GET_SECURED_DATA';
export const GET_SECURED_DATA_SUCCEEDED =
'StrapiAdmin/Admin/GET_SECURED_DATA_SUCCEEDED';
export const HIDE_LEFT_MENU = 'StrapiAdmin/Admin/HIDE_LEFT_MENU';
export const HIDE_LOGOUT = 'StrapiAdmin/Admin/HIDE_LOGOUT';
export const SET_APP_ERROR = 'StrapiAdmin/Admin/SET_APP_ERROR';
export const SET_APP_SECURED = 'StrapiAdmin/Admin/SET_APP_SECURED';
export const SHOW_LEFT_MENU = 'StrapiAdmin/Admin/SHOW_LEFT_MENU';
export const SHOW_LOGOUT = 'StrapiAdmin/Admin/SHOW_LOGOUT';
export const UNSET_APP_SECURED = 'StrapiAdmin/Admin/UNSET_APP_SECURED';

View File

@ -12,11 +12,7 @@ import { bindActionCreators, compose } from 'redux';
import { Switch, Route } from 'react-router-dom';
// Components from strapi-helper-plugin
import {
LoadingIndicatorPage,
OverlayBlocker,
injectHooks,
} from 'strapi-helper-plugin';
import { LoadingIndicatorPage, OverlayBlocker } from 'strapi-helper-plugin';
import { SHOW_TUTORIALS } from '../../config';
import Header from '../../components/Header/index';
@ -43,23 +39,7 @@ import makeSelecApp from '../App/selectors';
import injectSaga from '../../utils/injectSaga';
import injectReducer from '../../utils/injectReducer';
import {
resetLocaleDefaultClassName,
setLocaleCustomClassName,
} from '../LocaleToggle/actions';
import {
emitEvent,
getInitData,
getSecuredData,
hideLeftMenu,
hideLogout,
setAppError,
setAppSecured,
showLeftMenu,
showLogout,
unsetAppSecured,
} from './actions';
import { emitEvent, setAppError } from './actions';
import makeSelectAdmin from './selectors';
import reducer from './reducer';
import saga from './saga';
@ -68,8 +48,6 @@ import styles from './styles.scss';
export class Admin extends React.Component {
// eslint-disable-line react/prefer-stateless-function
state = { shouldSecureAfterAllPluginsAreMounted: true };
getChildContext = () => ({
emitEvent: this.props.emitEvent,
currentEnvironment: this.props.global.currentEnvironment,
@ -79,42 +57,6 @@ export class Admin extends React.Component {
updatePlugin: this.props.updatePlugin,
});
componentDidMount() {
/* istanbul ignore next */
// const { getHook } = this.props;
// getHook('didGetSecuredData');
// Retrieve the main settings of the application
// this.props.getInitData();
}
/* istanbul ignore next */
// componentDidUpdate(prevProps) {
// const {
// admin: { didGetSecuredData, isLoading, isSecured },
// getHook,
// getSecuredData,
// location: { pathname },
// } = this.props;
// if (!isLoading && this.state.shouldSecureAfterAllPluginsAreMounted) {
// if (!this.hasApluginNotReady(this.props)) {
// getHook('willSecure');
// }
// }
// if (prevProps.location.pathname !== pathname) {
// getHook('willSecure');
// }
// if (prevProps.admin.isSecured !== isSecured && isSecured) {
// getSecuredData();
// }
// if (prevProps.admin.didGetSecuredData !== didGetSecuredData) {
// getHook('didGetSecuredData');
// }
// }
/* istanbul ignore next */
componentDidCatch(error, info) {
/* eslint-disable */
@ -129,11 +71,6 @@ export class Admin extends React.Component {
this.props.setAppError();
}
// TODO remove
getContentWrapperStyle = () => {
return { main: {}, sub: styles.content };
};
hasApluginNotReady = props => {
const {
global: { plugins },
@ -145,12 +82,6 @@ export class Admin extends React.Component {
};
helpers = {
hideLeftMenu: this.props.hideLeftMenu,
hideLogout: this.props.hideLogout,
setAppSecured: this.props.setAppSecured,
showLeftMenu: this.props.showLeftMenu,
showLogout: this.props.showLogout,
unsetAppSecured: this.props.unsetAppSecured,
updatePlugin: this.props.updatePlugin,
};
@ -218,12 +149,9 @@ export class Admin extends React.Component {
<Logout />
<LocaleToggle isLogged />
</NavTopRightWrapper>
<div
className={styles.adminPageRightWrapper}
style={this.getContentWrapperStyle().main}
>
<div className={styles.adminPageRightWrapper}>
<Header />
<div className={this.getContentWrapperStyle().sub}>
<div className={styles.content}>
<Switch>
<Route
path="/"
@ -274,22 +202,12 @@ Admin.childContextTypes = {
Admin.propTypes = {
admin: PropTypes.shape({
autoReload: PropTypes.bool,
appError: PropTypes.bool,
currentEnvironment: PropTypes.string,
didGetSecuredData: PropTypes.bool,
isLoading: PropTypes.bool,
isSecured: PropTypes.bool,
layout: PropTypes.object,
showLogoutComponent: PropTypes.bool,
showMenu: PropTypes.bool,
}).isRequired,
disableGlobalOverlayBlocker: PropTypes.func.isRequired,
emitEvent: PropTypes.func.isRequired,
enableGlobalOverlayBlocker: PropTypes.func.isRequired,
getHook: PropTypes.func.isRequired,
getInitData: PropTypes.func.isRequired,
getSecuredData: PropTypes.func.isRequired,
// getHook: PropTypes.func.isRequired,
global: PropTypes.shape({
blockApp: PropTypes.bool,
currentEnvironment: PropTypes.string,
@ -298,15 +216,8 @@ Admin.propTypes = {
showGlobalAppBlocker: PropTypes.bool,
strapiVersion: PropTypes.string,
}).isRequired,
hideLeftMenu: PropTypes.func.isRequired,
hideLogout: PropTypes.func.isRequired,
location: PropTypes.object.isRequired,
resetLocaleDefaultClassName: PropTypes.func.isRequired,
setAppError: PropTypes.func.isRequired,
setAppSecured: PropTypes.func.isRequired,
showLeftMenu: PropTypes.func.isRequired,
showLogout: PropTypes.func.isRequired,
unsetAppSecured: PropTypes.func.isRequired,
updatePlugin: PropTypes.func.isRequired,
};
@ -321,17 +232,7 @@ export function mapDispatchToProps(dispatch) {
disableGlobalOverlayBlocker,
emitEvent,
enableGlobalOverlayBlocker,
getInitData,
getSecuredData,
hideLeftMenu,
hideLogout,
resetLocaleDefaultClassName,
setAppError,
setAppSecured,
setLocaleCustomClassName,
showLeftMenu,
showLogout,
unsetAppSecured,
updatePlugin,
},
dispatch
@ -344,11 +245,11 @@ const withConnect = connect(
);
const withReducer = injectReducer({ key: 'admin', reducer });
const withSaga = injectSaga({ key: 'admin', saga });
const withHooks = injectHooks({ key: 'admin' });
// const withHooks = injectHooks({ key: 'admin' });
export default compose(
withReducer,
withSaga,
withConnect,
withHooks
withConnect
// withHooks
)(Admin);

View File

@ -4,64 +4,17 @@
*
*/
import { fromJS, Map } from 'immutable';
import {
GET_INIT_DATA_SUCCEEDED,
GET_SECURED_DATA_SUCCEEDED,
HIDE_LEFT_MENU,
HIDE_LOGOUT,
SET_APP_ERROR,
SET_APP_SECURED,
SHOW_LEFT_MENU,
SHOW_LOGOUT,
UNSET_APP_SECURED,
} from './constants';
import { fromJS } from 'immutable';
import { SET_APP_ERROR } from './constants';
const initialState = fromJS({
autoReload: false,
appError: false,
currentEnvironment: 'development',
layout: Map({}),
// NOTE: This should be the models and our stuffs
// Since this api is not implemented yet I just set this vague key ATM
securedData: {},
strapiVersion: '3',
uuid: false,
});
function adminReducer(state = initialState, action) {
switch (action.type) {
case GET_INIT_DATA_SUCCEEDED: {
const {
data: { autoReload, currentEnvironment, layout, strapiVersion, uuid },
} = action;
return state
.update('autoReload', () => autoReload)
.update('currentEnvironment', () => currentEnvironment)
.update('isLoading', () => false)
.update('layout', () => Map(layout))
.update('strapiVersion', () => strapiVersion)
.update('uuid', () => uuid);
}
case GET_SECURED_DATA_SUCCEEDED:
return state
.update('didGetSecuredData', v => !v)
.update('securedData', () => action.data);
case HIDE_LEFT_MENU:
return state.update('showMenu', () => false);
case HIDE_LOGOUT:
return state.update('showLogoutComponent', () => false);
case SET_APP_ERROR:
return state.update('appError', () => true);
case SET_APP_SECURED:
return state.update('isSecured', () => true);
case SHOW_LEFT_MENU:
return state.update('showMenu', () => true);
case SHOW_LOGOUT:
return state.update('showLogoutComponent', () => true);
case UNSET_APP_SECURED:
return state.update('isSecured', () => false);
default:
return state;
}

View File

@ -1,9 +1,6 @@
/* eslint-disable */
import { all, fork, call, put, select, takeLatest } from 'redux-saga/effects';
import { request } from 'strapi-helper-plugin';
import { getInitDataSucceeded, getSecuredDataSucceeded } from './actions';
import { EMIT_EVENT, GET_INIT_DATA, GET_SECURED_DATA } from './constants';
import { EMIT_EVENT } from './constants';
import { makeSelectUuid } from '../App/selectors';
export function* emitter(action) {
@ -30,55 +27,7 @@ export function* emitter(action) {
}
}
export function* getData() {
try {
const endPoints = [
'gaConfig',
'strapiVersion',
'currentEnvironment',
'layout',
];
const [
{ uuid },
{ strapiVersion },
{ autoReload, currentEnvironment },
{ layout },
] = yield all(
endPoints.map(endPoint =>
call(request, `/admin/${endPoint}`, { method: 'GET' })
)
);
yield put(
getInitDataSucceeded({
autoReload,
uuid,
strapiVersion,
currentEnvironment,
layout,
})
);
} catch (err) {
strapi.notification.error('notification.error');
}
}
/* istanbul ignore next */
export function* getSecuredData() {
try {
const data = {};
yield put(getSecuredDataSucceeded(data));
} catch (err) {
console.log(err); // eslint-disable-line no-console
}
}
// Individual exports for testing
export default function* defaultSaga() {
yield all([
fork(takeLatest, EMIT_EVENT, emitter),
fork(takeLatest, GET_INIT_DATA, getData),
fork(takeLatest, GET_SECURED_DATA, getSecuredData),
]);
yield all([fork(takeLatest, EMIT_EVENT, emitter)]);
}

View File

@ -1,121 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`getData Saga should call the strapi.notification action if the response errors 1`] = `
Object {
"@@redux-saga/IO": true,
"ALL": Array [
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/gaConfig",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/strapiVersion",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/currentEnvironment",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/layout",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
],
}
`;
exports[`getData Saga should dispatch the getInitDataSucceeded action if it requests the data successfully 1`] = `
Object {
"@@redux-saga/IO": true,
"ALL": Array [
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/gaConfig",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/strapiVersion",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/currentEnvironment",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
Object {
"@@redux-saga/IO": true,
"CALL": Object {
"args": Array [
"/admin/layout",
Object {
"method": "GET",
},
],
"context": null,
"fn": [Function],
},
},
],
}
`;

View File

@ -1,23 +1,5 @@
import {
emitEvent,
getInitData,
getInitDataSucceeded,
hideLeftMenu,
setAppError,
setAppSecured,
showLeftMenu,
unsetAppSecured,
} from '../actions';
import {
EMIT_EVENT,
GET_INIT_DATA,
GET_INIT_DATA_SUCCEEDED,
HIDE_LEFT_MENU,
SET_APP_ERROR,
SET_APP_SECURED,
SHOW_LEFT_MENU,
UNSET_APP_SECURED,
} from '../constants';
import { emitEvent, setAppError } from '../actions';
import { EMIT_EVENT, SET_APP_ERROR } from '../constants';
describe('<Admin /> actions', () => {
describe('EmitEvent', () => {
@ -32,38 +14,6 @@ describe('<Admin /> actions', () => {
});
});
describe('GetInitData Action', () => {
it('has a type of GET_INIT_DATA', () => {
const expected = {
type: GET_INIT_DATA,
};
expect(getInitData()).toEqual(expected);
});
});
describe('GetInitDataSucceeded Action', () => {
it('should return the correct type and the passed data', () => {
const data = { autoReload: true };
const expected = {
type: GET_INIT_DATA_SUCCEEDED,
data,
};
expect(getInitDataSucceeded(data)).toEqual(expected);
});
});
describe('HideLeftMenu Action', () => {
it('has a type of HIDE_LEFT_MENU', () => {
const expected = {
type: HIDE_LEFT_MENU,
};
expect(hideLeftMenu()).toEqual(expected);
});
});
describe('SetAppError Action', () => {
it('has a type of SET_APP_ERROR', () => {
const expected = {
@ -73,34 +23,4 @@ describe('<Admin /> actions', () => {
expect(setAppError()).toEqual(expected);
});
});
describe('SetAppSecured Action', () => {
it('has a type of SET_APP_SECURED', () => {
const expected = {
type: SET_APP_SECURED,
};
expect(setAppSecured()).toEqual(expected);
});
});
describe('ShowLeftMenu Action', () => {
it('has a type of SHOW_LEFT_MENU', () => {
const expected = {
type: SHOW_LEFT_MENU,
};
expect(showLeftMenu()).toEqual(expected);
});
});
describe('UnsetAppSecured Action', () => {
it('has a type of UNSET_APP_SECURED', () => {
const expected = {
type: UNSET_APP_SECURED,
};
expect(unsetAppSecured()).toEqual(expected);
});
});
});

View File

@ -1,10 +1,5 @@
import React from 'react';
import { mount, shallow } from 'enzyme';
import { BrowserRouter as Router } from 'react-router-dom';
import { Provider } from 'react-redux';
import { IntlProvider } from 'react-intl';
import { compose } from 'redux';
import { shallow } from 'enzyme';
import {
disableGlobalOverlayBlocker,
@ -12,146 +7,10 @@ import {
updatePlugin,
} from '../../App/actions';
import { LoadingIndicatorPage, OverlayBlocker } from 'strapi-helper-plugin';
import injectReducer from '../../../utils/injectReducer';
import history from '../../../utils/history';
import configureStore from '../../../configureStore';
import messages from 'testUtils/commonTrads.json';
import Header from '../../../components/Header/index';
import Onboarding from '../../Onboarding';
import Logout from '../../../components/Logout';
import localeToggleReducer from '../../LocaleToggle/reducer';
import {
resetLocaleDefaultClassName,
setLocaleCustomClassName,
} from '../../LocaleToggle/actions';
import { OverlayBlocker } from 'strapi-helper-plugin';
import { Admin, mapDispatchToProps } from '../index';
import {
getInitData,
hideLeftMenu,
setAppError,
showLeftMenu,
} from '../actions';
import styles from '../styles.scss';
const initialState = {};
const store = configureStore(initialState, history);
const withLocaleToggleReducer = injectReducer({
key: 'localeToggle',
reducer: localeToggleReducer,
});
const WithAdmin = compose(withLocaleToggleReducer)(Admin);
const renderComponent = properties =>
mount(
React.createElement(
props => (
<Provider store={store}>
<IntlProvider locale="en" defaultLocale="en" messages={messages}>
<Router>
<WithAdmin store={store} {...props} />
</Router>
</IntlProvider>
</Provider>
),
properties
)
);
describe('<Admin />, (with Redux), React lifecycle', () => {
let props;
beforeEach(() => {
props = {
admin: {
autoReload: false,
appError: false,
currentEnvironment: 'development',
didGetSecuredData: false,
isLoading: true,
isSecured: false,
layout: {},
showLogoutComponent: false,
showMenu: true,
securedData: {},
strapiVersion: '3',
uuid: false,
},
disableGlobalOverlayBlocker: jest.fn(),
emitEvent: jest.fn(),
enableGlobalOverlayBlocker: jest.fn(),
getInitData: jest.fn(),
getSecuredData: jest.fn(),
getHook: jest.fn(),
global: {
appPlugins: [],
blockApp: false,
currentEnvironment: 'development',
overlayBlockerData: null,
hasUserPlugin: true,
isAppLoading: true,
plugins: {},
showGlobalAppBlocker: true,
strapiVersion: '3',
},
localeToggle: {},
hideLeftMenu: jest.fn(),
hideLogout: jest.fn(),
location: {},
resetLocaleDefaultClassName: jest.fn(),
setAppError: jest.fn(),
setAppSecured: jest.fn(),
showLeftMenu: jest.fn(),
showLogout: jest.fn(),
showGlobalAppBlocker: jest.fn(),
unsetAppSecured: jest.fn(),
updatePlugin: jest.fn(),
};
});
it('should not crash when mounted', () => {
renderComponent(props);
});
describe('ComponentDidUpdate', () => {
it('should call the getSecuredData if the app is secured', () => {
const wrapper = renderComponent(props);
expect(props.getSecuredData).not.toHaveBeenCalled();
wrapper.setProps({
admin: { isSecured: true },
});
expect(props.getSecuredData).toHaveBeenCalled();
wrapper.unmount();
});
it('should call the getHook props with the willSecure arg if the pathname has changed', () => {
props.admin.isLoading = false;
props.global.isAppLoading = false;
const renderedComponent = renderComponent(props);
renderedComponent.setProps({
admin: { isLoading: false },
location: { pathname: '/admin/marketPlace' },
});
expect(props.getHook).toHaveBeenCalledWith('willSecure');
renderedComponent.unmount();
});
it('should emit the didGetSecured hook event when the secured data has been retrieved', () => {
const wrapper = renderComponent(props);
expect(props.getHook).not.toHaveBeenCalled();
wrapper.setProps({
admin: { didGetSecuredData: true },
});
expect(props.getHook).toHaveBeenCalledWith('didGetSecuredData');
wrapper.unmount();
});
});
});
import { setAppError } from '../actions';
describe('<Admin />', () => {
let props;
@ -159,47 +18,27 @@ describe('<Admin />', () => {
beforeEach(() => {
props = {
admin: {
autoReload: false,
appError: false,
currentEnvironment: 'development',
didGetSecuredData: false,
isLoading: true,
isSecured: false,
layout: {},
securedData: {},
showLogoutComponent: false,
showMenu: true,
strapiVersion: '3',
uuid: false,
},
disableGlobalOverlayBlocker: jest.fn(),
emitEvent: jest.fn(),
enableGlobalOverlayBlocker: jest.fn(),
getInitData: jest.fn(),
getHook: jest.fn(),
getSecuredData: jest.fn(),
global: {
appPlugins: [],
autoReload: false,
blockApp: false,
currentEnvironment: 'development',
overlayBlockerData: null,
hasAdminUser: false,
hasUserPlugin: true,
isAppLoading: true,
isLoading: true,
overlayBlockerData: null,
plugins: {},
showGlobalAppBlocker: true,
strapiVersion: '3',
uuid: false,
},
localeToggle: {},
hideLeftMenu: jest.fn(),
hideLogout: jest.fn(),
location: {},
resetLocaleDefaultClassName: jest.fn(),
setAppError: jest.fn(),
setAppSecured: jest.fn(),
showLeftMenu: jest.fn(),
showLogout: jest.fn(),
showGlobalAppBlocker: jest.fn(),
unsetAppSecured: jest.fn(),
updatePlugin: jest.fn(),
};
});
@ -209,36 +48,6 @@ describe('<Admin />', () => {
});
describe('render', () => {
it('should not display the header if the showMenu prop is false', () => {
const adminProps = Object.assign(props.admin, {
isLoading: false,
showMenu: false,
});
const renderedComponent = shallow(<Admin {...props} {...adminProps} />);
expect(renderedComponent.find(Header)).toHaveLength(0);
});
it('should not display the Logout if the showLogoutComponent prop is false', () => {
props.admin.isLoading = false;
props.global.isAppLoading = false;
const wrapper = shallow(<Admin {...props} />);
expect(wrapper.find(Logout)).toHaveLength(0);
expect(wrapper.find(Onboarding)).toHaveLength(0);
});
it('should display the Logout if the showLogoutComponent prop is true', () => {
props.admin.showLogoutComponent = true;
props.admin.isLoading = false;
props.global.isAppLoading = false;
const wrapper = shallow(<Admin {...props} />);
expect(wrapper.find(Logout)).toHaveLength(1);
expect(wrapper.find(Onboarding)).toHaveLength(1);
});
it('should display the OverlayBlocker if blockApp and showGlobalOverlayBlocker are true', () => {
const globalProps = Object.assign(props.global, {
blockApp: true,
@ -249,19 +58,13 @@ describe('<Admin />', () => {
expect(renderedComponent.find(OverlayBlocker)).toHaveLength(1);
});
it('should display the LoadingIndicatorPage if the isLoading prop is true', () => {
const renderedComponent = shallow(<Admin {...props} />);
expect(renderedComponent.find(LoadingIndicatorPage)).toHaveLength(1);
});
});
describe('HasApluginNotReady instance', () => {
it('should return true if a plugin is not ready', () => {
props.global.plugins = {
test: { isReady: true },
other: { isReady: false },
test: { isReady: true, initializer: () => null, id: 'test' },
other: { isReady: false, initializer: () => null, id: 'other' },
};
const wrapper = shallow(<Admin {...props} />);
@ -283,25 +86,6 @@ describe('<Admin />', () => {
});
});
describe('getContentWrapperStyle instance', () => {
it('should return an empty object for the main key if showMenu prop is true', () => {
const renderedComponent = shallow(<Admin {...props} />);
const { getContentWrapperStyle } = renderedComponent.instance();
const expected = { main: {}, sub: styles.content };
expect(getContentWrapperStyle()).toEqual(expected);
});
it('should not return an empty object for the main key if showMenu prop is true', () => {
const adminProps = Object.assign(props.admin, { showMenu: false });
const renderedComponent = shallow(<Admin {...props} {...adminProps} />);
const { getContentWrapperStyle } = renderedComponent.instance();
const expected = { main: { width: '100%' }, sub: styles.wrapper };
expect(getContentWrapperStyle()).toEqual(expected);
});
});
describe('renderMarketPlace instance', () => {
it('should return the MarketPlace container', () => {
const renderedComponent = shallow(<Admin {...props} />);
@ -375,40 +159,6 @@ describe('<Admin />, mapDispatchToProps', () => {
});
});
describe('getInitData', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.getInitData).toBeDefined();
});
it('should dispatch the getInitData action when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.getInitData();
expect(dispatch).toHaveBeenCalledWith(getInitData());
});
});
describe('hideLeftMenu', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.hideLeftMenu).toBeDefined();
});
it('should dispatch the hideLeftMenu action when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.hideLeftMenu();
expect(dispatch).toHaveBeenCalledWith(hideLeftMenu());
});
});
describe('setAppError', () => {
it('should be injected', () => {
const dispatch = jest.fn();
@ -426,57 +176,6 @@ describe('<Admin />, mapDispatchToProps', () => {
});
});
describe('showLeftMenu', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.showLeftMenu).toBeDefined();
});
it('should dispatch the showLeftMenu action when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.showLeftMenu();
expect(dispatch).toHaveBeenCalledWith(showLeftMenu());
});
});
describe('resetLocaleDefaultClassName', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.resetLocaleDefaultClassName).toBeDefined();
});
it('should dispatch the resetLocaleDefaultClassName action when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.resetLocaleDefaultClassName();
expect(dispatch).toHaveBeenCalledWith(resetLocaleDefaultClassName());
});
});
describe('setLocaleCustomClassName', () => {
it('should be injected', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
expect(result.setLocaleCustomClassName).toBeDefined();
});
it('should dispatch the setLocaleCustomClassName action when called', () => {
const dispatch = jest.fn();
const result = mapDispatchToProps(dispatch);
result.setLocaleCustomClassName();
expect(dispatch).toHaveBeenCalledWith(setLocaleCustomClassName());
});
});
describe('updatePlugin', () => {
it('should be injected', () => {
const dispatch = jest.fn();

View File

@ -1,16 +1,6 @@
import { fromJS, Map } from 'immutable';
import { fromJS } from 'immutable';
import {
getSecuredDataSucceeded,
getInitDataSucceeded,
hideLeftMenu,
hideLogout,
setAppError,
setAppSecured,
showLeftMenu,
showLogout,
unsetAppSecured,
} from '../actions';
import { setAppError } from '../actions';
import adminReducer from '../reducer';
describe('adminReducer', () => {
@ -18,18 +8,7 @@ describe('adminReducer', () => {
beforeEach(() => {
state = fromJS({
autoReload: false,
appError: false,
currentEnvironment: 'development',
didGetSecuredData: false,
isLoading: true,
isSecured: false,
layout: Map({}),
securedData: {},
showLogoutComponent: false,
showMenu: true,
strapiVersion: '3',
uuid: false,
});
});
@ -39,76 +18,9 @@ describe('adminReducer', () => {
expect(adminReducer(undefined, {})).toEqual(expected);
});
it('should handle the getSecuredDataSucceeded action correctly', () => {
const expected = state
.set('didGetSecuredData', true)
.set('securedData', undefined);
expect(adminReducer(state, getSecuredDataSucceeded())).toEqual(expected);
});
it('should handle the getAdminDataSucceeded action correctly', () => {
const data = {
autoReload: true,
currentEnvironment: 'production',
isLoading: false,
layout: {},
strapiVersion: '3.0.0-beta',
uuid: 'uuid',
};
const expected = state
.set('autoReload', true)
.set('currentEnvironment', 'production')
.set('isLoading', false)
.set('layout', Map({}))
.set('strapiVersion', '3.0.0-beta')
.set('uuid', 'uuid');
expect(adminReducer(state, getInitDataSucceeded(data))).toEqual(expected);
});
it('should handle the hideLeftMenu action correctly', () => {
const expected = state.set('showMenu', false);
expect(adminReducer(state, hideLeftMenu())).toEqual(expected);
});
it('should handle the hideLogout action correctly', () => {
const expected = state.set('showLogoutComponent', false);
expect(adminReducer(state, hideLogout())).toEqual(expected);
});
it('should handle the setaAppError action correctly', () => {
const expected = state.set('appError', true);
expect(adminReducer(state, setAppError())).toEqual(expected);
});
it('should handle the setAppSecured action correctly', () => {
const expected = state.set('isSecured', true);
expect(adminReducer(state, setAppSecured())).toEqual(expected);
});
it('should handle the showLeftMenu action correctly', () => {
const expected = state.set('showMenu', true);
state.set('showMenu', false);
expect(adminReducer(state, showLeftMenu())).toEqual(expected);
});
it('should handle the showLogout action correctly', () => {
const expected = state.set('showLogoutComponent', true);
state.set('showLogoutComponent', false);
expect(adminReducer(state, showLogout())).toEqual(expected);
});
it('should handle the unsetAppSecured action correctly', () => {
state.set('isSecured', true);
const expected = state.set('isSecured', false);
expect(adminReducer(state, unsetAppSecured())).toEqual(expected);
});
});

View File

@ -4,63 +4,10 @@
/* eslint-disable redux-saga/yield-effects */
/* eslint-disable redux-saga/no-unhandled-errors */
import { all, fork, put, takeLatest } from 'redux-saga/effects';
import defaultSaga, { emitter, getData, getSecuredData } from '../saga';
import { all, fork, takeLatest } from 'redux-saga/effects';
import defaultSaga, { emitter } from '../saga';
import { getInitDataSucceeded } from '../actions';
import { EMIT_EVENT, GET_INIT_DATA, GET_SECURED_DATA } from '../constants';
describe('getData Saga', () => {
let getDataGenerator;
beforeEach(() => {
getDataGenerator = getData();
const response = [
{ uuid: 'uuid' },
{ strapiVersion: 'beta' },
{ autoReload: true, currentEnvironment: 'test' },
{ layout: {} },
];
const callDescriptor = getDataGenerator.next(response).value;
expect(callDescriptor).toMatchSnapshot();
});
it('should dispatch the getInitDataSucceeded action if it requests the data successfully', () => {
const response = [
{ uuid: 'uuid' },
{ strapiVersion: 'beta' },
{ autoReload: true, currentEnvironment: 'test' },
{ layout: {} },
];
const putDescriptor = getDataGenerator.next(response).value;
const [
{ uuid },
{ strapiVersion },
{ autoReload, currentEnvironment },
{ layout },
] = response;
expect(putDescriptor).toEqual(
put(
getInitDataSucceeded({
autoReload,
uuid,
strapiVersion,
currentEnvironment,
layout,
})
)
);
});
it('should call the strapi.notification action if the response errors', () => {
const response = new Error('Some error');
getDataGenerator.throw(response).value;
expect(strapi.notification.error).toHaveBeenCalled();
});
});
import { EMIT_EVENT } from '../constants';
describe('defaultSaga Saga', () => {
const defaultSagaSaga = defaultSaga();
@ -69,11 +16,7 @@ describe('defaultSaga Saga', () => {
const forkDescriptor = defaultSagaSaga.next().value;
expect(forkDescriptor).toEqual(
all([
fork(takeLatest, EMIT_EVENT, emitter),
fork(takeLatest, GET_INIT_DATA, getData),
fork(takeLatest, GET_SECURED_DATA, getSecuredData),
])
all([fork(takeLatest, EMIT_EVENT, emitter)])
);
});
});