bind.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = bind;
  6. function _jestUtil() {
  7. const data = require('jest-util');
  8. _jestUtil = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _array = _interopRequireDefault(require('./table/array'));
  14. var _template = _interopRequireDefault(require('./table/template'));
  15. var _validation = require('./validation');
  16. function _interopRequireDefault(obj) {
  17. return obj && obj.__esModule ? obj : {default: obj};
  18. }
  19. /**
  20. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. *
  25. */
  26. function bind(cb, supportsDone = true) {
  27. return (table, ...taggedTemplateData) =>
  28. function eachBind(title, test, timeout) {
  29. try {
  30. const tests = isArrayTable(taggedTemplateData)
  31. ? buildArrayTests(title, table)
  32. : buildTemplateTests(title, table, taggedTemplateData);
  33. return tests.forEach(row =>
  34. cb(
  35. row.title,
  36. applyArguments(supportsDone, row.arguments, test),
  37. timeout
  38. )
  39. );
  40. } catch (e) {
  41. const error = new (_jestUtil().ErrorWithStack)(e.message, eachBind);
  42. return cb(title, () => {
  43. throw error;
  44. });
  45. }
  46. };
  47. }
  48. const isArrayTable = data => data.length === 0;
  49. const buildArrayTests = (title, table) => {
  50. (0, _validation.validateArrayTable)(table);
  51. return (0, _array.default)(title, table);
  52. };
  53. const buildTemplateTests = (title, table, taggedTemplateData) => {
  54. const headings = getHeadingKeys(table[0]);
  55. (0, _validation.validateTemplateTableArguments)(headings, taggedTemplateData);
  56. return (0, _template.default)(title, headings, taggedTemplateData);
  57. };
  58. const getHeadingKeys = headings =>
  59. (0, _validation.extractValidTemplateHeadings)(headings)
  60. .replace(/\s/g, '')
  61. .split('|');
  62. const applyArguments = (supportsDone, params, test) =>
  63. supportsDone && params.length < test.length
  64. ? done => test(...params, done)
  65. : () => test(...params);