Skip to content

Commit

Permalink
[Tests] add shallow-failing test for state diffing in sCU after gDSFP
Browse files Browse the repository at this point in the history
  • Loading branch information
chenesan authored and ljharb committed Jan 15, 2019
1 parent d686c91 commit 0db2115
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
31 changes: 30 additions & 1 deletion packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Expand Up @@ -5774,7 +5774,7 @@ describeWithDOM('mount', () => {
]);
});

it('calls GDSFP when expected', () => {
it('calls gDSFP when expected', () => {
const prevProps = { a: 1 };
const state = { state: true };
const wrapper = mount(<GDSFP {...prevProps} />);
Expand Down Expand Up @@ -5821,6 +5821,35 @@ describeWithDOM('mount', () => {
}],
]);
});

it('cDU’s nextState differs from `this.state` when gDSFP returns new state', () => {
class SimpleComponent extends React.Component {
constructor(props) {
super(props);
this.state = { value: props.value };
}

static getDerivedStateFromProps(props, state) {
return props.value === state.value ? null : { value: props.value };
}

shouldComponentUpdate(nextProps, nextState) {
return nextState.value !== this.state.value;
}

render() {
const { value } = this.state;
return (<input value={value} />);
}
}
const wrapper = mount(<SimpleComponent value="initial" />);

expect(wrapper.find('input').prop('value')).to.equal('initial');

wrapper.setProps({ value: 'updated' });

expect(wrapper.find('input').prop('value')).to.equal('updated');
});
});

describeIf(is('>= 16'), 'componentDidCatch', () => {
Expand Down
31 changes: 30 additions & 1 deletion packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Expand Up @@ -6114,7 +6114,7 @@ describe('shallow', () => {
]);
});

it('calls GDSFP when expected', () => {
it('calls gDSFP when expected', () => {
const prevProps = { a: 1 };
const state = { state: true };
const wrapper = shallow(<GDSFP {...prevProps} />);
Expand Down Expand Up @@ -6161,6 +6161,35 @@ describe('shallow', () => {
}],
]);
});

it('cDU’s nextState differs from `this.state` when gDSFP returns new state', () => {
class SimpleComponent extends React.Component {
constructor(props) {
super(props);
this.state = { value: props.value };
}

static getDerivedStateFromProps(props, state) {
return props.value === state.value ? null : { value: props.value };
}

shouldComponentUpdate(nextProps, nextState) {
return nextState.value !== this.state.value;
}

render() {
const { value } = this.state;
return (<input value={value} />);
}
}
const wrapper = shallow(<SimpleComponent value="initial" />);

expect(wrapper.find('input').prop('value')).to.equal('initial');

wrapper.setProps({ value: 'updated' });

expect(wrapper.find('input').prop('value')).to.equal('updated');
});
});

describeIf(is('>= 16'), 'componentDidCatch', () => {
Expand Down

0 comments on commit 0db2115

Please sign in to comment.