aboutsummaryrefslogtreecommitdiffstats
path: root/src/scenario/Runner.js
blob: ff20d1d1caa3ef54534e6c355a0eb60df4903bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * Runner for scenarios.
 */
angular.scenario.Runner = function($window) {
  this.$window = $window;
  this.rootDescribe = new angular.scenario.Describe();
  this.currentDescribe = this.rootDescribe;
  this.api = {
    it: this.it,
    xit: angular.noop,
    describe: this.describe,
    xdescribe: angular.noop,
    beforeEach: this.beforeEach,
    afterEach: this.afterEach
  };
  angular.foreach(this.api, angular.bind(this, function(fn, key) {
    this.$window[key] = angular.bind(this, fn);
  }));
};

/**
 * Defines a describe block of a spec.
 *
 * @param {String} Name of the block
 * @param {Function} Body of the block
 */
angular.scenario.Runner.prototype.describe = function(name, body) {
  var self = this;
  this.currentDescribe.describe(name, function() {
    var parentDescribe = self.currentDescribe;
    self.currentDescribe = this;
    try {
      body.call(this);
    } finally {
      self.currentDescribe = parentDescribe;
    }
  });
};

/**
 * Defines a test in a describe block of a spec.
 *
 * @param {String} Name of the block
 * @param {Function} Body of the block
 */
angular.scenario.Runner.prototype.it = function(name, body) {
  this.currentDescribe.it(name, body);
};

/**
 * Defines a function to be called before each it block in the describe
 * (and before all nested describes).
 *
 * @param {Function} Callback to execute
 */
angular.scenario.Runner.prototype.beforeEach = function(body) {
  this.currentDescribe.beforeEach(body);
};

/**
 * Defines a function to be called after each it block in the describe
 * (and before all nested describes).
 *
 * @param {Function} Callback to execute
 */
angular.scenario.Runner.prototype.afterEach = function(body) {
  this.currentDescribe.afterEach(body);
};

/**
 * Defines a function to be called before each it block in the describe
 * (and before all nested describes).
 *
 * @param {Function} Callback to execute
 */
angular.scenario.Runner.prototype.run = function(ui, application, specRunnerClass, specsDone) {
  var $root = angular.scope({}, angular.service);
  var self = this;
  var specs = this.rootDescribe.getSpecs();
  $root.application = application;
  $root.ui = ui;
  $root.setTimeout = function() {
    return self.$window.setTimeout.apply(self.$window, arguments);
  };
  asyncForEach(specs, angular.bind(this, function(spec, specDone) {
    var runner = angular.scope($root);
    runner.$become(specRunnerClass);
    angular.foreach(angular.scenario.dsl, angular.bind(this, function(fn, key) {
      this.$window[key] = function() {
        return fn.call($root).apply(angular.scope(runner), arguments);
      };
    }));
    runner.run(ui, spec, specDone);
  }), specsDone || angular.noop);
};