2020-01-21 16:06:05 +01:00
|
|
|
import React from 'react';
|
2020-01-27 18:32:57 +01:00
|
|
|
import { Router, Route, Switch } from 'react-router-dom';
|
2020-01-21 16:06:05 +01:00
|
|
|
import { shallow } from 'enzyme';
|
2020-01-21 17:29:39 +01:00
|
|
|
import CreatableSelect from 'react-select/creatable';
|
2020-01-27 18:32:57 +01:00
|
|
|
import { render, cleanup } from '@testing-library/react';
|
|
|
|
|
import { createMemoryHistory } from 'history';
|
2020-01-21 17:29:39 +01:00
|
|
|
import { InputText } from '@buffetjs/core';
|
2020-01-27 18:32:57 +01:00
|
|
|
import { CircleButton, GlobalContextProvider } from 'strapi-helper-plugin';
|
|
|
|
|
import { IntlProvider } from 'react-intl';
|
|
|
|
|
|
|
|
|
|
import { translationMessages } from '../../../i18n';
|
|
|
|
|
|
2020-01-21 16:06:05 +01:00
|
|
|
import HeadersInput from '../index';
|
|
|
|
|
|
2020-01-27 18:32:57 +01:00
|
|
|
const history = createMemoryHistory();
|
|
|
|
|
|
2020-01-21 16:06:05 +01:00
|
|
|
describe('Admin | components | HeadersInput', () => {
|
|
|
|
|
const props = {
|
|
|
|
|
name: 'headers',
|
|
|
|
|
value: [
|
|
|
|
|
{
|
|
|
|
|
key: '',
|
|
|
|
|
value: '',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
onChange: jest.fn(),
|
|
|
|
|
onClick: jest.fn(),
|
|
|
|
|
onRemove: jest.fn(),
|
|
|
|
|
};
|
2020-01-27 18:32:57 +01:00
|
|
|
describe('should render properly', () => {
|
|
|
|
|
afterEach(cleanup);
|
2020-01-22 17:47:09 +01:00
|
|
|
it('It should not crash', () => {
|
2020-01-21 16:06:05 +01:00
|
|
|
shallow(<HeadersInput {...props} />);
|
|
|
|
|
});
|
2020-01-21 17:29:39 +01:00
|
|
|
|
2020-01-27 18:32:57 +01:00
|
|
|
it('render component', () => {
|
|
|
|
|
const intlProvider = new IntlProvider(
|
|
|
|
|
{
|
|
|
|
|
locale: 'en',
|
|
|
|
|
messages: translationMessages.en,
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
const { intl: originalIntl } = intlProvider.getChildContext();
|
|
|
|
|
|
|
|
|
|
const { asFragment } = render(
|
|
|
|
|
<IntlProvider locale={intlProvider.locale}>
|
|
|
|
|
<GlobalContextProvider formatMessage={originalIntl.formatMessage}>
|
|
|
|
|
<Router history={history}>
|
|
|
|
|
<Switch>
|
|
|
|
|
<Route>
|
|
|
|
|
<HeadersInput {...props} />
|
|
|
|
|
</Route>
|
|
|
|
|
</Switch>
|
|
|
|
|
</Router>
|
|
|
|
|
</GlobalContextProvider>
|
|
|
|
|
</IntlProvider>
|
|
|
|
|
);
|
|
|
|
|
expect(asFragment()).toMatchSnapshot();
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-21 17:29:39 +01:00
|
|
|
it('It should render as many key/value rows as value', () => {
|
|
|
|
|
const renderedComponent = shallow(<HeadersInput {...props} />);
|
|
|
|
|
|
|
|
|
|
expect(renderedComponent.find(CreatableSelect)).toHaveLength(1);
|
|
|
|
|
});
|
2020-01-21 16:06:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Actions', () => {
|
|
|
|
|
it('It should call the onClick props on remove button', () => {
|
|
|
|
|
const renderedComponent = shallow(<HeadersInput {...props} />);
|
|
|
|
|
|
|
|
|
|
const removeButton = renderedComponent.find(CircleButton).at(0);
|
|
|
|
|
removeButton.simulate('click');
|
|
|
|
|
|
2020-01-21 17:29:39 +01:00
|
|
|
expect(props.onRemove).toHaveBeenCalledWith(0);
|
2020-01-21 16:06:05 +01:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('It should call the onClick props on add button', () => {
|
|
|
|
|
const renderedComponent = shallow(<HeadersInput {...props} />);
|
|
|
|
|
|
|
|
|
|
const addButton = renderedComponent.find('ul + button');
|
|
|
|
|
addButton.simulate('click');
|
|
|
|
|
|
|
|
|
|
expect(props.onClick).toHaveBeenCalled();
|
|
|
|
|
});
|
2020-01-21 17:29:39 +01:00
|
|
|
|
|
|
|
|
it('It should call the onChange props on input text change', () => {
|
|
|
|
|
const renderedComponent = shallow(<HeadersInput {...props} />);
|
|
|
|
|
|
|
|
|
|
const input = renderedComponent.find(InputText).at(0);
|
|
|
|
|
input.simulate('change');
|
|
|
|
|
|
|
|
|
|
expect(props.onChange).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should have default onRemove', () => {
|
|
|
|
|
expect(HeadersInput.defaultProps.onRemove).toBeDefined();
|
|
|
|
|
});
|
2020-01-21 16:06:05 +01:00
|
|
|
});
|