Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const isAlreadyMocked = Boolean(reactEl._mock);
if (isAlreadyMocked) {
reactEl._mock._render();
}
if (!isAlreadyMocked && ReactIs.isForwardRef(reactEl)) {
const shallowRender = new ShallowRender(reactEl, config);
return {
...shallowRender._rendered,
_mock: shallowRender
};
}
// When rendering modify the context value
if (ReactIs.isContextProvider(reactEl) && reactEl.props.value) {
reactEl.type._context._currentValue = reactEl.props.value;
}
if (!isAlreadyMocked && shouldBeRender(reactEl, config)) {
const mock = config.mocks && config.mocks[reactEl.type.displayName || reactEl.type.name];
const el = mock === true ? reactEl.type : mock || reactEl.type;
const shallowRender = new ShallowRender(
React.createElement(
el,
reactEl.props,
reactEl.props && reactEl.props.children
),
config
);
function prepareElement(element, context) {
if (element === null || typeof element !== 'object') {
return Promise.resolve([null, context]);
}
const {type, props} = element;
if (isContextConsumer(element)) {
return Promise.resolve([props.children(type._currentValue), context]);
}
if (isContextProvider(element)) {
type._context._currentValue = props.value;
return Promise.resolve([props.children, context]);
}
if (typeof type === 'string' || isFragment(element)) {
return Promise.resolve([props.children, context]);
}
if (!isReactCompositeComponent(type)) {
return Promise.resolve([type(props, context), context]);
}
const CompositeComponent = type;
const instance = new CompositeComponent(props, context);
instance.props = props;
instance.context = context;
return prepareComponentInstance(instance).then(prepareConfig => {
// Stop traversing if the component is defer or boundary
if (prepareConfig.defer || prepareConfig.boundary) {
const serializeChild = (child) => {
if (Array.isArray(child)) {
return serializeChildren(child)
} else if (
(ReactIs.isContextConsumer(child) || ReactIs.isContextProvider(child)) &&
child.props && child.props.children
) {
if (isFunction(child.props.children)) {
return serializeElement(child.props.children(child.type._currentValue))
} else {
return serializeChildren(child.props.children)
}
} else if (child != null && !Array.isArray(child) && React.isValidElement(child)) {
return serializeElement(child)
} else {
return child
}
}
isCustomComponent(type) {
const fakeElement = makeFakeElement(type);
return !!type && (
typeof type === 'function'
|| isForwardRef(fakeElement)
|| isContextProvider(fakeElement)
|| isContextConsumer(fakeElement)
);
}
isCustomComponent(type) {
const fakeElement = makeFakeElement(type);
return !!type && (
typeof type === 'function'
|| isForwardRef(fakeElement)
|| isContextProvider(fakeElement)
|| isContextConsumer(fakeElement)
|| isSuspense(fakeElement)
);
}