Removing need for getDerivedStateFromProps
This commit is contained in:
parent
d3a0746ebb
commit
f7c803a3a6
@ -4,6 +4,7 @@ exports[`App rendering 1`] = `
|
|||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<Translate(Form)
|
<Translate(Form)
|
||||||
downloadUrls={Array []}
|
downloadUrls={Array []}
|
||||||
|
key="expr=undefined&syntax=undefined"
|
||||||
onSubmit={[Function]}
|
onSubmit={[Function]}
|
||||||
syntaxes={
|
syntaxes={
|
||||||
Object {
|
Object {
|
||||||
|
@ -18,6 +18,8 @@ const syntaxes = {
|
|||||||
pcre: 'PCRE'
|
pcre: 'PCRE'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const toUrl = params => new URLSearchParams(params).toString();
|
||||||
|
|
||||||
class App extends React.PureComponent {
|
class App extends React.PureComponent {
|
||||||
state = {}
|
state = {}
|
||||||
|
|
||||||
@ -110,8 +112,7 @@ class App extends React.PureComponent {
|
|||||||
|
|
||||||
handleSubmit = ({expr, syntax}) => {
|
handleSubmit = ({expr, syntax}) => {
|
||||||
if (expr) {
|
if (expr) {
|
||||||
const params = new URLSearchParams({ syntax, expr });
|
document.location.hash = toUrl({ syntax, expr });
|
||||||
document.location.hash = params.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,6 +193,7 @@ class App extends React.PureComponent {
|
|||||||
|
|
||||||
return <React.Fragment>
|
return <React.Fragment>
|
||||||
<Form
|
<Form
|
||||||
|
key={ toUrl({ expr, syntax }) }
|
||||||
syntaxes={ syntaxes }
|
syntaxes={ syntaxes }
|
||||||
downloadUrls={ downloadUrls }
|
downloadUrls={ downloadUrls }
|
||||||
permalinkUrl={ permalinkUrl }
|
permalinkUrl={ permalinkUrl }
|
||||||
|
@ -10,23 +10,8 @@ import style from './style.css';
|
|||||||
|
|
||||||
class Form extends React.PureComponent {
|
class Form extends React.PureComponent {
|
||||||
state = {
|
state = {
|
||||||
syntax: Object.keys(this.props.syntaxes)[0],
|
expr: this.props.expr,
|
||||||
prevProps: {}
|
syntax: this.props.syntax || Object.keys(this.props.syntaxes)[0]
|
||||||
}
|
|
||||||
|
|
||||||
static getDerivedStateFromProps(props, state) {
|
|
||||||
let changes = { prevProps: props };
|
|
||||||
const { prevProps } = state;
|
|
||||||
|
|
||||||
if (props.expr && props.expr !== prevProps.expr) {
|
|
||||||
changes.expr = props.expr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (props.syntax && props.syntax !== prevProps.syntax) {
|
|
||||||
changes.syntax = props.syntax;
|
|
||||||
}
|
|
||||||
|
|
||||||
return changes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleSubmit = event => {
|
handleSubmit = event => {
|
||||||
|
@ -36,49 +36,6 @@ describe('Form', () => {
|
|||||||
expect(component).toMatchSnapshot();
|
expect(component).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('changing expression input', () => {
|
|
||||||
const component = shallow(
|
|
||||||
<Form t={ translate } syntaxes={ syntaxes }/>
|
|
||||||
);
|
|
||||||
const eventObj = {
|
|
||||||
target: { name: 'expr', value: 'Testing value' }
|
|
||||||
};
|
|
||||||
component.find('[name="expr"]').simulate('change', eventObj);
|
|
||||||
|
|
||||||
expect(component.state('expr')).toEqual('Testing value');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('changing syntax input', () => {
|
|
||||||
const component = shallow(
|
|
||||||
<Form t={ translate } syntaxes={ syntaxes }/>
|
|
||||||
);
|
|
||||||
const eventObj = {
|
|
||||||
target: { name: 'syntax', value: 'Testing value' }
|
|
||||||
};
|
|
||||||
component.find('[name="syntax"]').simulate('change', eventObj);
|
|
||||||
|
|
||||||
expect(component.state('syntax')).toEqual('Testing value');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('setting expression and syntax via props', () => {
|
|
||||||
const component = shallow(
|
|
||||||
<Form t={ translate } syntaxes={ syntaxes }/>
|
|
||||||
);
|
|
||||||
expect(component.state()).toEqual(expect.objectContaining({
|
|
||||||
syntax: 'js'
|
|
||||||
}));
|
|
||||||
component.setProps({ expr: 'Testing expression' });
|
|
||||||
expect(component.state()).toEqual(expect.objectContaining({
|
|
||||||
expr: 'Testing expression',
|
|
||||||
syntax: 'js'
|
|
||||||
}));
|
|
||||||
component.setProps({ syntax: 'testing syntax' });
|
|
||||||
expect(component.state()).toEqual(expect.objectContaining({
|
|
||||||
expr: 'Testing expression',
|
|
||||||
syntax: 'testing syntax'
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('submitting expression', () => {
|
describe('submitting expression', () => {
|
||||||
test('submitting form', () => {
|
test('submitting form', () => {
|
||||||
const onSubmit = jest.fn();
|
const onSubmit = jest.fn();
|
||||||
|
Loading…
Reference in New Issue
Block a user