treeProcessor.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 {
  14. nodeComplete,
  15. nodeStart,
  16. queueRunnerFactory,
  17. runnableIds,
  18. tree
  19. } = options;
  20. function isEnabled(node, parentEnabled) {
  21. return parentEnabled || runnableIds.indexOf(node.id) !== -1;
  22. }
  23. function getNodeHandler(node, parentEnabled) {
  24. const enabled = isEnabled(node, parentEnabled);
  25. return node.children
  26. ? getNodeWithChildrenHandler(node, enabled)
  27. : getNodeWithoutChildrenHandler(node, enabled);
  28. }
  29. function getNodeWithoutChildrenHandler(node, enabled) {
  30. return function fn(done = () => {}) {
  31. node.execute(done, enabled);
  32. };
  33. }
  34. function getNodeWithChildrenHandler(node, enabled) {
  35. return async function fn(done = () => {}) {
  36. nodeStart(node);
  37. await queueRunnerFactory({
  38. onException: error => node.onException(error),
  39. queueableFns: wrapChildren(node, enabled),
  40. userContext: node.sharedUserContext()
  41. });
  42. nodeComplete(node);
  43. done();
  44. };
  45. }
  46. function hasNoEnabledTest(node) {
  47. if (node.children) {
  48. return node.children.every(hasNoEnabledTest);
  49. }
  50. return node.disabled || node.markedPending;
  51. }
  52. function wrapChildren(node, enabled) {
  53. if (!node.children) {
  54. throw new Error('`node.children` is not defined.');
  55. }
  56. const children = node.children.map(child => ({
  57. fn: getNodeHandler(child, enabled)
  58. }));
  59. if (hasNoEnabledTest(node)) {
  60. return children;
  61. }
  62. return node.beforeAllFns.concat(children).concat(node.afterAllFns);
  63. }
  64. const treeHandler = getNodeHandler(tree, false);
  65. return treeHandler();
  66. }