UNPKG

1.68 kBJavaScriptView Raw
1import { shallow } from 'enzyme';
2/**
3 * An extention of enzyme's shallow function which will fail to work
4 * with decorated components and/or components using the styled() function.
5 * This function allows you to pass a 'target' component (e.g. ComponentBase)
6 * and keep running shallow on each child component till a match is found.
7 *
8 * @public
9 */
10export function shallowUntilTarget(componentInstance, TargetComponent, options) {
11 if (options === void 0) { options = {
12 maxTries: 10,
13 shallowOptions: {},
14 }; }
15 var maxTries = options.maxTries, shallowOptions = options.shallowOptions;
16 var root = shallow(componentInstance, shallowOptions);
17 var rootType = root.type();
18 if (typeof rootType === 'string' || rootType.toString().indexOf(TargetComponent) !== -1) {
19 // Default shallow()
20 // If type() is a string then it's a DOM Node.
21 // If it were wrapped, it would be a React component.
22 return root;
23 }
24 for (var tries = 1; tries <= maxTries; tries++) {
25 // Check for target as a string to avoid conflicts
26 // with decoratored components name
27 if (rootType.toString().indexOf(TargetComponent) !== -1) {
28 // Now that we found the target component, render it.
29 return root.first().shallow(shallowOptions);
30 }
31 // Unwrap the next component in the hierarchy.
32 root = root.first().shallow(shallowOptions);
33 rootType = root.type();
34 }
35 throw new Error("Could not find " + TargetComponent + " in React instance: " + componentInstance + ";\n gave up after " + maxTries + " tries");
36}
37//# sourceMappingURL=shallowUntilTarget.js.map
\No newline at end of file