|
| 1 | +var middleware = require("../middleware"); |
| 2 | +var should = require("should"); |
| 3 | + |
| 4 | +var options = { |
| 5 | + quiet: true, |
| 6 | + publicPath: "/public/" |
| 7 | +}; |
| 8 | + |
| 9 | +describe.only("Advanced API", function() { |
| 10 | + |
| 11 | + var plugins = {}; |
| 12 | + var invalidationCount = 0; |
| 13 | + var closeCount = 0; |
| 14 | + // TODO: Should use sinon or something for this... |
| 15 | + var compiler = { |
| 16 | + outputPath: "/output", |
| 17 | + watch: function() { |
| 18 | + return { |
| 19 | + invalidate: function() { |
| 20 | + invalidationCount += 1; |
| 21 | + }, |
| 22 | + close: function(callback) { |
| 23 | + closeCount += 1; |
| 24 | + callback(); |
| 25 | + } |
| 26 | + }; |
| 27 | + }, |
| 28 | + plugin: function(name, callback) { |
| 29 | + plugins[name] = callback; |
| 30 | + } |
| 31 | + }; |
| 32 | + beforeEach(function() { |
| 33 | + plugins = {}; |
| 34 | + invalidationCount = 0; |
| 35 | + closeCount = 0; |
| 36 | + }); |
| 37 | + var doneStats = { |
| 38 | + hasErrors: function() { |
| 39 | + return false; |
| 40 | + }, |
| 41 | + hasWarnings: function() { |
| 42 | + return false; |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + describe("waitUntilValid", function() { |
| 47 | + it("should wait for bundle done", function(done) { |
| 48 | + var doneCalled = false; |
| 49 | + var instance = middleware(compiler, options); |
| 50 | + instance.waitUntilValid(function() { |
| 51 | + if(doneCalled) { |
| 52 | + done(); |
| 53 | + } else { |
| 54 | + done(new Error('`waitUntilValid` called before bundle was done')); |
| 55 | + } |
| 56 | + }); |
| 57 | + setTimeout(function() { |
| 58 | + plugins.done(doneStats); |
| 59 | + doneCalled = true; |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("callback should be called when bundle is already done", function(done) { |
| 64 | + var instance = middleware(compiler, options); |
| 65 | + plugins.done(doneStats); |
| 66 | + setTimeout(function() { |
| 67 | + instance.waitUntilValid(done); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("invalidate", function() { |
| 73 | + it("should use callback immediately when in lazy mode", function(done) { |
| 74 | + var instance = middleware(compiler, { lazy: true, quiet: true }); |
| 75 | + instance.invalidate(done); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should wait for bundle done", function(done) { |
| 79 | + var instance = middleware(compiler, options); |
| 80 | + var doneCalled = false; |
| 81 | + instance.invalidate(function() { |
| 82 | + if(doneCalled) { |
| 83 | + should.strictEqual(invalidationCount, 1); |
| 84 | + done(); |
| 85 | + } else { |
| 86 | + done(new Error('`invalid` called before bundle was done')); |
| 87 | + } |
| 88 | + }); |
| 89 | + setTimeout(function() { |
| 90 | + plugins.done(doneStats); |
| 91 | + doneCalled = true; |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("close", function() { |
| 97 | + it("should use callback immediately when in lazy mode", function(done) { |
| 98 | + var instance = middleware(compiler, { lazy: true, quiet: true }); |
| 99 | + instance.close(done); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should call close on watcher", function(done) { |
| 103 | + var instance = middleware(compiler, options); |
| 104 | + instance.close(function() { |
| 105 | + should.strictEqual(closeCount, 1); |
| 106 | + done(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("getFilenameFromUrl", function() { |
| 112 | + it("use publicPath and compiler.outputPath to parse the filename", function(done) { |
| 113 | + var instance = middleware(compiler, options); |
| 114 | + var filename = instance.getFilenameFromUrl("/public/index.html"); |
| 115 | + should.strictEqual(filename, "/output/index.html"); |
| 116 | + done(); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments