Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!elRef) {
delete elRefs.current[elPath];
return;
}
// Only track instances with state
const { state } = elRef;
if (!state) {
return;
}
elRefs.current[elPath] = elRef;
setInitialState(initialStates.current, elPath, elRef);
const elementId = { decoratorId, elPath };
const fsClassState = findFixtureStateClassState(
lastFixtureState.current,
elementId
);
if (!fsClassState) {
setFixtureState(prevFs => ({
...prevFs,
classState: createFixtureStateClassState({
fixtureState: prevFs,
elementId,
values: createValues(state),
componentName: getComponentName(
elRef.constructor as React.ComponentType
)
})
}));
} else {
elPaths.forEach(elPath => {
const elementId = { decoratorId, elPath };
// Component fixture state can be provided before the fixture mounts (eg.
// a previous snapshot of a fixture state or the current fixture state
// from another renderer)
const fsClassState = findFixtureStateClassState(fixtureState, elementId);
if (!fsClassState) {
if (initialStates.current[elPath]) {
const { state } = initialStates.current[elPath];
const elRef = elRefs.current[elPath];
if (!isEqual(elRef.state, state)) {
replaceState(elRef, state);
}
setFixtureState(prevFs => ({
...prevFs,
classState: createFixtureStateClassState({
fixtureState: prevFs,
elementId,
values: createValues(state),
componentName: getComponentName(
// 1. Element type is stateless
// 2. Element type is a class, but doesn't have state. An instance exists
// but has been discarded because of its lack of state.
// 3. Element instance unmounted and is about to remount. When this
// happens, the new instance will be handled when its ref fires again.
if (!elRef) {
return;
}
// The child's state can be out of sync with the fixture state for two
// reasons:
// 1. The child's state changed internally
// 2. The fixture state changed
// Here we're interested in the second scenario. In the first scenario
// we want to let the component state override the fixture state.
const prevFsClassState = findFixtureStateClassState(
prevFixtureState.current,
elementId
);
if (prevFsClassState && !isEqual(prevFsClassState, fsClassState)) {
return replaceState(
elRef,
extendWithValues(elRef.state, fsClassState.values)
);
}
}
});
}, [
onFixtureStateChange(prevFs => {
const fsClassState = findFixtureStateClassState(prevFs, elementId);
if (!fsClassState) {
console.warn(`Decorator id ${elementId} no longer exists`);
return prevFs;
}
return {
...prevFs,
classState: updateFixtureStateClassState({
fixtureState: prevFs,
elementId,
values
})
};
});
};
setFixtureState(fixtureState => {
const fsClassState = findFixtureStateClassState(fixtureState, elementId);
if (!fsClassState) {
console.warn(`Decorator id ${elementId} no longer exists`);
return fixtureState;
}
const { values } = fsClassState;
return {
...fixtureState,
classState: updateFixtureStateClassState({
fixtureState,
elementId,
values: replaceOrAddItem(values, stateVal => stateVal.key === key, {
serializable: true,
key,
stringified: value
})
({ elementId, componentName, values }) => {
const { decoratorId, elPath } = elementId;
const classState = findFixtureStateClassState(fixtureState, elementId);
return (
<div>
<p>
<strong>{componentName}</strong>
</p>
<div>
<p>Props</p>
{values.map(({ key, serializable, stringified }) => (
</div></div>