tools.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore'
  7. ], function (_) {
  8. 'use strict';
  9. return {
  10. /**
  11. * Processes configuration for a testsuite.
  12. *
  13. * @param {(Object|String)} config - Suite configuration.
  14. * @param {Object} tmplMap - Template map for test cases.
  15. */
  16. init: function (config, tmplMap) {
  17. var preset;
  18. if (_.isString(config)) {
  19. preset = JSON.parse(config);
  20. }
  21. this.applyBase(preset);
  22. if (tmplMap) {
  23. this.applyTmpls(preset, tmplMap);
  24. }
  25. return preset;
  26. },
  27. /**
  28. * Extends first levell properties of provided object
  29. * with a default configuration.
  30. *
  31. * @param {Object} data - Object to be modified.
  32. */
  33. applyBase: function (data) {
  34. var base = data.base = data.base || {};
  35. _.each(data, function (item) {
  36. _.defaults(item, base);
  37. });
  38. },
  39. /**
  40. * Renderes template based on template map and a source data.
  41. *
  42. * @param {Object} source - Data for a lookup.
  43. * @param {Object} map - Template map.
  44. */
  45. applyTmpls: function (source, map) {
  46. _.each(map, function (tmpl, suite) {
  47. suite = source[suite];
  48. suite.tmpl = _.template(tmpl)(suite);
  49. });
  50. },
  51. /**
  52. * Removes element by provided id.
  53. *
  54. * @param {String} id - Id of the element.
  55. */
  56. removeContainer: function (id) {
  57. var node = document.getElementById(id);
  58. if (node) {
  59. node.parentNode.removeChild(node);
  60. }
  61. }
  62. };
  63. });