41 lines
1.8 KiB
JavaScript
41 lines
1.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.shallowUntilTarget = void 0;
|
|
var enzyme_1 = require("enzyme");
|
|
/**
|
|
* An extention of enzyme's shallow function which will fail to work
|
|
* with decorated components and/or components using the styled() function.
|
|
* This function allows you to pass a 'target' component (e.g. ComponentBase)
|
|
* and keep running shallow on each child component till a match is found.
|
|
*
|
|
* @public
|
|
*/
|
|
function shallowUntilTarget(componentInstance, TargetComponent, options) {
|
|
if (options === void 0) { options = {
|
|
maxTries: 10,
|
|
shallowOptions: {},
|
|
}; }
|
|
var maxTries = options.maxTries, shallowOptions = options.shallowOptions;
|
|
var root = (0, enzyme_1.shallow)(componentInstance, shallowOptions);
|
|
var rootType = root.type();
|
|
if (typeof rootType === 'string' || rootType.toString().indexOf(TargetComponent) !== -1) {
|
|
// Default shallow()
|
|
// If type() is a string then it's a DOM Node.
|
|
// If it were wrapped, it would be a React component.
|
|
return root;
|
|
}
|
|
for (var tries = 1; tries <= maxTries; tries++) {
|
|
// Check for target as a string to avoid conflicts
|
|
// with decoratored components name
|
|
if (rootType.toString().indexOf(TargetComponent) !== -1) {
|
|
// Now that we found the target component, render it.
|
|
return root.first().shallow(shallowOptions);
|
|
}
|
|
// Unwrap the next component in the hierarchy.
|
|
root = root.first().shallow(shallowOptions);
|
|
rootType = root.type();
|
|
}
|
|
throw new Error("Could not find ".concat(TargetComponent, " in React instance: ").concat(componentInstance, ";\n gave up after ").concat(maxTries, " tries"));
|
|
}
|
|
exports.shallowUntilTarget = shallowUntilTarget;
|
|
//# sourceMappingURL=shallowUntilTarget.js.map
|