Skip to content

Commit f1e02e9

Browse files
committed
Add more tests
1 parent ba98c3d commit f1e02e9

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

lib/GetFilenameFromUrl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getFilenameFromUrl(publicPath, outputPath, url) {
1010

1111
// publicPath has the hostname that is not the same as request url's, should fail
1212
if(localPrefix.hostname !== null && urlObject.hostname !== null &&
13-
localPrefix.hostname !== urlObject.hostname) {
13+
localPrefix.hostname !== urlObject.hostname) {
1414
return false;
1515
}
1616

@@ -25,7 +25,7 @@ function getFilenameFromUrl(publicPath, outputPath, url) {
2525
}
2626

2727
if(!urlObject.hostname && localPrefix.hostname &&
28-
url.indexOf(localPrefix.path) !== 0) {
28+
url.indexOf(localPrefix.path) !== 0) {
2929
return false;
3030
}
3131
// and if not match, use outputPath as filename

test/Lazy.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var should = require("should");
2+
var middleware = require("../middleware");
3+
4+
var doneStats = {
5+
hasErrors: function() {
6+
return false;
7+
},
8+
hasWarnings: function() {
9+
return false;
10+
}
11+
};
12+
13+
describe("Lazy mode", function() {
14+
var compiler = {
15+
plugin: function(name, callback) {
16+
plugins[name] = callback;
17+
}
18+
};
19+
var instance;
20+
var next;
21+
var res = {};
22+
23+
var plugins = [];
24+
beforeEach(function() {
25+
plugins = {};
26+
compiler.run = this.sinon.stub();
27+
next = this.sinon.stub();
28+
});
29+
30+
describe("builds", function() {
31+
var req = { method: "GET", url: "/bundle.js" };
32+
beforeEach(function() {
33+
instance = middleware(compiler, { lazy: true, quiet: true });
34+
});
35+
it("should trigger build", function(done) {
36+
instance(req, res, next);
37+
should.strictEqual(compiler.run.callCount, 1);
38+
plugins.done(doneStats);
39+
setTimeout(function() {
40+
should.strictEqual(next.callCount, 1);
41+
done();
42+
});
43+
});
44+
45+
it("should trigger rebuild when state is invalidated", function(done) {
46+
plugins.invalid();
47+
instance(req, res, next);
48+
plugins.done(doneStats);
49+
50+
should.strictEqual(compiler.run.callCount, 1);
51+
setTimeout(function() {
52+
should.strictEqual(next.callCount, 0);
53+
done();
54+
});
55+
});
56+
57+
it("should pass through compiler error", function() {
58+
compiler.run.callsArgWith(0, new Error("MyCompilerError"));
59+
should.throws(function() {
60+
instance(req, res, next);
61+
}, "MyCompilerError");
62+
});
63+
});
64+
65+
describe("custom filename", function() {
66+
it("should trigger build", function() {
67+
instance = middleware(compiler, { lazy: true, quiet: true, filename: "foo.js" });
68+
69+
var req = { method: "GET", url: "/bundle.js" };
70+
instance(req, res, next);
71+
should.strictEqual(compiler.run.callCount, 0);
72+
73+
req = { method: "GET", url: "/foo.js" };
74+
instance(req, res, next);
75+
should.strictEqual(compiler.run.callCount, 1);
76+
});
77+
78+
it("should allow prepended slash", function() {
79+
var options = { lazy: true, quiet: true, filename: "/foo.js" };
80+
instance = middleware(compiler, options);
81+
82+
var req = { method: "GET", url: "/foo.js" };
83+
instance(req, res, next);
84+
should.strictEqual(compiler.run.callCount, 1);
85+
});
86+
});
87+
});

test/Server.test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ describe("Server", function() {
2929
before(function(done) {
3030
app = express();
3131
var compiler = webpack(webpackConfig);
32-
app.use(middleware(compiler, {
32+
var instance = middleware(compiler, {
3333
stats: "errors-only",
3434
quiet: true,
3535
publicPath: "/",
36-
}));
36+
});
37+
app.use(instance);
3738
listen = listenShorthand(done);
39+
// Hack to add a mock HMR json file to the in-memory filesystem.
40+
instance.fileSystem.writeFileSync("/123a123412.hot-update.json", "[\"hi\"]");
3841
});
3942
after(close);
4043

@@ -65,6 +68,12 @@ describe("Server", function() {
6568
.expect(404, done);
6669
});
6770

71+
it("request to HMR json", function(done) {
72+
request(app).get("/123a123412.hot-update.json")
73+
.expect("Content-Type", "application/json")
74+
.expect(200, /\[\"hi\"\]/, done);
75+
});
76+
6877
it("request to directory", function(done) {
6978
request(app).get("/")
7079
.expect("Content-Type", "text/html")

0 commit comments

Comments
 (0)