Skip to content

Commit 09cc396

Browse files
author
parkerziegler
committed
Upgrade to inspectpack 4; add regression tests.
1 parent e0ca98a commit 09cc396

15 files changed

+1364
-20
lines changed

bin/webpack-dashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const commander = require("commander");
66
const spawn = require("cross-spawn");
7-
const Dashboard = require("../dashboard/index.js");
7+
const Dashboard = require("../dashboard/index");
88
const SocketIO = require("socket.io");
99

1010
const DEFAULT_PORT = 9838;

dashboard/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
const chalk = require("chalk");
44
const blessed = require("blessed");
55

6-
const formatOutput = require("../utils/format-output.js");
7-
const formatModules = require("../utils/format-modules.js");
8-
const formatAssets = require("../utils/format-assets.js");
9-
const formatProblems = require("../utils/format-problems.js");
6+
const { formatOutput } = require("../utils/format-output");
7+
const { formatModules } = require("../utils/format-modules");
8+
const { formatAssets } = require("../utils/format-assets");
9+
const { formatProblems } = require("../utils/format-problems");
1010
const { deserializeError } = require("../utils/error-serialization");
1111

1212
const PERCENT_MULTIPLIER = 100;

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
22

3-
const dashboard = require("./dashboard/index.js");
3+
const dashboard = require("./dashboard/index");
44

55
module.exports = dashboard;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"cross-spawn": "^6.0.5",
4040
"filesize": "^3.6.1",
4141
"handlebars": "^4.0.11",
42-
"inspectpack": "^3.0.1",
42+
"inspectpack": "^4.0.0",
4343
"most": "^1.7.3",
4444
"socket.io": "^2.1.1",
4545
"socket.io-client": "^2.1.1"

plugin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class DashboardPlugin {
171171
},
172172
{
173173
type: "progress",
174-
value: 0
174+
value: 1
175175
},
176176
{
177177
type: "operations",

test/base.spec.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,35 @@ beforeEach(() => {
3131

3232
// Some manual hacking.
3333
blessed.screen.returns({
34-
append: () => {},
35-
key: () => {},
36-
render: () => {}
34+
append: sinon.spy(),
35+
key: sinon.spy(),
36+
render: sinon.spy()
37+
});
38+
39+
blessed.listbar.returns({
40+
selected: "selected",
41+
setLabel: sinon.spy(),
42+
setProblems: sinon.spy(),
43+
selectTab: sinon.spy(),
44+
setItems: sinon.spy()
45+
});
46+
47+
blessed.box.returns({
48+
setContent: sinon.spy(),
49+
setLabel: sinon.spy()
50+
});
51+
52+
blessed.log.returns({
53+
log: sinon.spy()
54+
});
55+
56+
blessed.table.returns({
57+
setData: sinon.spy()
58+
});
59+
60+
blessed.ProgressBar.returns({
61+
setContent: sinon.spy(),
62+
setProgress: sinon.spy()
3763
});
3864
});
3965

test/dashboard/index.spec.js

Lines changed: 286 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
"use strict";
22

3-
require("../base.spec");
3+
const chalk = require("chalk");
4+
const blessed = require("blessed");
45

6+
const base = require("../base.spec");
57
const Dashboard = require("../../dashboard");
68

9+
const mockSetItems = () => {
10+
// Override ListBar fakes from what we do in `base.spec.js`.
11+
// Note that these are **already** stubbed. We're not monkey-patching blessed.
12+
blessed.listbar.returns({
13+
selected: "selected",
14+
setLabel: base.sandbox.spy(),
15+
selectTab: base.sandbox.spy(),
16+
setItems: base.sandbox.stub().callsFake(obj => {
17+
// Naively simulate what setItems would do calling each object key.
18+
Object.keys(obj).forEach(key => obj[key]());
19+
})
20+
});
21+
};
22+
723
describe("dashboard", () => {
824
const options = {
925
color: "red",
@@ -25,4 +41,273 @@ describe("dashboard", () => {
2541
expect(dashboardWithOptions.color).to.equal("red");
2642
expect(dashboardWithOptions.minimal).to.be.true;
2743
});
44+
45+
describe("set* methods", () => {
46+
let dashboard;
47+
48+
beforeEach(() => {
49+
dashboard = new Dashboard();
50+
});
51+
52+
describe("setData", () => {
53+
const dataArray = [{
54+
type: "progress",
55+
value: 0.57
56+
}, {
57+
type: "operations",
58+
value: "IDLE"
59+
}];
60+
61+
it("can setData", () => {
62+
expect(() => dashboard.setData(dataArray)).to.not.throw;
63+
});
64+
});
65+
66+
describe("setOperations", () => {
67+
const data = {
68+
value: "IDLE"
69+
};
70+
71+
it("can setOperations", () => {
72+
expect(() => dashboard.setOperations(data)).to.not.throw;
73+
74+
dashboard.setOperations(data);
75+
expect(dashboard.operations.setContent).to.have.been.calledWith(data.value);
76+
});
77+
});
78+
79+
describe("setStatus", () => {
80+
const data = {
81+
value: "Success"
82+
};
83+
84+
it("can setStatus", () => {
85+
expect(() => dashboard.setStatus(data)).to.not.throw;
86+
87+
dashboard.setStatus(data);
88+
expect(dashboard.status.setContent)
89+
.to.have.been.calledWith(`{green-fg}{bold}${data.value}{/}`);
90+
});
91+
92+
it("should display a failed status on build failure", () => {
93+
data.value = "Failed";
94+
expect(() => dashboard.setStatus(data)).to.not.throw;
95+
96+
dashboard.setStatus(data);
97+
expect(dashboard.status.setContent)
98+
.to.have.been.calledWith(`{red-fg}{bold}${data.value}{/}`);
99+
});
100+
101+
it("should display any other status string without coloring", () => {
102+
data.value = "Unknown";
103+
expect(() => dashboard.setStatus(data)).to.not.throw;
104+
105+
dashboard.setStatus(data);
106+
expect(dashboard.status.setContent)
107+
.to.have.been.calledWith(`{bold}${data.value}{/}`);
108+
});
109+
});
110+
111+
describe("setProgress", () => {
112+
const data = {
113+
value: 0.57
114+
};
115+
116+
it("can setProgress", () => {
117+
expect(() => dashboard.setProgress(data)).to.not.throw;
118+
119+
dashboard.setProgress(data);
120+
expect(dashboard.progressbar.setProgress).to.have.been.calledOnce;
121+
expect(dashboard.progressbar.setContent).to.have.been.called;
122+
});
123+
124+
it(`should call progressbar.setProgress twice if not in minimal mode
125+
and percent is falsy`, () => {
126+
data.value = null;
127+
expect(() => dashboard.setProgress(data)).to.not.throw;
128+
129+
dashboard.setProgress(data);
130+
expect(dashboard.progressbar.setProgress).to.have.been.calledTwice;
131+
});
132+
});
133+
134+
describe("setStats", () => {
135+
const data = {
136+
value: {
137+
errors: null,
138+
data: {
139+
errors: [],
140+
warnings: []
141+
}
142+
}
143+
};
144+
145+
it("can setStats", () => {
146+
expect(() => dashboard.setStats(data)).not.to.throw;
147+
148+
dashboard.setStats(data);
149+
expect(dashboard.logText.log).to.have.been.called;
150+
expect(dashboard.modulesMenu.setLabel)
151+
.to.have.been.calledWith(chalk.yellow("Modules (loading...)"));
152+
expect(dashboard.assets.setLabel)
153+
.to.have.been.calledWith(chalk.yellow("Assets (loading...)"));
154+
expect(dashboard.problemsMenu.setLabel)
155+
.to.have.been.calledWith(chalk.yellow("Problems (loading...)"));
156+
});
157+
158+
it("should display stats errors if present", () => {
159+
data.value.errors = ["error"];
160+
expect(() => dashboard.setStats(data)).not.to.throw;
161+
162+
dashboard.setStats(data);
163+
expect(dashboard.status.setContent)
164+
.to.have.been.calledWith("{red-fg}{bold}Failed{/}");
165+
});
166+
});
167+
168+
describe("setSizes", () => {
169+
const data = {
170+
value: {
171+
assets: {
172+
foo: {
173+
meta: {
174+
full: 456
175+
},
176+
files: [{
177+
size: {
178+
full: 123
179+
},
180+
fileName: "test.js",
181+
baseName: "/home/bar/test.js"
182+
}]
183+
},
184+
bar: {
185+
meta: {
186+
full: 123
187+
},
188+
files: []
189+
}
190+
}
191+
}
192+
};
193+
194+
const formattedData = [
195+
["Name", "Size"],
196+
["foo", "456 B"],
197+
["bar", "123 B"],
198+
["Total", "579 B"]
199+
];
200+
201+
it("can setSizes", () => {
202+
expect(() => dashboard.setSizes(data)).to.not.throw;
203+
204+
dashboard.setSizes(data);
205+
expect(dashboard.assets.setLabel).to.have.been.calledWith("Assets");
206+
expect(dashboard.assetTable.setData).to.have.been.calledWith(formattedData);
207+
expect(dashboard.modulesMenu.setLabel).to.have.been.calledWith("Modules");
208+
expect(dashboard.modulesMenu.setItems).to.have.been.called;
209+
expect(dashboard.modulesMenu.selectTab)
210+
.to.have.been.calledWith(dashboard.modulesMenu.selected);
211+
expect(dashboard.screen.render).to.have.been.called;
212+
});
213+
214+
it("should call formatModules", () => {
215+
// Mock out the call to setItems to force call of formatModules.
216+
mockSetItems();
217+
// Discard generic dashboard, create a new one with adjusted mocks.
218+
dashboard = new Dashboard();
219+
expect(() => dashboard.setSizes(data)).to.not.throw;
220+
});
221+
});
222+
223+
describe("setSizesError", () => {
224+
const err = "error";
225+
226+
it("can setSizesError", () => {
227+
expect(() => dashboard.setSizesError(err)).to.not.throw;
228+
229+
dashboard.setSizesError(err);
230+
expect(dashboard.modulesMenu.setLabel)
231+
.to.have.been.calledWith(chalk.red("Modules (error)"));
232+
expect(dashboard.assets.setLabel).to.have.been.calledWith(chalk.red("Assets (error)"));
233+
expect(dashboard.logText.log)
234+
.to.have.been.calledWith(chalk.red("Could not load module/asset sizes."));
235+
expect(dashboard.logText.log).to.have.been.calledWith(chalk.red(err));
236+
});
237+
});
238+
239+
describe("setProblems", () => {
240+
const data = {
241+
value: {
242+
duplicates: {
243+
assets: {
244+
foo: "foo",
245+
bar: "bar"
246+
}
247+
},
248+
versions: {
249+
assets: {
250+
foo: "1.2.3",
251+
bar: "3.2.1"
252+
}
253+
}
254+
}
255+
};
256+
257+
it("can setProblems", () => {
258+
expect(() => dashboard.setProblems(data)).to.not.throw;
259+
260+
dashboard.setProblems(data);
261+
expect(dashboard.problemsMenu.setLabel).to.have.been.calledWith("Problems");
262+
expect(dashboard.problemsMenu.setItems).to.have.been.called;
263+
expect(dashboard.problemsMenu.selectTab)
264+
.to.have.been.calledWith(dashboard.problemsMenu.selected);
265+
expect(dashboard.screen.render).to.have.been.called;
266+
});
267+
268+
it("should call formatProblems", () => {
269+
// Mock out the call to setItems to force call of formatProblems.
270+
mockSetItems();
271+
// Discard generic dashboard, create a new one with adjusted mocks.
272+
273+
dashboard = new Dashboard();
274+
expect(() => dashboard.setProblems(data)).to.not.throw;
275+
});
276+
});
277+
278+
describe("setProblemsError", () => {
279+
const err = { stack: "stack" };
280+
281+
it("can setProblemsError", () => {
282+
expect(() => dashboard.setProblemsError(err)).to.not.throw;
283+
284+
dashboard.setProblemsError(err);
285+
expect(dashboard.problemsMenu.setLabel)
286+
.to.have.been.calledWith(chalk.red("Problems (error)"));
287+
expect(dashboard.logText.log)
288+
.to.have.been.calledWith(chalk.red("Could not analyze bundle problems."));
289+
expect(dashboard.logText.log).to.have.been.calledWith(chalk.red(err.stack));
290+
});
291+
});
292+
293+
describe("setLog", () => {
294+
const data = { value: "[{ log: 'log' }]" };
295+
296+
it("can setLog", () => {
297+
expect(() => dashboard.setLog(data)).not.to.throw;
298+
299+
dashboard.setLog(data);
300+
expect(dashboard.logText.log).to.have.been.calledWith("[ log: 'log' ]");
301+
});
302+
303+
it("should return early if the stats object has errors", () => {
304+
dashboard.stats = {};
305+
dashboard.stats.hasErrors = () => true;
306+
expect(dashboard.setLog(data)).to.be.undefined;
307+
308+
dashboard.setLog(data);
309+
expect(dashboard.logText.log).to.not.have.been.called;
310+
});
311+
});
312+
});
28313
});

0 commit comments

Comments
 (0)