Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { children, state: mockedState, fixtureState } = this.props;
// Re-render if child type or props changed (eg. via webpack HMR)
if (
!isEqual(nextChildren, children) ||
!isEqual(nextMockedState, mockedState)
) {
return true;
}
if (nextFixtureState === fixtureState) {
return false;
}
const instanceId = getInstanceId(this);
const next = getFixtureStateStateInst(nextFixtureState, instanceId);
const prev = getFixtureStateStateInst(fixtureState, instanceId);
// Deeper comparisons are made in componentDidUpdate to avoid redundant
// setState calls to child ref
return next !== prev;
}
// Re-render if child type or props changed (eg. via webpack HMR)
if (
!isEqual(nextChildren, children) ||
!isEqual(nextMockedState, mockedState)
) {
return true;
}
if (nextFixtureState === fixtureState) {
return false;
}
const instanceId = getInstanceId(this);
const next = getFixtureStateStateInst(nextFixtureState, instanceId);
const prev = getFixtureStateStateInst(fixtureState, instanceId);
// Deeper comparisons are made in componentDidUpdate to avoid redundant
// setState calls to child ref
return next !== prev;
}
componentDidUpdate({ state: prevMockedState }) {
const { childRef } = this;
if (!childRef) {
return;
}
const { fixtureState, state: mockedState } = this.props;
const instanceId = getInstanceId(this);
const stateInstance = getFixtureStateStateInst(fixtureState, instanceId);
// Reset fixture state if...
if (
// ...the fixture state associated with this instance (initially created
// in handleRef) has been emptied deliberately. This is an edge case that
// occurs when a user interacting with a fixture desires to discard the
// current fixture state and load the fixture from scatch.
!stateInstance ||
// ...mocked state from fixture element changed, likely via webpack HMR.
!isEqual(mockedState, prevMockedState)
) {
return this.resetState(childRef);
}
this.replaceState(
childRef,
fixtureState
} = this.props;
this.childRef = childRef;
// Call any previously defined ref from the child element
if (prevRef) {
prevRef(childRef);
}
if (!childRef) {
return;
}
const instanceId = getInstanceId(this);
const stateInstance = getFixtureStateStateInst(fixtureState, instanceId);
// Fixture state already exists for this instance, which means that this
// isn't the first child ref. The child type has likely been replaced
// (eg. via webpack HMR). Conversely, the child instance might reset due
// to resetting the renderKey in CaptureProps. Regardless, apply the
// fixture state to the new child instance.
if (stateInstance) {
return this.replaceState(
childRef,
extendMockedStateWithFixtureState(mockedState, stateInstance)
);
}
if (childRef.state) {
this.initialState = childRef.state;
}