summaryrefslogtreecommitdiff
path: root/tests/frontend/specs/helper.js
blob: 7270d479802617a77329c26fc3626667f80d4fe3 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
describe("the test helper", function(){
  describe("the newPad method", function(){
    xit("doesn't leak memory if you creates iframes over and over again", function(done){
      this.timeout(100000);

      var times = 10;

      var loadPad = function(){
        helper.newPad(function(){
          times--;
          if(times > 0){
            loadPad();
          } else {
            done();
          }
        })
      }

      loadPad();
    });

    it("gives me 3 jquery instances of chrome, outer and inner", function(done){
      this.timeout(5000);

      helper.newPad(function(){
        //check if the jquery selectors have the desired elements
        expect(helper.padChrome$("#editbar").length).to.be(1);
        expect(helper.padOuter$("#outerdocbody").length).to.be(1);
        expect(helper.padInner$("#innerdocbody").length).to.be(1);

        //check if the document object was set correctly
        expect(helper.padChrome$.window.document).to.be(helper.padChrome$.document);
        expect(helper.padOuter$.window.document).to.be(helper.padOuter$.document);
        expect(helper.padInner$.window.document).to.be(helper.padInner$.document);

        done();
      });
    });
  });

  describe("the waitFor method", function(){
    it("takes a timeout and waits long enough", function(done){
      this.timeout(2000);
      var startTime = new Date().getTime();

      helper.waitFor(function(){
        return false;
      }, 1500).fail(function(){
        var duration = new Date().getTime() - startTime;
        expect(duration).to.be.greaterThan(1400);
        done();
      });
    });

    it("takes an interval and checks on every interval", function(done){
      this.timeout(4000);
      var checks = 0;

      helper.waitFor(function(){
        checks++;
        return false;
      }, 2000, 100).fail(function(){
        expect(checks).to.be.greaterThan(10);
        expect(checks).to.be.lessThan(30);
        done();
      });
    });

    describe("returns a deferred object", function(){
      it("it calls done after success", function(done){
        helper.waitFor(function(){
          return true;
        }).done(function(){
          done();
        });
      });

      it("calls fail after failure", function(done){
        helper.waitFor(function(){
          return false;
        },0).fail(function(){
          done();
        });
      });

      xit("throws if you don't listen for fails", function(done){
        var onerror = window.onerror;
        window.onerror = function(){
          window.onerror = onerror;
          done();
        }

        helper.waitFor(function(){
          return false;
        },100);
      });
    });
  });

  describe("the selectLines method", function(){
    // function to support tests, use a single way to represent whitespaces
    var cleanText = function(text){
      return text
      // IE replaces line breaks with a whitespace, so we need to unify its behavior
      // for other browsers, to have all tests running for all browsers
      .replace(/\n/gi, " ")
      .replace(/\s/gi, " ");
    }

    before(function(done){
      helper.newPad(function() {
        // create some lines to be used on the tests
        var $firstLine = helper.padInner$("div").first();
        $firstLine.sendkeys("{selectall}some{enter}short{enter}lines{enter}to test{enter}{enter}");

        // wait for lines to be split
        helper.waitFor(function(){
          var $fourthLine = helper.padInner$("div").slice(3,4);
          return $fourthLine.text() === "to test";
        }).done(done);
      });

      this.timeout(60000);
    });

    it("changes editor selection to be between startOffset of $startLine and endOffset of $endLine", function(done){
      var inner$ = helper.padInner$;

      var startOffset = 2;
      var endOffset   = 4;

      var $lines     = inner$("div");
      var $startLine = $lines.slice(1,2);
      var $endLine   = $lines.slice(3,4);

      helper.selectLines($startLine, $endLine, startOffset, endOffset);

      var selection = inner$.document.getSelection();
      expect(cleanText(selection.toString())).to.be("ort lines to t");

      done();
    });

    it("ends selection at beginning of $endLine when it is an empty line", function(done){
      var inner$ = helper.padInner$;

      var startOffset = 2;
      var endOffset   = 1;

      var $lines     = inner$("div");
      var $startLine = $lines.slice(1,2);
      var $endLine   = $lines.slice(4,5);

      helper.selectLines($startLine, $endLine, startOffset, endOffset);

      var selection = inner$.document.getSelection();
      expect(cleanText(selection.toString())).to.be("ort lines to test");

      done();
    });

    it("ends selection at beginning of $endLine when its offset is zero", function(done){
      var inner$ = helper.padInner$;

      var startOffset = 2;
      var endOffset   = 0;

      var $lines     = inner$("div");
      var $startLine = $lines.slice(1,2);
      var $endLine   = $lines.slice(3,4);

      helper.selectLines($startLine, $endLine, startOffset, endOffset);

      var selection = inner$.document.getSelection();
      expect(cleanText(selection.toString())).to.be("ort lines ");

      done();
    });

    it("selects full line when offset is longer than line content", function(done){
      var inner$ = helper.padInner$;

      var startOffset = 2;
      var endOffset   = 50;

      var $lines     = inner$("div");
      var $startLine = $lines.slice(1,2);
      var $endLine   = $lines.slice(3,4);

      helper.selectLines($startLine, $endLine, startOffset, endOffset);

      var selection = inner$.document.getSelection();
      expect(cleanText(selection.toString())).to.be("ort lines to test");

      done();
    });

    it("selects all text between beginning of $startLine and end of $endLine when no offset is provided", function(done){
      var inner$ = helper.padInner$;

      var $lines     = inner$("div");
      var $startLine = $lines.slice(1,2);
      var $endLine   = $lines.slice(3,4);

      helper.selectLines($startLine, $endLine);

      var selection = inner$.document.getSelection();
      expect(cleanText(selection.toString())).to.be("short lines to test");

      done();
    });
  });
});