Skip to content

Commit

Permalink
tests: update wherever stream/reload used to include scheduler control
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 23, 2017
1 parent 4c7530a commit 4c66bb8
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 96 deletions.
12 changes: 9 additions & 3 deletions test/specs/api/init.reload.stream.noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ describe("API: .stream() noop", function() {
});
it("should can handle a reload + stream call after there IS an instance", function(done) {
var emitterStub;
var bs = browserSync(function() {
var scheduler = require("../../utils").getScheduler();
var bs = browserSync({ debug: { scheduler: scheduler } }, function(
err,
_bs
) {
var stream = bs.stream();

emitterStub = sinon.spy(bs.emitter, "emit");
emitterStub = sinon.spy(_bs.emitter, "emit");

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

scheduler.advanceTo(600);

sinon.assert.calledWithExactly(emitterStub, "file:changed", {
path: "styles.css",
basename: "styles.css",
Expand All @@ -33,7 +39,7 @@ describe("API: .stream() noop", function() {
event: "change",
ext: "css"
});
done();
_bs.cleanup(done);
});
});
});
112 changes: 71 additions & 41 deletions test/specs/commands/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,94 @@ var cli = require(path.resolve(pkg.bin)).default;
describe("E2E CLI `reload` with no files arg", function() {
it("should make a http request to the protocol with no files arg", function(done) {
browserSync.reset();
var scheduler = require("../../utils").getScheduler();
browserSync
.create()
.init({ server: "test/fixtures", open: false }, function(err, bs) {
var spy = sinon.spy(bs.events, "emit");
.init(
{
server: "test/fixtures",
open: false,
debug: { scheduler: scheduler }
},
function(err, bs) {
var spy = sinon.spy(bs.events, "emit");

cli({
cli: {
input: ["reload"],
flags: {
port: bs.options.get("port")
cli({
cli: {
input: ["reload"],
flags: {
port: bs.options.get("port")
}
},
cb: function() {
scheduler.advanceTo(600);
sinon.assert.calledWithExactly(
spy,
"browser:reload"
);
bs.cleanup();
done();
}
},
cb: function() {
sinon.assert.calledWithExactly(spy, "browser:reload");
bs.cleanup();
done();
}
});
});
});
}
);
});

it("should make a http request with files arg", function(done) {
browserSync.reset();

var scheduler = require("../../utils").getScheduler();
browserSync
.create()
.init({ server: "test/fixtures", open: false }, function(err, bs) {
var spy = sinon.spy(bs.events, "emit");
.init(
{
server: "test/fixtures",
open: false,
debug: { scheduler: scheduler }
},
function(err, bs) {
var spy = sinon.spy(bs.events, "emit");

cli({
cli: {
input: ["reload"],
flags: {
port: bs.options.get("port"),
files: "core.css"
cli({
cli: {
input: ["reload"],
flags: {
port: bs.options.get("port"),
files: "core.css"
}
},
cb: function() {
scheduler.advanceTo(600);
sinon.assert.calledWithExactly(
spy,
"file:changed",
{
path: "core.css",
basename: "core.css",
log: true,
namespace: "core",
event: "change",
ext: "css"
}
);
bs.cleanup();
done();
}
},
cb: function() {
sinon.assert.calledWithExactly(spy, "file:changed", {
path: "core.css",
basename: "core.css",
log: true,
namespace: "core",
event: "change",
ext: "css"
});
bs.cleanup();
done();
}
});
});
});
}
);
});
it("should make a http request with files arg over HTTPS", function(done) {
browserSync.reset();

var scheduler = require("../../utils").getScheduler();
browserSync
.create()
.init(
{ server: "test/fixtures", open: false, https: true },
{
server: "test/fixtures",
open: false,
https: true,
debug: { scheduler: scheduler }
},
function(err, bs) {
var spy = sinon.spy(bs.events, "emit");

Expand All @@ -80,6 +109,7 @@ describe("E2E CLI `reload` with no files arg", function() {
}
},
cb: function() {
scheduler.advanceTo(600);
sinon.assert.calledWithExactly(
spy,
"file:changed",
Expand Down
20 changes: 10 additions & 10 deletions test/specs/e2e/files/e2e.file.changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ var sinon = require("sinon");
var assert = require("chai").assert;

describe("E2E Responding to events", function() {
var instance, socketsStub, clock;
var instance, socketsStub, scheduler;

before(function(done) {
browserSync.reset();
scheduler = require("../../../utils").getScheduler();

var config = {
server: {
baseDir: path.join(__dirname, "../../fixtures")
},
files: ["test/fixtures/assets/*.css"],
logLevel: "silent",
open: false
open: false,
debug: {scheduler: scheduler}
};

instance = browserSync(config, function(err, bs) {
socketsStub = sinon.stub(bs.io.sockets, "emit");
done();
}).instance;

clock = sinon.useFakeTimers();
});

afterEach(function() {
socketsStub.reset();
scheduler.clock = 0;
});

after(function() {
instance.io.sockets.emit.restore();
instance.cleanup();
clock.restore();
});

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

clock.tick();
scheduler.advanceTo(1000);

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

clock.tick();
scheduler.advanceTo(1000);

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

clock.tick();
scheduler.advanceTo(500);

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

clock.tick();
scheduler.advanceTo(1000);

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

clock.tick();
scheduler.advanceTo(1000);

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

Expand Down
36 changes: 0 additions & 36 deletions test/specs/files/files.watching.debounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,6 @@ var sinon = require("sinon");
var assert = require("chai").assert;

describe("File Watcher Module - reloadDebounce", function() {
it("Fires as fast as possible with no debounce", function(done) {
browserSync.reset();
var scheduler = require("../../utils").getScheduler();
var config = {
server: "test/fixtures",
open: false,
logLevel: "silent",
reloadDebounce: 0,
online: false,
files: "test/fixtures/*.html",
debug: {
scheduler: scheduler
}
};
browserSync(config, function(err, bs) {
var fn = bs.watchers.core.watchers[0]._events.all;

var stub = sinon.stub(bs.io.sockets, "emit");

fn("change", "index.html");
fn("change", "index.html");
fn("change", "index.html");
fn("change", "index.html");
fn("change", "index.html");
fn("change", "index.html");
fn("change", "index.html");

assert.isTrue(
stub.withArgs("browser:reload").getCalls().length === 7,
"Should emit 7 times"
);

bs.cleanup();
done();
});
});
it("only calls file:reload once within the time window", function(done) {
browserSync.reset();
var scheduler = require("../../utils").getScheduler();
Expand Down
6 changes: 3 additions & 3 deletions test/specs/files/files.watching.delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("File Watcher Module - reloadDelay", function() {
sinon.assert.notCalled(stub.withArgs("file:reload"));

// Advance virtual time beyond the delay
scheduler.advanceTo(1001);
scheduler.advanceTo(1501);

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

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

// before delay
scheduler.advanceTo(499);
scheduler.advanceTo(999);

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

// Advance virtual time beyond the delay
scheduler.advanceTo(501);
scheduler.advanceTo(1000);

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

Expand Down
13 changes: 10 additions & 3 deletions test/specs/http-protocol/http.reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ var sinon = require("sinon");
var proto = require("../../../dist/http-protocol");

describe("HTTP protocol", function() {
var bs, spy;
var bs, spy, scheduler;

before(function(done) {
browserSync.reset();

scheduler = require("../../utils").getScheduler();

var config = {
server: "test/fixtures",
logLevel: "info",
open: false,
online: false
online: false,
debug: { scheduler: scheduler }
};

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

afterEach(function() {
spy.reset();
scheduler.clock = 0;
});

after(function() {
Expand All @@ -37,6 +41,7 @@ describe("HTTP protocol", function() {
);

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

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

request(url, function(e, r, body) {
scheduler.advanceTo(500);
sinon.assert.calledWith(spy, "file:changed", {
path: "somefile.php",
log: true,
namespace: "core",
event: "change"
});
sinon.assert.calledWithExactly(spy, "browser:reload");
sinon.assert.calledWith(spy, "browser:reload");
assert.include(body, "Called public API method `.reload()`");
assert.include(body, 'With args: "somefile.php"');
done();
Expand Down

0 comments on commit 4c66bb8

Please sign in to comment.