UNPKG

2.21 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = treeProcessor;
7
8/**
9 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
10 *
11 * This source code is licensed under the MIT license found in the
12 * LICENSE file in the root directory of this source tree.
13 */
14function treeProcessor(options) {
15 const {nodeComplete, nodeStart, queueRunnerFactory, runnableIds, tree} =
16 options;
17
18 function isEnabled(node, parentEnabled) {
19 return parentEnabled || runnableIds.indexOf(node.id) !== -1;
20 }
21
22 function getNodeHandler(node, parentEnabled) {
23 const enabled = isEnabled(node, parentEnabled);
24 return node.children
25 ? getNodeWithChildrenHandler(node, enabled)
26 : getNodeWithoutChildrenHandler(node, enabled);
27 }
28
29 function getNodeWithoutChildrenHandler(node, enabled) {
30 return function fn(done = () => {}) {
31 node.execute(done, enabled);
32 };
33 }
34
35 function getNodeWithChildrenHandler(node, enabled) {
36 return async function fn(done = () => {}) {
37 nodeStart(node);
38 await queueRunnerFactory({
39 onException: error => node.onException(error),
40 queueableFns: wrapChildren(node, enabled),
41 userContext: node.sharedUserContext()
42 });
43 nodeComplete(node);
44 done();
45 };
46 }
47
48 function hasNoEnabledTest(node) {
49 var _node$children$every, _node$children;
50
51 return (
52 node.disabled ||
53 node.markedPending ||
54 ((_node$children$every =
55 (_node$children = node.children) === null || _node$children === void 0
56 ? void 0
57 : _node$children.every(hasNoEnabledTest)) !== null &&
58 _node$children$every !== void 0
59 ? _node$children$every
60 : false)
61 );
62 }
63
64 function wrapChildren(node, enabled) {
65 if (!node.children) {
66 throw new Error('`node.children` is not defined.');
67 }
68
69 const children = node.children.map(child => ({
70 fn: getNodeHandler(child, enabled)
71 }));
72
73 if (hasNoEnabledTest(node)) {
74 return children;
75 }
76
77 return node.beforeAllFns.concat(children).concat(node.afterAllFns);
78 }
79
80 const treeHandler = getNodeHandler(tree, false);
81 return treeHandler();
82}