state.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.addEventHandler = exports.dispatchSync = exports.dispatch = exports.setState = exports.getState = exports.ROOT_DESCRIBE_BLOCK_NAME = void 0;
  6. var _types = require('./types');
  7. var _utils = require('./utils');
  8. var _eventHandler = _interopRequireDefault(require('./eventHandler'));
  9. var _formatNodeAssertErrors = _interopRequireDefault(
  10. require('./formatNodeAssertErrors')
  11. );
  12. function _interopRequireDefault(obj) {
  13. return obj && obj.__esModule ? obj : {default: obj};
  14. }
  15. /**
  16. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  17. *
  18. * This source code is licensed under the MIT license found in the
  19. * LICENSE file in the root directory of this source tree.
  20. */
  21. const eventHandlers = [_eventHandler.default, _formatNodeAssertErrors.default];
  22. const ROOT_DESCRIBE_BLOCK_NAME = 'ROOT_DESCRIBE_BLOCK';
  23. exports.ROOT_DESCRIBE_BLOCK_NAME = ROOT_DESCRIBE_BLOCK_NAME;
  24. const ROOT_DESCRIBE_BLOCK = (0, _utils.makeDescribe)(ROOT_DESCRIBE_BLOCK_NAME);
  25. const INITIAL_STATE = {
  26. currentDescribeBlock: ROOT_DESCRIBE_BLOCK,
  27. currentlyRunningTest: null,
  28. expand: undefined,
  29. hasFocusedTests: false,
  30. hasStarted: false,
  31. includeTestLocationInResult: false,
  32. parentProcess: null,
  33. rootDescribeBlock: ROOT_DESCRIBE_BLOCK,
  34. testNamePattern: null,
  35. testTimeout: 5000,
  36. unhandledErrors: []
  37. };
  38. global[_types.STATE_SYM] = INITIAL_STATE;
  39. const getState = () => global[_types.STATE_SYM];
  40. exports.getState = getState;
  41. const setState = state => (global[_types.STATE_SYM] = state);
  42. exports.setState = setState;
  43. const dispatch = async event => {
  44. for (const handler of eventHandlers) {
  45. await handler(event, getState());
  46. }
  47. };
  48. exports.dispatch = dispatch;
  49. const dispatchSync = event => {
  50. for (const handler of eventHandlers) {
  51. handler(event, getState());
  52. }
  53. };
  54. exports.dispatchSync = dispatchSync;
  55. const addEventHandler = handler => {
  56. eventHandlers.push(handler);
  57. };
  58. exports.addEventHandler = addEventHandler;