{"size": 4105, "ext": "js", "max_stars_repo_path": "static/js/keiken/tests/loadTestEnv.js", "max_stars_repo_name": "fvclaus/photomap", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "static/js/keiken/tests/loadTestEnv.js", "max_issues_repo_name": "fvclaus/photomap", "max_issues_repo_issues_event_min_datetime": "2019-12-11T17:21:51.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T06:09:41.000Z", "max_forks_repo_path": "static/js/keiken/tests/loadTestEnv.js", "max_forks_repo_name": "fvclaus/photomap", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 10, "max_forks_count": null, "avg_line_length": 30.6343283582, "max_line_length": 94, "alphanum_fraction": 0.5395858709}
define(["dojo/_base/declare",
  "../util/Communicator",
  "./JQueryMatchers",
  "node_modules/jasmine-jquery-matchers/dist/jasmine-jquery-matchers",
  "dojo/domReady!"], function (declare, communicator, jQueryMatchers, jasmineJqueryMatchers) {
  // Make matcher available to all specs
  beforeEach(function () {
    jasmine.addMatchers(jQueryMatchers)
    jasmine.addMatchers(jasmineJqueryMatchers)
  })

  return {
    // eslint-disable-next-line no-unused-vars
    load: function (id, require, callback) {
      var $testBody = $("#testBody")
      if (!$testBody.length) {
        if (!document.body) {
          document.body = document.createElement("body")
        }
        $testBody = $("<div id='testBody'></div>")
          .appendTo(document.body)
      }
      var TestEnv = declare(null, {
        createWidget: function (params, Widget, $container) {
          if ($container) {
            $container = TestEnv.append($container)
          } else {
            $container = this.createContainer()
          }
          this.widget = new Widget(params || {}, $container.get(0))
          this.$container = TestEnv.findContainer().attr("id", this.widget.viewName)
          this.domNode = this.$container.get(0)
          return this
        },
        createContainer: function (id) {
          var $container = $("<div/>")
          // Avoid creating temporary ids (dijit widgets are indexed by id)
          if (id) {
            $container.attr("id", id)
          }
          $testBody
            .empty()
            .append($container)
          return TestEnv.findContainer()
        },

        destroyWidget: function () {
          this.widget.destroy()
        }
      })

      TestEnv.findContainer = function () {
        return $testBody.find(":first-child").first()
      }
      TestEnv.createCustomContainer = function (elements) {
        $testBody
          .empty()
          .append(elements)
        return TestEnv.findContainer()
      }
      // TODO Rename this
      TestEnv.append = function (html) {
        var $els = $(html)
        $testBody
          .append($els)
        return $els
      }

      var executeTestFn = function (testFn, done) {
        return function () {
          // We must release the subscription function
          // otherwise a new publish event could result in an infinte loop.
          setTimeout(function () {
            switch (testFn.length) {
              case 0:
                try {
                  testFn()
                } catch (e) {
                  console.error(e)
                  throw e
                } finally {
                  done()
                }
                break
              case 1:
                testFn(done)
                break
              default:
                console.error(name, "testFn has too many arguments")
            }
          })
        }
      }

      var executeSetupFn = function (setupFn, additionalArgs) {
        try {
          setupFn.apply(null, additionalArgs)
        } catch (e) {
          console.error("Error while preparing it " + e)
          throw e
        }
      }

      TestEnv.wrapJasmineItAsyncSetup = function (registerAsyncFn, setupFn) {
        return function (name, testFn) {
          var args = Array.prototype.slice.call(arguments)
          it(name, function (done) {
            registerAsyncFn(executeTestFn(testFn, done))
            executeSetupFn(setupFn, args.slice(2))
          })
        }
      }

      TestEnv.wrapJasmineItSyncSetup = function (setupFn) {
        return function (name, testFn) {
          var args = Array.prototype.slice.call(arguments)
          it(name, function (done) {
            executeSetupFn(setupFn, args.slice(2))
            executeTestFn(testFn, done)()
          })
        }
      }

      TestEnv.waitForPublishEvent = function (eventName, triggerPublishFn) {
        return TestEnv.wrapJasmineItAsyncSetup(function (testFnWrapper) {
          return communicator.subscribeOnce(eventName, testFnWrapper)
        }, triggerPublishFn)
      }

      callback(TestEnv)
    }
  }
})
