redux-devtools/packages/redux-devtools-test-generator/src/index.js

184 lines
5.0 KiB
JavaScript
Raw Normal View History

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,
2019-01-10 21:51:14 +03:00
} from 'devui';
import { formSchema, uiSchema, defaultFormData } from './templateForm';
import { MdAdd } from 'react-icons/md';
import { MdEdit } from 'react-icons/md';
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 */) =>
/*
if (lib === 'redux') {
return [mochaTemplate, tapeTemplate, avaTemplate];
}
return [mochaVTemplate, tapeVTemplate, avaVTemplate];
*/
2019-01-10 21:51:14 +03:00
[jestTemplate, mochaTemplate, tapeTemplate, avaTemplate];
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 || {};
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,
2019-01-10 21:51:14 +03:00
} = this.getPersistedState();
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,
2019-01-10 21:51:14 +03:00
} = this.getPersistedState();
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)],
});
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}>
<MdEdit />
2019-01-10 21:51:14 +03:00
</Button>
<Button onClick={this.addTemplate}>
<MdAdd />
2019-01-10 21:51:14 +03:00
</Button>
</Toolbar>
2019-01-10 21:51:14 +03:00
{!assertion ? (
<Notification>No template for tests specified.</Notification>
) : (
<TestGenerator
isVanilla={false}
assertion={assertion}
dispatcher={dispatcher}
wrap={wrap}
{...rest}
/>
2019-01-10 21:51:14 +03:00
)}
{!persistedState.hideTip && assertion && rest.startActionId === null && (
<Notification onClose={this.handleCloseTip}>
Hold <b>SHIFT</b> key to select more actions.
</Notification>
2019-01-10 21:51:14 +03:00
)}
{dialogStatus && (
<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>,
]}
submitText={dialogStatus}
schema={formSchema}
uiSchema={uiSchema}
formData={dialogStatus === 'Edit' ? template : defaultFormData}
/>
2019-01-10 21:51:14 +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,
};