/app/javascript/testbase.js

https://gitlab.com/gregtyka/server · JavaScript · 103 lines · 67 code · 8 blank · 28 comment · 7 complexity · 51d85963b6b430e6e4eef5098b70b419 MD5 · raw file

  1. /**
  2. * Base utilities for running a JavaScript Unit Test, or demo file.
  3. */
  4. var KA_TEST = {
  5. BASE_PATH_: "",
  6. computeBasePath: function() {
  7. var doc = window.document;
  8. var scripts = doc.getElementsByTagName("script");
  9. // Search backwards since the current script is in almost all cases the one
  10. // that has testbase.js.
  11. for (var i = scripts.length - 1; i >= 0; --i) {
  12. var src = scripts[i].src;
  13. if (src.substr(src.length - 11, 11) == "testbase.js") {
  14. return src.substr(0, src.length - 11);
  15. }
  16. }
  17. return null;
  18. },
  19. /**
  20. * Injects a script into the page.
  21. * @param {string} path The relative path to the script with trailing "/"
  22. * @param {string} name The name of the script with the ".js" suffix
  23. */
  24. writeScript: function(path, name) {
  25. document.write("<script src=\"" + path + name + "\"></script>");
  26. },
  27. init: function() {
  28. var basePath = this.BASE_PATH_ = this.computeBasePath();
  29. if (basePath === null) {
  30. // Error - it should be an empty string if anything.
  31. console.log("Can't compute the base path for the test");
  32. }
  33. var sharedPackagePath = basePath + "/shared-package/";
  34. // Write out some common utilities which are probably going to be
  35. // needed in most tests.
  36. this.writeScript(sharedPackagePath, "jquery.js");
  37. this.writeScript(sharedPackagePath, "jquery-ui.js");
  38. this.writeScript(basePath + "../../khan-exercises/utils/",
  39. "underscore.js");
  40. this.writeScript(sharedPackagePath, "backbone.js");
  41. this.writeScript(sharedPackagePath, "handlebars.js");
  42. this.writeScript(sharedPackagePath, "templates.js");
  43. },
  44. /**
  45. * The list of templates that need to be loaded still.
  46. */
  47. outstandingTemplates: [],
  48. onTemplatesLoaded: function() {},
  49. /**
  50. * Load a single template asnychronously.
  51. */
  52. loadTemplateForTest: function(name) {
  53. var parts = name.split(".");
  54. var path = parts[0] + "-package/" + parts[1] + ".handlebars";
  55. $.ajax({
  56. type: "GET",
  57. url: KA_TEST.BASE_PATH_ + path,
  58. success: function(data) {
  59. var canonicalName = Templates.getCanonicalName(name);
  60. Templates.cache_[canonicalName] = Handlebars.compile(data);
  61. var index = $.inArray(name, KA_TEST.outstandingTemplates);
  62. if (index < 0) {
  63. // Shouldn't happen!
  64. return;
  65. }
  66. KA_TEST.outstandingTemplates.splice(index, 1);
  67. if (!KA_TEST.outstandingTemplates.length) {
  68. KA_TEST.onTemplatesLoaded();
  69. }
  70. },
  71. error: function() {
  72. console.log("Can't load template " + name);
  73. }
  74. });
  75. },
  76. /**
  77. * Asynchronously loads templates from source and prepares them for the test.
  78. * This is needed since templates are compiled in production into a package, and
  79. * needs to be manually made available when requiring them in test code.
  80. * @param {string|Array.<string>} names The names of the templates to load
  81. * @param {Function} onLoaded The method to be executed once all templates are
  82. * loaded. Typically, this is the code that renders the demo/tests.
  83. */
  84. loadTemplates: function(names, onLoaded) {
  85. if (typeof names === "String") {
  86. names = [names];
  87. }
  88. this.onTemplatesLoaded = onLoaded;
  89. this.outstandingTemplates = names;
  90. $.each(names, function(i, name) {
  91. KA_TEST.loadTemplateForTest(name);
  92. });
  93. }
  94. };
  95. KA_TEST.init();