|
| 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 | +}); |
0 commit comments