Converted CreateServer into functional component

This commit is contained in:
Alejandro Celaya 2020-03-15 10:33:23 +01:00
parent 7db222664d
commit f6baedc655
3 changed files with 55 additions and 68 deletions

View file

@ -10,19 +10,24 @@ describe('<CreateServer />', () => {
const historyMock = {
push: jest.fn(),
};
beforeEach(() => {
createServerMock.mockReset();
const CreateServer = createServerConstruct(ImportServersBtn);
const createWrapper = (serversImported = false) => {
const CreateServer = createServerConstruct(ImportServersBtn, () => [ serversImported, () => '' ]);
wrapper = shallow(
<CreateServer createServer={createServerMock} resetSelectedServer={identity} history={historyMock} />
);
return wrapper;
};
afterEach(() => {
jest.resetAllMocks();
wrapper && wrapper.unmount();
});
afterEach(() => wrapper.unmount());
it('renders components', () => {
const wrapper = createWrapper();
expect(wrapper.find('#name')).toHaveLength(1);
expect(wrapper.find('#url')).toHaveLength(1);
expect(wrapper.find('#apiKey')).toHaveLength(1);
@ -31,11 +36,13 @@ describe('<CreateServer />', () => {
});
it('shows success message when imported is true', () => {
wrapper.setState({ serversImported: true });
const wrapper = createWrapper(true);
expect(wrapper.find('.create-server__import-success-msg')).toHaveLength(1);
});
it('creates server and redirects to it when form is submitted', () => {
const wrapper = createWrapper();
const form = wrapper.find('form');
form.simulate('submit', { preventDefault() {
@ -45,18 +52,4 @@ describe('<CreateServer />', () => {
expect(createServerMock).toHaveBeenCalledTimes(1);
expect(historyMock.push).toHaveBeenCalledTimes(1);
});
it('updates state when inputs are changed', () => {
const nameInput = wrapper.find('#name');
const urlInput = wrapper.find('#url');
const apiKeyInput = wrapper.find('#apiKey');
nameInput.simulate('change', { target: { value: 'the_name' } });
urlInput.simulate('change', { target: { value: 'the_url' } });
apiKeyInput.simulate('change', { target: { value: 'the_api_key' } });
expect(wrapper.state('name')).toEqual('the_name');
expect(wrapper.state('url')).toEqual('the_url');
expect(wrapper.state('apiKey')).toEqual('the_api_key');
});
});