aboutsummaryrefslogtreecommitdiffstats
path: root/test/scenario/DSLSpec.js
blob: 7a8e2e3bc2f4e192b26bade51ab4942756b693f5 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
describe("DSL", function() {

  var lastDocument, executeFuture, Expect;

  beforeEach(function() {
    setUpContext();
    executeFuture = function(future, html, callback) {
      lastDocument = _jQuery('<div>' + html + '</div>');
      lastFrame = _jQuery('<iframe>' + lastDocument + '</iframe>');
      _jQuery(document.body).append(lastDocument);
      var specThis = {
        testWindow: window,
        testDocument: lastDocument,
        testFrame: lastFrame,
        jQuery: _jQuery
      };
      future.behavior.call(specThis, callback || noop);
    };
    Expect = _window.expect;
  });

  describe("input", function() {

    var input = angular.scenario.dsl.input;

    it('should enter', function() {
      var future = input('name').enter('John');
      expect(future.name).toEqual("input 'name' enter 'John'");
      executeFuture(future, '<input type="text" name="name" />');
      expect(lastDocument.find('input').val()).toEqual('John');
    });

    it('should select', function() {
      var future = input('gender').select('female');
      expect(future.name).toEqual("input 'gender' select 'female'");
      executeFuture(future,
        '<input type="radio" name="0@gender" value="male" checked/>' +
        '<input type="radio" name="0@gender" value="female"/>');
      expect(lastDocument.find(':radio:checked').length).toEqual(1);
      expect(lastDocument.find(':radio:checked').val()).toEqual('female');
    });
  });

  describe('browser', function() {
    var browser = angular.scenario.dsl.browser;
    it('shoud return true if location with empty hash provided is same ' +
        'as location of the page', function() {
      browser.location.href = "http://server";
      expect(browser.location.toEqual("http://server")).toEqual(true);
    });
    it('shoud return true if location with hash provided is same ' +
        'as location of the page', function() {
      browser.location.href = "http://server";
      browser.location.hash = "hashPath";
      expect(browser.location.toEqual("http://server/#/hashPath")).toEqual(true);
    });
    it('should return true if the location provided is the same as which ' +
        'browser navigated to', function() {
      var future = browser.navigateTo("http://server/#/hashPath");
      expect(future.name).toEqual("Navigate to: http://server/#/hashPath");
      executeFuture(future, '<input type="text" name="name" />');
      expect(browser.location.toEqual("http://server/#/hashPath")).toEqual(true);
      expect(browser.location.toEqual("http://server/")).toEqual(false);

      future = browser.navigateTo("http://server/");
      expect(future.name).toEqual("Navigate to: http://server/");
      executeFuture(future, '<input type="text" name="name" />');
      expect(browser.location.toEqual("http://server/")).toEqual(true);
    });
  });

  describe('repeater', function() {

    var repeater = angular.scenario.dsl.repeater;
    var html;
    beforeEach(function() {
      html = "<table>" +
          "<tr class='epic'>" +
            "<td class='hero-name'>" +
              "<span ng:bind='hero'>John Marston</span>" +
            "</td>" +
            "<td class='game-name'>" +
              "<span ng:bind='game'>Red Dead Redemption</span>" +
            "</td>" +
          "</tr>" +
          "<tr class='epic'>" +
            "<td class='hero-name'>" +
              "<span ng:bind='hero'>Nathan Drake</span>" +
            "</td>" +
            "<td class='game-name'>" +
              "<span ng:bind='game'>Uncharted</span>" +
            "</td>" +
          "</tr>" +
        "</table>";
    });
    it('should count', function() {
      var future = repeater('.repeater-row').count();
      expect(future.name).toEqual("repeater '.repeater-row' count");
      executeFuture(future,
        "<div class='repeater-row'>a</div>" +
        "<div class='repeater-row'>b</div>",
        function(value) {
          future.fulfill(value);
      });
      expect(future.fulfilled).toBeTruthy();
      expect(future.value).toEqual(2);
    });

    function assertFutureState(future, expectedName, expectedValue) {
      expect(future.name).toEqual(expectedName);
      executeFuture(future, html, function(value) {
        future.fulfill(value);
      });
      expect(future.fulfilled).toBeTruthy();
      expect(future.value).toEqual(expectedValue);
    }
    it('should collect bindings', function() {
      assertFutureState(repeater('.epic').collect('{{hero}}'),
        "repeater '.epic' collect '{{hero}}'",
        ['John Marston', 'Nathan Drake']);
      assertFutureState(repeater('.epic').collect('{{game}}'),
        "repeater '.epic' collect '{{game}}'",
        ['Red Dead Redemption', 'Uncharted']);
    });
    it('should collect normal selectors', function() {
      assertFutureState(repeater('.epic').collect('.hero-name'),
        "repeater '.epic' collect '.hero-name'",
        ['John Marston', 'Nathan Drake']);
      assertFutureState(repeater('.epic').collect('.game-name'),
        "repeater '.epic' collect '.game-name'",
        ['Red Dead Redemption', 'Uncharted']);
    });
    it('should collect normal attributes', function() {
      //TODO(shyamseshadri) : Left as an exercise to the user
    });
  });

  describe('element', function() {
    var element = angular.scenario.dsl.element;
    var html;
    beforeEach(function() {
      html = '<div class="container">' +
          '<div class="reports-detail">' +
            '<span class="desc">Description : ' +
              '<span ng:bind="report.description">Details...</span>' +
            '</span>' +
            '<span>Date created: ' +
              '<span ng:bind="report.creationDate">01/01/01</span>' +
            '</span>' +
          '</div>' +
        '</div>';
    });
    function timeTravel(future) {
      executeFuture(future, html, function(value) { future.fulfill(value); });
      expect(future.fulfilled).toBeTruthy();
    }
    it('should find elements on the page and provide jquery api', function() {
      var future = element('.reports-detail').text();
      expect(future.name).toEqual("Element '.reports-detail'.text()");
      timeTravel(future);
      expect(future.value).
        toEqual('Description : Details...Date created: 01/01/01');
//      expect(future.value.find('.desc').text()).
//        toEqual('Description : Details...');
    });
    it('should find elements with angular syntax', function() {
      var future = element('{{report.description}}').text();
      expect(future.name).toEqual("Element '{{report.description}}'.text()");
      timeTravel(future);
      expect(future.value).toEqual('Details...');
//      expect(future.value.attr('ng:bind')).toEqual('report.description');
    });
    it('should be able to click elements', function(){
      var future = element('.link-class').click();
      expect(future.name).toEqual("Element '.link-class'.click()");
      executeFuture(future, html, function(value) { future.fulfill(value); });
      expect(future.fulfilled).toBeTruthy();
      // TODO(rajat): look for some side effect from click happening?
    });
  });
});