mirror of
https://github.com/strapi/strapi.git
synced 2025-12-16 09:45:08 +00:00
Add tests to attributesModal
This commit is contained in:
parent
ac80b5bbdc
commit
afb3628f2f
@ -2,13 +2,13 @@ import React from 'react';
|
|||||||
import { mount } from 'enzyme';
|
import { mount } from 'enzyme';
|
||||||
import { IntlProvider, intlShape } from 'react-intl';
|
import { IntlProvider, intlShape } from 'react-intl';
|
||||||
|
|
||||||
const mountWithIntl = (componentToMount, pluginTrads) => {
|
const mountWithIntl = (componentToMount, pluginTrads, context = {}) => {
|
||||||
const intlProvider = new IntlProvider({ locale: 'en', messages: pluginTrads }, {});
|
const intlProvider = new IntlProvider({ locale: 'en', messages: pluginTrads }, {});
|
||||||
const { intl } = intlProvider.getChildContext();
|
const { intl } = intlProvider.getChildContext();
|
||||||
|
|
||||||
return mount(
|
return mount(
|
||||||
React.cloneElement(componentToMount, { intl }), {
|
React.cloneElement(componentToMount, { intl }), {
|
||||||
context: { intl },
|
context: { intl, ...context },
|
||||||
childContextTypes: { intl: intlShape },
|
childContextTypes: { intl: intlShape },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@ -26,7 +26,7 @@ class AttributesPickerModal extends React.Component { // eslint-disable-line rea
|
|||||||
const { isOpen } = this.props;
|
const { isOpen } = this.props;
|
||||||
|
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
document.addEventListener('keydown', this.handleKeyDown);
|
this.addEventListener();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,9 +37,9 @@ class AttributesPickerModal extends React.Component { // eslint-disable-line rea
|
|||||||
this.updateNodeToFocus(0);
|
this.updateNodeToFocus(0);
|
||||||
|
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
document.addEventListener('keydown', this.handleKeyDown);
|
this.addEventListener();
|
||||||
} else {
|
} else {
|
||||||
document.removeEventListener('keydown', this.handleKeyDown);
|
this.removeEventListener();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,16 +57,30 @@ class AttributesPickerModal extends React.Component { // eslint-disable-line rea
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyDown = (e ) => {
|
addEventListener = () => {
|
||||||
|
document.addEventListener('keydown', this.handleKeyDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeEventListener = () => {
|
||||||
|
document.removeEventListener('keydown', this.handleKeyDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
|
handleKeyDown = (e) => {
|
||||||
|
/* istanbul ignore next */
|
||||||
const { nodeToFocus } = this.state;
|
const { nodeToFocus } = this.state;
|
||||||
|
/* istanbul ignore next */
|
||||||
const attributesLength = this.getAttributes().length;
|
const attributesLength = this.getAttributes().length;
|
||||||
|
/* istanbul ignore next */
|
||||||
let next = 0;
|
let next = 0;
|
||||||
|
|
||||||
// Disable the tab behavior
|
// Disable the tab behavior
|
||||||
|
/* istanbul ignore next */
|
||||||
if (e.keyCode === 9) {
|
if (e.keyCode === 9) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
switch (e.keyCode) {
|
switch (e.keyCode) {
|
||||||
case 9: // Tab
|
case 9: // Tab
|
||||||
case 39: // Right Arrow
|
case 39: // Right Arrow
|
||||||
@ -79,6 +93,7 @@ class AttributesPickerModal extends React.Component { // eslint-disable-line rea
|
|||||||
next = 0;
|
next = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* istanbul ignore next */
|
||||||
this.updateNodeToFocus(next);
|
this.updateNodeToFocus(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,171 @@
|
|||||||
// import React from 'react';
|
import React from 'react';
|
||||||
// import { shallow } from 'enzyme';
|
import { fromJS } from 'immutable';
|
||||||
|
|
||||||
// import { AttributesPickerModal } from '../index';
|
import mountWithIntl from 'testUtils/mountWithIntl';
|
||||||
|
import formatMessagesWithPluginId from 'testUtils/formatMessages';
|
||||||
|
|
||||||
|
import pluginId from '../../../pluginId';
|
||||||
|
import pluginTradsEn from '../../../translations/en.json';
|
||||||
|
|
||||||
|
import AttributesPickerModal from '../index';
|
||||||
|
|
||||||
|
const messages = formatMessagesWithPluginId(pluginId, pluginTradsEn);
|
||||||
|
|
||||||
|
const renderComponent = (props = {}, context = {}) => mountWithIntl(<AttributesPickerModal {...props} />, messages, context);
|
||||||
|
|
||||||
describe('<AttributesPickerModal />', () => {
|
describe('<AttributesPickerModal />', () => {
|
||||||
it('Expect to have unit tests specified', () => {
|
let props;
|
||||||
expect(true).toEqual(false);
|
|
||||||
|
beforeEach(() => {
|
||||||
|
props = {
|
||||||
|
isOpen: false,
|
||||||
|
push: jest.fn(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it should not crash', () => {
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add the keyup eventListener when mounted if the isOpen prop is true', () => {
|
||||||
|
props.isOpen = true;
|
||||||
|
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
const spyOnAddEventListener = jest.spyOn(wrapper.instance(), 'addEventListener');
|
||||||
|
|
||||||
|
wrapper.instance().componentDidMount();
|
||||||
|
|
||||||
|
expect(spyOnAddEventListener).toHaveBeenCalled();
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update the nodeToFocus state when the isOpen prop changes', () => {
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
const spyOnUpdateNodeToFocus = jest.spyOn(wrapper.instance(), 'updateNodeToFocus');
|
||||||
|
const spyOnAddEventListener = jest.spyOn(wrapper.instance(), 'addEventListener');
|
||||||
|
|
||||||
|
wrapper.setState({ nodeToFocus: 1 });
|
||||||
|
wrapper.setProps({ isOpen: true });
|
||||||
|
|
||||||
|
expect(spyOnUpdateNodeToFocus).toHaveBeenCalledWith(0);
|
||||||
|
expect(spyOnAddEventListener).toHaveBeenCalled();
|
||||||
|
expect(wrapper.state('nodeToFocus')).toEqual(0);
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove the event listener when the props isOpen changes from true to false', () => {
|
||||||
|
props.isOpen = true;
|
||||||
|
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
const removeEventListener = jest.spyOn(wrapper.instance(), 'removeEventListener');
|
||||||
|
|
||||||
|
wrapper.setProps({ isOpen: false });
|
||||||
|
|
||||||
|
expect(removeEventListener).toHaveBeenCalled();
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove the media option if the upload plugin is not installed', () => {
|
||||||
|
const context = {
|
||||||
|
plugins: {
|
||||||
|
'content-type-builder': {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const wrapper = renderComponent(props, context);
|
||||||
|
const { getAttributes } = wrapper.instance();
|
||||||
|
|
||||||
|
expect(getAttributes()).not.toContain({
|
||||||
|
"type": "media",
|
||||||
|
"description": "content-type-builder.popUpForm.attributes.media.description",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not remove the media option if the upload plugin is installed', () => {
|
||||||
|
const context = {
|
||||||
|
plugins: {
|
||||||
|
'content-type-builder': {},
|
||||||
|
'upload': {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const wrapper = renderComponent(props, context);
|
||||||
|
const { getAttributes } = wrapper.instance();
|
||||||
|
|
||||||
|
expect(getAttributes()).toContainEqual({
|
||||||
|
"type": "media",
|
||||||
|
"description": "content-type-builder.popUpForm.attributes.media.description",
|
||||||
|
});
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle the plugins context object correctly', () => {
|
||||||
|
const context = {
|
||||||
|
plugins: fromJS({
|
||||||
|
'content-type-builder': {},
|
||||||
|
'upload': {},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const wrapper = renderComponent(props, context);
|
||||||
|
const { getAttributes } = wrapper.instance();
|
||||||
|
|
||||||
|
expect(getAttributes()).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle the onOpened instance correctly', () => {
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
|
||||||
|
wrapper.instance().handleOnOpened();
|
||||||
|
expect(wrapper.state('isDisplayed')).toEqual(true);
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle the onClosed instance correctly', () => {
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
|
||||||
|
wrapper.setState({ isDisplayed: true });
|
||||||
|
wrapper.instance().handleOnClosed();
|
||||||
|
|
||||||
|
expect(wrapper.state('isDisplayed')).toEqual(false);
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle the toggle instance correctly', () => {
|
||||||
|
const wrapper = renderComponent(props);
|
||||||
|
|
||||||
|
wrapper.instance().handleToggle();
|
||||||
|
|
||||||
|
expect(props.push).toHaveBeenCalledWith({ search: '' });
|
||||||
|
|
||||||
|
wrapper.unmount();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('listens to the keydown event', () => {
|
||||||
|
const map = {};
|
||||||
|
document.addEventListener = jest.fn((event, cb) => {
|
||||||
|
map[event] = cb;
|
||||||
|
});
|
||||||
|
const context = {
|
||||||
|
plugins: fromJS({
|
||||||
|
'content-type-builder': {},
|
||||||
|
'upload': {},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const wrapper = renderComponent(props, context);
|
||||||
|
|
||||||
|
|
||||||
|
const spyOnHandleKeyDown = jest.spyOn(wrapper.instance(), 'handleKeyDown');
|
||||||
|
|
||||||
|
wrapper.instance().forceUpdate();
|
||||||
|
wrapper.setProps({ isOpen: true });
|
||||||
|
map.keydown({ key: 'Tab' });
|
||||||
|
|
||||||
|
expect(spyOnHandleKeyDown).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user