This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
/
event_source_test.js
111 lines (92 loc) · 2.73 KB
/
event_source_test.js
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
const assert = require('assert');
const brains = require('./helpers/brains');
const Browser = require('../src');
describe('EventSource', function() {
const browser = new Browser();
before(function() {
return brains.ready();
});
before(function() {
brains.static('/streaming', `
<html>
<head>
<script>
var source = new EventSource("/stream");
window.events = [];
source.addEventListener("test", function(event) {
window.events.push(event.data);
});
/*
setTimeout(function() {
window.events.push("timeout");
}, 100);
*/
</script>
</head>
<body>
<button>1</button>
</body>
</html>
`);
brains.get('/stream', function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
// Send first event immediately
setTimeout(function() {
res.write('event: test\nid: 1\ndata: first\n\n');
}, 10);
// Send second event with some delay, still get to see this because of
// client-side timeout
setTimeout(function() {
res.write('event: test\nid: 2\ndata: second\n\n');
}, 50);
// Send third event with too much delay, browser.wait() concluded
setTimeout(function() {
res.write('event: test\nid: 3\ndata: third\n\n');
res.end();
}, 200);
});
});
describe('when present', function() {
before(async function() {
await browser.visit('/streaming');
await browser.pressButton('1');
});
it('pressButton should not timeout', function() {
assert(true);
});
});
describe('wait', function() {
before(async function() {
await browser.visit('/streaming');
await browser.wait();
});
it('should not wait for server event', function() {
assert.deepEqual(browser.window.events, []);
});
});
describe('waitForServer', function() {
before(async function() {
await browser.visit('/streaming');
await browser.waitForServer();
});
it('should capture synchronous event', function() {
assert.deepEqual(browser.window.events, ['first']);
});
it('should not wait longer than specified', function(done) {
function gotTwoEvents(window) {
return (window.events && window.events.length >= 2);
}
browser.waitForServer({ function: gotTwoEvents }, function() {
assert.deepEqual(browser.window.events, ['first', 'second']);
done();
});
});
});
after(function() {
browser.destroy();
});
});