Skip to content

Commit 4c66bb8

Browse files
committed
tests: update wherever stream/reload used to include scheduler control
1 parent 4c7530a commit 4c66bb8

6 files changed

Lines changed: 103 additions & 96 deletions

File tree

test/specs/api/init.reload.stream.noop.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ describe("API: .stream() noop", function() {
1717
});
1818
it("should can handle a reload + stream call after there IS an instance", function(done) {
1919
var emitterStub;
20-
var bs = browserSync(function() {
20+
var scheduler = require("../../utils").getScheduler();
21+
var bs = browserSync({ debug: { scheduler: scheduler } }, function(
22+
err,
23+
_bs
24+
) {
2125
var stream = bs.stream();
2226

23-
emitterStub = sinon.spy(bs.emitter, "emit");
27+
emitterStub = sinon.spy(_bs.emitter, "emit");
2428

2529
stream.write(new File({ path: "styles.css" }));
2630
stream.end();
2731

32+
scheduler.advanceTo(600);
33+
2834
sinon.assert.calledWithExactly(emitterStub, "file:changed", {
2935
path: "styles.css",
3036
basename: "styles.css",
@@ -33,7 +39,7 @@ describe("API: .stream() noop", function() {
3339
event: "change",
3440
ext: "css"
3541
});
36-
done();
42+
_bs.cleanup(done);
3743
});
3844
});
3945
});

test/specs/commands/reload.js

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,94 @@ var cli = require(path.resolve(pkg.bin)).default;
99
describe("E2E CLI `reload` with no files arg", function() {
1010
it("should make a http request to the protocol with no files arg", function(done) {
1111
browserSync.reset();
12+
var scheduler = require("../../utils").getScheduler();
1213
browserSync
1314
.create()
14-
.init({ server: "test/fixtures", open: false }, function(err, bs) {
15-
var spy = sinon.spy(bs.events, "emit");
15+
.init(
16+
{
17+
server: "test/fixtures",
18+
open: false,
19+
debug: { scheduler: scheduler }
20+
},
21+
function(err, bs) {
22+
var spy = sinon.spy(bs.events, "emit");
1623

17-
cli({
18-
cli: {
19-
input: ["reload"],
20-
flags: {
21-
port: bs.options.get("port")
24+
cli({
25+
cli: {
26+
input: ["reload"],
27+
flags: {
28+
port: bs.options.get("port")
29+
}
30+
},
31+
cb: function() {
32+
scheduler.advanceTo(600);
33+
sinon.assert.calledWithExactly(
34+
spy,
35+
"browser:reload"
36+
);
37+
bs.cleanup();
38+
done();
2239
}
23-
},
24-
cb: function() {
25-
sinon.assert.calledWithExactly(spy, "browser:reload");
26-
bs.cleanup();
27-
done();
28-
}
29-
});
30-
});
40+
});
41+
}
42+
);
3143
});
3244

3345
it("should make a http request with files arg", function(done) {
3446
browserSync.reset();
35-
47+
var scheduler = require("../../utils").getScheduler();
3648
browserSync
3749
.create()
38-
.init({ server: "test/fixtures", open: false }, function(err, bs) {
39-
var spy = sinon.spy(bs.events, "emit");
50+
.init(
51+
{
52+
server: "test/fixtures",
53+
open: false,
54+
debug: { scheduler: scheduler }
55+
},
56+
function(err, bs) {
57+
var spy = sinon.spy(bs.events, "emit");
4058

41-
cli({
42-
cli: {
43-
input: ["reload"],
44-
flags: {
45-
port: bs.options.get("port"),
46-
files: "core.css"
59+
cli({
60+
cli: {
61+
input: ["reload"],
62+
flags: {
63+
port: bs.options.get("port"),
64+
files: "core.css"
65+
}
66+
},
67+
cb: function() {
68+
scheduler.advanceTo(600);
69+
sinon.assert.calledWithExactly(
70+
spy,
71+
"file:changed",
72+
{
73+
path: "core.css",
74+
basename: "core.css",
75+
log: true,
76+
namespace: "core",
77+
event: "change",
78+
ext: "css"
79+
}
80+
);
81+
bs.cleanup();
82+
done();
4783
}
48-
},
49-
cb: function() {
50-
sinon.assert.calledWithExactly(spy, "file:changed", {
51-
path: "core.css",
52-
basename: "core.css",
53-
log: true,
54-
namespace: "core",
55-
event: "change",
56-
ext: "css"
57-
});
58-
bs.cleanup();
59-
done();
60-
}
61-
});
62-
});
84+
});
85+
}
86+
);
6387
});
6488
it("should make a http request with files arg over HTTPS", function(done) {
6589
browserSync.reset();
66-
90+
var scheduler = require("../../utils").getScheduler();
6791
browserSync
6892
.create()
6993
.init(
70-
{ server: "test/fixtures", open: false, https: true },
94+
{
95+
server: "test/fixtures",
96+
open: false,
97+
https: true,
98+
debug: { scheduler: scheduler }
99+
},
71100
function(err, bs) {
72101
var spy = sinon.spy(bs.events, "emit");
73102

@@ -80,6 +109,7 @@ describe("E2E CLI `reload` with no files arg", function() {
80109
}
81110
},
82111
cb: function() {
112+
scheduler.advanceTo(600);
83113
sinon.assert.calledWithExactly(
84114
spy,
85115
"file:changed",

test/specs/e2e/files/e2e.file.changed.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@ var sinon = require("sinon");
55
var assert = require("chai").assert;
66

77
describe("E2E Responding to events", function() {
8-
var instance, socketsStub, clock;
8+
var instance, socketsStub, scheduler;
99

1010
before(function(done) {
1111
browserSync.reset();
12+
scheduler = require("../../../utils").getScheduler();
1213

1314
var config = {
1415
server: {
1516
baseDir: path.join(__dirname, "../../fixtures")
1617
},
1718
files: ["test/fixtures/assets/*.css"],
1819
logLevel: "silent",
19-
open: false
20+
open: false,
21+
debug: {scheduler: scheduler}
2022
};
2123

2224
instance = browserSync(config, function(err, bs) {
2325
socketsStub = sinon.stub(bs.io.sockets, "emit");
2426
done();
2527
}).instance;
26-
27-
clock = sinon.useFakeTimers();
2828
});
2929

3030
afterEach(function() {
3131
socketsStub.reset();
32+
scheduler.clock = 0;
3233
});
3334

3435
after(function() {
3536
instance.io.sockets.emit.restore();
3637
instance.cleanup();
37-
clock.restore();
3838
});
3939

4040
it("fires the file:reload event to the browser", function() {
@@ -46,7 +46,7 @@ describe("E2E Responding to events", function() {
4646
namespace: "core"
4747
});
4848

49-
clock.tick();
49+
scheduler.advanceTo(1000);
5050

5151
var eventName = socketsStub.getCall(0).args[0];
5252
var args = socketsStub.getCall(0).args[1];
@@ -65,7 +65,7 @@ describe("E2E Responding to events", function() {
6565
namespace: "core"
6666
});
6767

68-
clock.tick();
68+
scheduler.advanceTo(1000);
6969

7070
var eventName = socketsStub.getCall(0).args[0];
7171
var args = socketsStub.getCall(0).args[1];
@@ -88,7 +88,7 @@ describe("E2E Responding to events", function() {
8888
namespace: "core"
8989
});
9090

91-
clock.tick();
91+
scheduler.advanceTo(500);
9292

9393
assert.isTrue(socketsStub.withArgs("file:reload").notCalled); // should not be called
9494
assert.isTrue(instance.paused);
@@ -103,7 +103,7 @@ describe("E2E Responding to events", function() {
103103
namespace: "core"
104104
});
105105

106-
clock.tick();
106+
scheduler.advanceTo(1000);
107107

108108
assert.isTrue(socketsStub.withArgs("file:reload").called);
109109
assert.isFalse(instance.paused);
@@ -113,7 +113,7 @@ describe("E2E Responding to events", function() {
113113
// Emit the event as it comes from the file-watcher
114114
instance.events.emit("browser:reload");
115115

116-
clock.tick();
116+
scheduler.advanceTo(1000);
117117

118118
var eventName = socketsStub.getCall(0).args[0];
119119

test/specs/files/files.watching.debounce.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,6 @@ var sinon = require("sinon");
33
var assert = require("chai").assert;
44

55
describe("File Watcher Module - reloadDebounce", function() {
6-
it("Fires as fast as possible with no debounce", function(done) {
7-
browserSync.reset();
8-
var scheduler = require("../../utils").getScheduler();
9-
var config = {
10-
server: "test/fixtures",
11-
open: false,
12-
logLevel: "silent",
13-
reloadDebounce: 0,
14-
online: false,
15-
files: "test/fixtures/*.html",
16-
debug: {
17-
scheduler: scheduler
18-
}
19-
};
20-
browserSync(config, function(err, bs) {
21-
var fn = bs.watchers.core.watchers[0]._events.all;
22-
23-
var stub = sinon.stub(bs.io.sockets, "emit");
24-
25-
fn("change", "index.html");
26-
fn("change", "index.html");
27-
fn("change", "index.html");
28-
fn("change", "index.html");
29-
fn("change", "index.html");
30-
fn("change", "index.html");
31-
fn("change", "index.html");
32-
33-
assert.isTrue(
34-
stub.withArgs("browser:reload").getCalls().length === 7,
35-
"Should emit 7 times"
36-
);
37-
38-
bs.cleanup();
39-
done();
40-
});
41-
});
426
it("only calls file:reload once within the time window", function(done) {
437
browserSync.reset();
448
var scheduler = require("../../utils").getScheduler();

test/specs/files/files.watching.delay.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("File Watcher Module - reloadDelay", function() {
2525
sinon.assert.notCalled(stub.withArgs("file:reload"));
2626

2727
// Advance virtual time beyond the delay
28-
scheduler.advanceTo(1001);
28+
scheduler.advanceTo(1501);
2929

3030
sinon.assert.calledOnce(stub.withArgs("file:reload"));
3131

@@ -54,12 +54,12 @@ describe("File Watcher Module - reloadDelay", function() {
5454
fn("change", "index.html");
5555

5656
// before delay
57-
scheduler.advanceTo(499);
57+
scheduler.advanceTo(999);
5858

5959
sinon.assert.notCalled(stub.withArgs("browser:reload"));
6060

6161
// Advance virtual time beyond the delay
62-
scheduler.advanceTo(501);
62+
scheduler.advanceTo(1000);
6363

6464
sinon.assert.calledOnce(stub.withArgs("browser:reload"));
6565

test/specs/http-protocol/http.reload.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ var sinon = require("sinon");
55
var proto = require("../../../dist/http-protocol");
66

77
describe("HTTP protocol", function() {
8-
var bs, spy;
8+
var bs, spy, scheduler;
99

1010
before(function(done) {
1111
browserSync.reset();
1212

13+
scheduler = require("../../utils").getScheduler();
14+
1315
var config = {
1416
server: "test/fixtures",
1517
logLevel: "info",
1618
open: false,
17-
online: false
19+
online: false,
20+
debug: { scheduler: scheduler }
1821
};
1922

2023
bs = browserSync.init(config, done).instance;
@@ -24,6 +27,7 @@ describe("HTTP protocol", function() {
2427

2528
afterEach(function() {
2629
spy.reset();
30+
scheduler.clock = 0;
2731
});
2832

2933
after(function() {
@@ -37,6 +41,7 @@ describe("HTTP protocol", function() {
3741
);
3842

3943
request(url, function(e, r, body) {
44+
scheduler.advanceTo(500);
4045
sinon.assert.calledWith(spy, "browser:reload");
4146
assert.include(body, "Called public API method `.reload()`");
4247
assert.include(body, "With args: undefined");
@@ -50,6 +55,7 @@ describe("HTTP protocol", function() {
5055
);
5156

5257
request(url, function(e, r, body) {
58+
scheduler.advanceTo(500);
5359
sinon.assert.calledWith(spy, "file:changed");
5460
sinon.assert.calledWithExactly(spy, "file:changed", {
5561
path: "a.css",
@@ -71,13 +77,14 @@ describe("HTTP protocol", function() {
7177
);
7278

7379
request(url, function(e, r, body) {
80+
scheduler.advanceTo(500);
7481
sinon.assert.calledWith(spy, "file:changed", {
7582
path: "somefile.php",
7683
log: true,
7784
namespace: "core",
7885
event: "change"
7986
});
80-
sinon.assert.calledWithExactly(spy, "browser:reload");
87+
sinon.assert.calledWith(spy, "browser:reload");
8188
assert.include(body, "Called public API method `.reload()`");
8289
assert.include(body, 'With args: "somefile.php"');
8390
done();

0 commit comments

Comments
 (0)