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,
|
2020-08-08 23:26:39 +03:00
|
|
|
Dialog,
|
2019-01-10 21:51:14 +03:00
|
|
|
} from 'devui';
|
2020-08-09 04:18:45 +03:00
|
|
|
import { MdAdd } from 'react-icons/md';
|
|
|
|
import { MdEdit } from 'react-icons/md';
|
2020-09-13 07:02:24 +03:00
|
|
|
import { Action } from 'redux';
|
|
|
|
import {
|
|
|
|
DevtoolsInspectorState,
|
|
|
|
TabComponentProps,
|
|
|
|
} from 'redux-devtools-inspector-monitor';
|
|
|
|
import { formSchema, uiSchema, defaultFormData } from './templateForm';
|
2018-12-23 03:13:56 +03:00
|
|
|
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';
|
2020-09-13 07:02:24 +03:00
|
|
|
import { Template } from './types';
|
2018-12-23 03:13:56 +03:00
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
export const getDefaultTemplates = (/* lib */): Template[] =>
|
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
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
interface TestGeneratorMonitorState {
|
|
|
|
hideTip?: boolean;
|
|
|
|
selected?: number;
|
|
|
|
templates?: Template[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
dialogStatus: 'Add' | 'Edit' | null;
|
|
|
|
}
|
2018-12-23 03:13:56 +03:00
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
export default class TestTab<S, A extends Action<unknown>> extends Component<
|
|
|
|
TabComponentProps<S, A>,
|
|
|
|
State
|
|
|
|
> {
|
|
|
|
state: State = { dialogStatus: null };
|
2018-12-23 03:13:56 +03:00
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
getPersistedState = (): TestGeneratorMonitorState =>
|
|
|
|
(this.props.monitorState as { testGenerator?: TestGeneratorMonitorState })
|
|
|
|
.testGenerator || {};
|
|
|
|
|
|
|
|
handleSelectTemplate = (selectedTemplate: Template) => {
|
2018-12-23 03:13:56 +03:00
|
|
|
const { templates = getDefaultTemplates() } = this.getPersistedState();
|
|
|
|
this.updateState({ selected: templates.indexOf(selectedTemplate) });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCloseTip = () => {
|
|
|
|
this.updateState({ hideTip: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleCloseDialog = () => {
|
|
|
|
this.setState({ dialogStatus: null });
|
|
|
|
};
|
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
handleSubmit = ({ formData: template }: { formData: Template }) => {
|
2019-01-10 21:51:14 +03:00
|
|
|
const {
|
|
|
|
templates = getDefaultTemplates(),
|
2020-08-08 23:26:39 +03:00
|
|
|
selected = 0,
|
2019-01-10 21:51:14 +03:00
|
|
|
} = this.getPersistedState();
|
2018-12-23 03:13:56 +03:00
|
|
|
if (this.state.dialogStatus === 'Add') {
|
|
|
|
this.updateState({
|
|
|
|
selected: templates.length,
|
2020-08-08 23:26:39 +03:00
|
|
|
templates: [...templates, template],
|
2018-12-23 03:13:56 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const editedTemplates = [...templates];
|
|
|
|
editedTemplates[selected] = template;
|
|
|
|
this.updateState({
|
2020-08-08 23:26:39 +03:00
|
|
|
templates: editedTemplates,
|
2018-12-23 03:13:56 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
this.handleCloseDialog();
|
|
|
|
};
|
|
|
|
|
|
|
|
handleRemove = () => {
|
2019-01-10 21:51:14 +03:00
|
|
|
const {
|
|
|
|
templates = getDefaultTemplates(),
|
2020-08-08 23:26:39 +03:00
|
|
|
selected = 0,
|
2019-01-10 21:51:14 +03:00
|
|
|
} = 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
|
2020-08-08 23:26:39 +03:00
|
|
|
: [...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' });
|
|
|
|
};
|
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
updateState = (newState: TestGeneratorMonitorState) => {
|
2018-12-23 03:13:56 +03:00
|
|
|
this.props.updateMonitorState({
|
|
|
|
testGenerator: {
|
2020-09-13 07:02:24 +03:00
|
|
|
...(this.props.monitorState as {
|
|
|
|
testGenerator?: TestGeneratorMonitorState;
|
|
|
|
}).testGenerator,
|
2020-08-08 23:26:39 +03:00
|
|
|
...newState,
|
|
|
|
},
|
2020-09-13 07:02:24 +03:00
|
|
|
} as Partial<DevtoolsInspectorState>);
|
2018-12-23 03:13:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
2020-09-20 02:24:18 +03:00
|
|
|
<div style={{ flexGrow: 1, zIndex: 100 }}>
|
|
|
|
<Select
|
|
|
|
options={templates}
|
|
|
|
getOptionValue={(template: Template) => template.name}
|
|
|
|
getOptionLabel={(template: Template) => template.name}
|
|
|
|
value={templates.filter((template) => template.name === name)}
|
|
|
|
onChange={this.handleSelectTemplate}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-01-10 21:51:14 +03:00
|
|
|
<Button onClick={this.editTemplate}>
|
2020-08-09 04:18:45 +03:00
|
|
|
<MdEdit />
|
2019-01-10 21:51:14 +03:00
|
|
|
</Button>
|
|
|
|
<Button onClick={this.addTemplate}>
|
2020-08-09 04:18:45 +03:00
|
|
|
<MdAdd />
|
2019-01-10 21:51:14 +03:00
|
|
|
</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>
|
|
|
|
) : (
|
2020-09-13 07:02:24 +03:00
|
|
|
<TestGenerator<S, A>
|
2018-12-23 03:13:56 +03:00
|
|
|
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 && (
|
2020-09-13 07:02:24 +03:00
|
|
|
<Dialog<Template>
|
2018-12-23 03:13:56 +03:00
|
|
|
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
|
2020-08-08 23:26:39 +03:00
|
|
|
</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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-13 07:02:24 +03:00
|
|
|
static 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,
|
|
|
|
};
|
|
|
|
}
|