admin/tool/behat/amd/src/steps.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. import Ajax from 'core/ajax';
  16. import Templates from 'core/templates';
  17. import PendingJS from 'core/pending';
  18. /**
  19. * Enhancements for the step definitions page.
  20. *
  21. * @module tool_behat/steps
  22. * @copyright 2022 Catalyst IT EU
  23. * @author Mark Johnson <mark.johnson@catalyst-eu.net>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. /**
  27. * Call the get_entity_generator web service function
  28. *
  29. * Takes the name of an entity generator and returns an object containing a list of the required fields.
  30. *
  31. * @param {String} entityType
  32. * @returns {Promise}
  33. */
  34. const getGeneratorEntities = (entityType) => Ajax.call([{
  35. methodname: 'tool_behat_get_entity_generator',
  36. args: {entitytype: entityType}
  37. }])[0];
  38. /**
  39. * Render HTML for required fields
  40. *
  41. * Takes the entity data returned from getGeneratorEntities and renders the HTML to display the required fields.
  42. *
  43. * @param {String} entityData
  44. * @return {Promise}
  45. */
  46. const getRequiredFieldsContent = (entityData) => {
  47. if (!entityData.required?.length) {
  48. return Promise.resolve({
  49. html: '',
  50. js: ''
  51. });
  52. }
  53. return Templates.renderForPromise('tool_behat/steprequiredfields', {fields: entityData.required});
  54. };
  55. export const init = () => {
  56. // When an entity is selected in the "the following exist" step, fetch and display the required fields.
  57. document.addEventListener('change', async(e) => {
  58. const entityElement = e.target.closest('.entities');
  59. const stepElement = e.target.closest('.stepcontent');
  60. if (!entityElement || !stepElement) {
  61. return;
  62. }
  63. const pendingPromise = new PendingJS('tool_behat/steps:change');
  64. const entityData = await getGeneratorEntities(e.target.value);
  65. const {html, js} = await getRequiredFieldsContent(entityData);
  66. const stepRequiredFields = stepElement.querySelector('.steprequiredfields');
  67. if (stepRequiredFields) {
  68. await Templates.replaceNode(stepRequiredFields, html, js);
  69. } else {
  70. await Templates.appendNodeContents(stepElement, html, js);
  71. }
  72. pendingPromise.resolve();
  73. });
  74. };