2018-12-23 03:13:56 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-01-10 21:51:14 +03:00
|
|
|
import {
|
|
|
|
Toolbar,
|
|
|
|
Container,
|
|
|
|
Button,
|
|
|
|
Select,
|
|
|
|
Notification,
|
|
|
|
Dialog
|
|
|
|
} from 'devui';
|
2018-12-23 03:13:56 +03:00
|
|
|
import { formSchema, uiSchema, defaultFormData } from './templateForm';
|
|
|
|
import AddIcon from 'react-icons/lib/md/add';
|
|
|
|
import EditIcon from 'react-icons/lib/md/edit';
|
|
|
|
import TestGenerator from './TestGenerator';
|
|
|
|
import jestTemplate from './redux/jest/template';
|
|
|
|
import mochaTemplate from './redux/mocha/template';
|
|
|
|
import tapeTemplate from './redux/tape/template';
|
|
|
|
import avaTemplate from './redux/ava/template';
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
export const getDefaultTemplates = (/* lib */) =>
|
2018-12-23 03:13:56 +03:00
|
|
|
/*
|
|
|
|
if (lib === 'redux') {
|
|
|
|
return [mochaTemplate, tapeTemplate, avaTemplate];
|
|
|
|
}
|
|
|
|
return [mochaVTemplate, tapeVTemplate, avaVTemplate];
|
|
|
|
*/
|
2019-01-10 21:51:14 +03:00
|
|
|
[jestTemplate, mochaTemplate, tapeTemplate, avaTemplate];
|
2018-12-23 03:13:56 +03:00
|
|
|
|
|
|
|
export default class TestTab extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = { dialogStatus: null };
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:51:14 +03:00
|
|
|
getPersistedState = () => this.props.monitorState.testGenerator || {};
|
2018-12-23 03:13:56 +03:00
|
|
|
|
|
|
|
handleSelectTemplate = selectedTemplate => {
|
|
|
|
const { templates = getDefaultTemplates() } = this.getPersistedState();
|
|
|
|
this.updateState({ selected: templates.indexOf(selectedTemplate) });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCloseTip = () => {
|
|
|
|
this.updateState({ hideTip: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCloseDialog = () => {
|
|
|
|
this.setState({ dialogStatus: null });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSubmit = ({ formData: template }) => {
|
2019-01-10 21:51:14 +03:00
|
|
|
const {
|
|
|
|
templates = getDefaultTemplates(),
|
|
|
|
selected = 0
|
|
|
|
} = this.getPersistedState();
|
2018-12-23 03:13:56 +03:00
|
|
|
if (this.state.dialogStatus === 'Add') {
|
|
|
|
this.updateState({
|
|
|
|
selected: templates.length,
|
|
|
|
templates: [...templates, template]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const editedTemplates = [...templates];
|
|
|
|
editedTemplates[selected] = template;
|
|
|
|
this.updateState({
|
|
|
|
templates: editedTemplates
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.handleCloseDialog();
|
|
|
|
};
|
|
|
|
|
|
|
|
handleRemove = () => {
|
2019-01-10 21:51:14 +03:00
|
|
|
const {
|
|
|
|
templates = getDefaultTemplates(),
|
|
|
|
selected = 0
|
|
|
|
} = this.getPersistedState();
|
2018-12-23 03:13:56 +03:00
|
|
|
this.updateState({
|
|
|
|
selected: 0,
|
2019-01-10 21:51:14 +03:00
|
|
|
templates:
|
|
|
|
templates.length === 1
|
|
|
|
? undefined
|
|
|
|
: [...templates.slice(0, selected), ...templates.slice(selected + 1)]
|
2018-12-23 03:13:56 +03:00
|
|
|
});
|
|
|
|
this.handleCloseDialog();
|
|
|
|
};
|
|
|
|
|
|
|
|
addTemplate = () => {
|
|
|
|
this.setState({ dialogStatus: 'Add' });
|
|
|
|
};
|
|
|
|
|
|
|
|
editTemplate = () => {
|
|
|
|
this.setState({ dialogStatus: 'Edit' });
|
|
|
|
};
|
|
|
|
|
|
|
|
updateState = newState => {
|
|
|
|
this.props.updateMonitorState({
|
|
|
|
testGenerator: {
|
|
|
|
...this.props.monitorState.testGenerator,
|
|
|
|
...newState
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { monitorState, updateMonitorState, ...rest } = this.props; // eslint-disable-line no-unused-vars, max-len
|
|
|
|
const { dialogStatus } = this.state;
|
|
|
|
const persistedState = this.getPersistedState();
|
|
|
|
const { selected = 0, templates = getDefaultTemplates() } = persistedState;
|
|
|
|
const template = templates[selected];
|
|
|
|
const { name, assertion, dispatcher, wrap } = template;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<Toolbar>
|
|
|
|
<Select
|
|
|
|
options={templates}
|
|
|
|
valueKey="name"
|
|
|
|
labelKey="name"
|
|
|
|
value={name}
|
|
|
|
simpleValue={false}
|
|
|
|
onChange={this.handleSelectTemplate}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
<Button onClick={this.editTemplate}>
|
|
|
|
<EditIcon />
|
|
|
|
</Button>
|
|
|
|
<Button onClick={this.addTemplate}>
|
|
|
|
<AddIcon />
|
|
|
|
</Button>
|
2018-12-23 03:13:56 +03:00
|
|
|
</Toolbar>
|
2019-01-10 21:51:14 +03:00
|
|
|
{!assertion ? (
|
|
|
|
<Notification>No template for tests specified.</Notification>
|
|
|
|
) : (
|
2018-12-23 03:13:56 +03:00
|
|
|
<TestGenerator
|
|
|
|
isVanilla={false}
|
|
|
|
assertion={assertion}
|
|
|
|
dispatcher={dispatcher}
|
|
|
|
wrap={wrap}
|
|
|
|
{...rest}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
)}
|
|
|
|
{!persistedState.hideTip && assertion && rest.startActionId === null && (
|
2018-12-23 03:13:56 +03:00
|
|
|
<Notification onClose={this.handleCloseTip}>
|
|
|
|
Hold <b>SHIFT</b> key to select more actions.
|
|
|
|
</Notification>
|
2019-01-10 21:51:14 +03:00
|
|
|
)}
|
|
|
|
{dialogStatus && (
|
2018-12-23 03:13:56 +03:00
|
|
|
<Dialog
|
|
|
|
open
|
|
|
|
title={`${dialogStatus} test template`}
|
|
|
|
onDismiss={this.handleCloseDialog}
|
|
|
|
onSubmit={this.handleSubmit}
|
|
|
|
actions={[
|
2019-01-10 21:51:14 +03:00
|
|
|
<Button key="cancel" onClick={this.handleCloseDialog}>
|
|
|
|
Cancel
|
|
|
|
</Button>,
|
|
|
|
<Button key="remove" onClick={this.handleRemove}>
|
|
|
|
Remove
|
|
|
|
</Button>
|
2018-12-23 03:13:56 +03:00
|
|
|
]}
|
|
|
|
submitText={dialogStatus}
|
|
|
|
schema={formSchema}
|
|
|
|
uiSchema={uiSchema}
|
|
|
|
formData={dialogStatus === 'Edit' ? template : defaultFormData}
|
|
|
|
/>
|
2019-01-10 21:51:14 +03:00
|
|
|
)}
|
2018-12-23 03:13:56 +03:00
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestTab.propTypes = {
|
|
|
|
monitorState: PropTypes.shape({
|
|
|
|
testGenerator: PropTypes.shape({
|
|
|
|
templates: PropTypes.array,
|
|
|
|
selected: PropTypes.number,
|
|
|
|
hideTip: PropTypes.bool
|
|
|
|
})
|
|
|
|
}).isRequired,
|
|
|
|
/*
|
|
|
|
options: PropTypes.shape({
|
|
|
|
lib: PropTypes.string
|
|
|
|
}).isRequired,
|
|
|
|
*/
|
|
|
|
updateMonitorState: PropTypes.func.isRequired
|
|
|
|
};
|