treeProcessor.js 2.2 KB

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