From 6d44ac1e0c6bb1f0fb77457b50cd22b098beac57 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 15 Mar 2020 11:59:07 +0100 Subject: [PATCH] Created common component that can be used both for create and edit servers --- src/servers/CreateServer.js | 41 +++++++++---------------- src/servers/helpers/ServerForm.js | 41 +++++++++++++++++++++++++ test/servers/CreateServer.test.js | 11 +++---- test/servers/helpers/ServerForm.test.js | 33 ++++++++++++++++++++ 4 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 src/servers/helpers/ServerForm.js create mode 100644 test/servers/helpers/ServerForm.test.js diff --git a/src/servers/CreateServer.js b/src/servers/CreateServer.js index 81375eaa..235ecd82 100644 --- a/src/servers/CreateServer.js +++ b/src/servers/CreateServer.js @@ -1,8 +1,8 @@ -import React, { useState, useEffect } from 'react'; +import React, { useEffect } from 'react'; import { v4 as uuid } from 'uuid'; import PropTypes from 'prop-types'; -import { HorizontalFormGroup } from '../utils/HorizontalFormGroup'; import './CreateServer.scss'; +import { ServerForm } from './helpers/ServerForm'; const SHOW_IMPORT_MSG_TIME = 4000; const propTypes = { @@ -15,15 +15,10 @@ const propTypes = { const CreateServer = (ImportServersBtn, useStateFlagTimeout) => { const CreateServerComp = ({ createServer, history: { push }, resetSelectedServer }) => { - const [ name, setName ] = useState(''); - const [ url, setUrl ] = useState(''); - const [ apiKey, setApiKey ] = useState(''); const [ serversImported, setServersImported ] = useStateFlagTimeout(false, SHOW_IMPORT_MSG_TIME); - const handleSubmit = (e) => { - e.preventDefault(); - + const handleSubmit = (serverData) => { const id = uuid(); - const server = { id, name, url, apiKey }; + const server = { id, ...serverData }; createServer(server); push(`/server/${id}/list-short-urls/1`); @@ -35,26 +30,20 @@ const CreateServer = (ImportServersBtn, useStateFlagTimeout) => { return (
-
- Name - URL - API key + + + + -
- - -
- - {serversImported && ( -
-
-
- Servers properly imported. You can now select one from the list :) -
+ {serversImported && ( +
+
+
+ Servers properly imported. You can now select one from the list :)
- )} - +
+ )}
); }; diff --git a/src/servers/helpers/ServerForm.js b/src/servers/helpers/ServerForm.js new file mode 100644 index 00000000..57650f09 --- /dev/null +++ b/src/servers/helpers/ServerForm.js @@ -0,0 +1,41 @@ +import React, { useEffect, useState } from 'react'; +import PropTypes from 'prop-types'; +import { HorizontalFormGroup } from '../../utils/HorizontalFormGroup'; + +const propTypes = { + onSubmit: PropTypes.func.isRequired, + initialValues: PropTypes.shape({ + name: PropTypes.string.isRequired, + url: PropTypes.string.isRequired, + apiKey: PropTypes.string.isRequired, + }), + children: PropTypes.node.isRequired, +}; + +export const ServerForm = ({ onSubmit, initialValues, children }) => { + const [ name, setName ] = useState(''); + const [ url, setUrl ] = useState(''); + const [ apiKey, setApiKey ] = useState(''); + const handleSubmit = (e) => { + e.preventDefault(); + onSubmit({ name, url, apiKey }); + }; + + useEffect(() => { + initialValues && setName(initialValues.name); + initialValues && setUrl(initialValues.url); + initialValues && setApiKey(initialValues.apiKey); + }, [ initialValues ]); + + return ( +
+ Name + URL + API key + +
{children}
+
+ ); +}; + +ServerForm.propTypes = propTypes; diff --git a/test/servers/CreateServer.test.js b/test/servers/CreateServer.test.js index b3e5bc1c..cab51a60 100644 --- a/test/servers/CreateServer.test.js +++ b/test/servers/CreateServer.test.js @@ -2,7 +2,7 @@ import React from 'react'; import { shallow } from 'enzyme'; import { identity } from 'ramda'; import createServerConstruct from '../../src/servers/CreateServer'; -import { HorizontalFormGroup } from '../../src/utils/HorizontalFormGroup'; +import { ServerForm } from '../../src/servers/helpers/ServerForm'; describe('', () => { let wrapper; @@ -29,8 +29,7 @@ describe('', () => { it('renders components', () => { const wrapper = createWrapper(); - expect(wrapper.find(HorizontalFormGroup)).toHaveLength(3); - expect(wrapper.find(ImportServersBtn)).toHaveLength(1); + expect(wrapper.find(ServerForm)).toHaveLength(1); expect(wrapper.find('.create-server__import-success-msg')).toHaveLength(0); }); @@ -42,11 +41,9 @@ describe('', () => { it('creates server and redirects to it when form is submitted', () => { const wrapper = createWrapper(); - const form = wrapper.find('form'); + const form = wrapper.find(ServerForm); - form.simulate('submit', { preventDefault() { - return ''; - } }); + form.simulate('submit', {}); expect(createServerMock).toHaveBeenCalledTimes(1); expect(historyMock.push).toHaveBeenCalledTimes(1); diff --git a/test/servers/helpers/ServerForm.test.js b/test/servers/helpers/ServerForm.test.js new file mode 100644 index 00000000..855e8c1e --- /dev/null +++ b/test/servers/helpers/ServerForm.test.js @@ -0,0 +1,33 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import { ServerForm } from '../../../src/servers/helpers/ServerForm'; +import { HorizontalFormGroup } from '../../../src/utils/HorizontalFormGroup'; + +describe('', () => { + let wrapper; + const onSubmit = jest.fn(); + + beforeEach(() => { + wrapper = shallow(Something); + }); + + afterEach(() => { + jest.resetAllMocks(); + wrapper && wrapper.unmount(); + }); + + it('renders components', () => { + expect(wrapper.find(HorizontalFormGroup)).toHaveLength(3); + expect(wrapper.find('span')).toHaveLength(1); + }); + + it('invokes submit callback when submit event is triggered', () => { + const form = wrapper.find('form'); + const preventDefault = jest.fn(); + + form.simulate('submit', { preventDefault }); + + expect(preventDefault).toHaveBeenCalled(); + expect(onSubmit).toHaveBeenCalled(); + }); +});