Skip to content

Commit e0ca98a

Browse files
Merge pull request #257 from FormidableLabs/chore/add-test
Add unit tests for dashboard, plugin, and utils.
2 parents 5b65faa + 379d0e9 commit e0ca98a

File tree

9 files changed

+261
-74
lines changed

9 files changed

+261
-74
lines changed

dashboard/index.js

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const DEFAULT_SCROLL_OPTIONS = {
2525
};
2626

2727
class Dashboard {
28-
constructor(options) { // eslint-disable-line max-statements
28+
// eslint-disable-next-line max-statements
29+
constructor(options) {
2930
// Options, params
3031
options = options || {};
3132
const title = options.title || "webpack-dashboard";
@@ -44,15 +45,19 @@ class Dashboard {
4445
log: this.setLog.bind(this),
4546
clear: this.clear.bind(this),
4647
sizes: _data => {
47-
if (this.minimal) { return; }
48+
if (this.minimal) {
49+
return;
50+
}
4851
if (_data.value instanceof Error) {
4952
this.setSizesError(_data.value);
5053
} else {
5154
this.setSizes(_data);
5255
}
5356
},
5457
problems: _data => {
55-
if (this.minimal) { return; }
58+
if (this.minimal) {
59+
return;
60+
}
5661
if (_data.value instanceof Error) {
5762
this.setProblemsError(_data.value);
5863
} else {
@@ -89,10 +94,13 @@ class Dashboard {
8994

9095
setData(dataArray) {
9196
dataArray
92-
.map(data => data.error
93-
? Object.assign({}, data, {
94-
value: deserializeError(data.value)
95-
}) : data
97+
.map(
98+
data =>
99+
data.error
100+
? Object.assign({}, data, {
101+
value: deserializeError(data.value)
102+
})
103+
: data
96104
)
97105
.forEach(data => {
98106
this.actionForMessageType[data.type](data);
@@ -169,12 +177,16 @@ class Dashboard {
169177

170178
// Then split modules across assets.
171179
const previousSelection = this.modulesMenu.selected;
172-
const modulesItems = Object.keys(assets).reduce((memo, name) => Object.assign({}, memo, {
173-
[name]: () => {
174-
this.moduleTable.setData(formatModules(assets[name].files));
175-
this.screen.render();
176-
}
177-
}), {});
180+
const modulesItems = Object.keys(assets).reduce(
181+
(memo, name) =>
182+
Object.assign({}, memo, {
183+
[name]: () => {
184+
this.moduleTable.setData(formatModules(assets[name].files));
185+
this.screen.render();
186+
}
187+
}),
188+
{}
189+
);
178190

179191
this.modulesMenu.setLabel("Modules");
180192
this.modulesMenu.setItems(modulesItems);
@@ -199,15 +211,21 @@ class Dashboard {
199211
const assetNames = Object.keys(duplicates.assets);
200212

201213
const previousSelection = this.problemsMenu.selected;
202-
const problemsItems = assetNames.reduce((memo, name) => Object.assign({}, memo, {
203-
[name]: () => {
204-
this.problems.setContent(formatProblems({
205-
duplicates: duplicates.assets[name],
206-
versions: versions.assets[name]
207-
}));
208-
this.screen.render();
209-
}
210-
}), {});
214+
const problemsItems = assetNames.reduce(
215+
(memo, name) =>
216+
Object.assign({}, memo, {
217+
[name]: () => {
218+
this.problems.setContent(
219+
formatProblems({
220+
duplicates: duplicates.assets[name],
221+
versions: versions.assets[name]
222+
})
223+
);
224+
this.screen.render();
225+
}
226+
}),
227+
{}
228+
);
211229

212230
this.problemsMenu.setLabel("Problems");
213231
this.problemsMenu.setItems(problemsItems);
@@ -477,9 +495,11 @@ class Dashboard {
477495
parent: this.wrapper,
478496
label: "Progress",
479497
tags: true,
480-
padding: this.minimal ? {
481-
left: 1
482-
} : 1,
498+
padding: this.minimal
499+
? {
500+
left: 1
501+
}
502+
: 1,
483503
width: this.minimal ? "33%" : "100%",
484504
height: this.minimal ? "100%" : "34%",
485505
valign: "middle",

test/dashboard/index.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,24 @@ require("../base.spec");
55
const Dashboard = require("../../dashboard");
66

77
describe("dashboard", () => {
8-
it("can create a new dashboard", () => {
8+
const options = {
9+
color: "red",
10+
minimal: true,
11+
title: "my-title"
12+
};
13+
14+
it("can create a new no option dashboard", () => {
915
const dashboard = new Dashboard();
1016
expect(dashboard).to.be.ok;
17+
expect(dashboard.color).to.equal("green");
18+
expect(dashboard.minimal).to.be.false;
19+
expect(dashboard.stats).to.be.null;
20+
});
21+
22+
it("can create a new with options dashboard", () => {
23+
const dashboardWithOptions = new Dashboard(options);
24+
expect(dashboardWithOptions).to.be.ok;
25+
expect(dashboardWithOptions.color).to.equal("red");
26+
expect(dashboardWithOptions.minimal).to.be.true;
1127
});
1228
});

test/plugin/index.spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,25 @@
33
const Plugin = require("../../plugin");
44

55
describe("plugin", () => {
6-
it("can create a new plugin", () => {
6+
const options = {
7+
port: 3000,
8+
host: "111.0.2.3"
9+
};
10+
11+
it("can create a new no option plugin", () => {
712
const plugin = new Plugin();
813
expect(plugin).to.be.ok;
14+
expect(plugin.host).to.equal("127.0.0.1");
15+
// eslint-disable-next-line no-magic-numbers
16+
expect(plugin.port).to.equal(9838);
17+
expect(plugin.handler).to.be.null;
18+
expect(plugin.watching).to.be.false;
19+
});
20+
21+
it("can create a new with options dashboard", () => {
22+
const pluginWithOptions = new Plugin(options);
23+
expect(pluginWithOptions.host).to.equal("111.0.2.3");
24+
// eslint-disable-next-line no-magic-numbers
25+
expect(pluginWithOptions.port).to.equal(3000);
926
});
1027
});

test/utils/format-assets.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"use strict";
2+
3+
const { _getAssetSize, _getTotalSize, _printAssets } = require("../../utils/format-assets");
4+
5+
describe("format-assets.js", () => {
6+
describe("#_getAssetSize", () => {
7+
context("when asset size is present", () => {
8+
it("returns a readable file size as string", () => {
9+
const asset = {
10+
size: 500
11+
};
12+
expect(_getAssetSize(asset)).to.equal("500 B");
13+
});
14+
});
15+
16+
context("when no asset size is present", () => {
17+
it("returns zero in a readable file size as string", () => {
18+
const asset = {
19+
size: undefined
20+
};
21+
expect(_getAssetSize(asset)).to.equal("0 B");
22+
});
23+
});
24+
});
25+
26+
describe("#_getTotalSize", () => {
27+
it("returns a readable file size of all assets as a string", () => {
28+
const assets = [{ size: 500 }, { size: undefined }, { size: 1000 }];
29+
expect(_getTotalSize(assets)).to.equal("1.46 KB");
30+
});
31+
});
32+
33+
describe("#_printAssets", () => {
34+
it("returns a nested array of assets information", () => {
35+
const assetList = [
36+
{
37+
name: "assets1",
38+
size: 500
39+
},
40+
{
41+
name: "assets2",
42+
size: 0
43+
},
44+
{
45+
name: "assets2",
46+
size: 500
47+
}
48+
];
49+
50+
const output = [
51+
["Name", "Size"],
52+
["assets1", "500 B"],
53+
["assets2", "0 B"],
54+
["assets2", "500 B"],
55+
["Total", "1000 B"]
56+
];
57+
expect(_printAssets(assetList)).eql(output);
58+
});
59+
});
60+
});

test/utils/format-modules.spec.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const { _formatFileName, _formatPercentage } = require("../../utils/format-modules");
4+
5+
describe("format-modules.js", () => {
6+
describe("#_formatFileName", () => {
7+
it("returns a blessed green colored file name", () => {
8+
const mod = {
9+
fileName: "foo/bar/test.js"
10+
};
11+
expect(_formatFileName(mod)).to.equal("{green-fg}./foo/bar/test.js{/}");
12+
});
13+
14+
context("when there is a baseName", () => {
15+
it("returns a blessed yellow colored file name", () => {
16+
const mod = {
17+
fileName: "test.js",
18+
baseName: "/home/bar/test.js"
19+
};
20+
expect(_formatFileName(mod)).to.equal("{yellow-fg}test.js{/}");
21+
});
22+
});
23+
24+
context("when node_modules is present in fileName", () => {
25+
it("returns a blessed yellow colored file name", () => {
26+
const mod = {
27+
fileName: "/node_modules/@foo/test.js",
28+
baseName: "/home/bar/node_modules/@foo/test.js"
29+
};
30+
expect(_formatFileName(mod)).to.equal("~/{yellow-fg}@foo{/}/{yellow-fg}test.js{/}");
31+
});
32+
});
33+
});
34+
35+
describe("#_formatPercentage", () => {
36+
it("returns a precentage as a string", () => {
37+
// eslint-disable-next-line no-magic-numbers
38+
expect(_formatPercentage(30, 15)).to.equal("200%");
39+
});
40+
});
41+
});

test/utils/format-output.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
3+
const { _isLikelyASyntaxError, _lineJoin, _formatMessage } = require("../../utils/format-output");
4+
5+
describe("format-output.js", () => {
6+
describe("#_isLikelyASyntaxError", () => {
7+
context("when message is a syntax error", () => {
8+
it("returns true", () => {
9+
const message = "Syntax error: missing ; before statement";
10+
expect(_isLikelyASyntaxError(message)).to.be.true;
11+
});
12+
});
13+
14+
context("when message is a type error", () => {
15+
it("returns false", () => {
16+
const message = "Type error: null has no properties";
17+
expect(_isLikelyASyntaxError(message)).to.be.false;
18+
});
19+
});
20+
});
21+
22+
describe("#_formatMessage", () => {
23+
it("returns a readable user friendly message", () => {
24+
const message1 = "Module build failed: SyntaxError: missing ; before statement";
25+
const message2 = "/Module not found: Error: Cannot resolve 'file' or 'directory'/";
26+
expect(_formatMessage(message1)).to.equal("Syntax error: missing ; before statement");
27+
expect(_formatMessage(message2)).to.equal("/Module not found:/");
28+
});
29+
});
30+
31+
describe("#_lineJoin", () => {
32+
it("returns the elements of an array on a newline as a string", () => {
33+
const array = ["word", "word2", "word3"];
34+
const output = "word\nword2\nword3";
35+
expect(_lineJoin(array)).to.equal(output);
36+
});
37+
});
38+
});

utils/format-assets.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@
55
*/
66
const filesize = require("filesize");
77

8-
function getAssetSize(asset) {
8+
function _getAssetSize(asset) {
99
return filesize(asset.size || 0);
1010
}
1111

12-
function getTotalSize(assetsList) {
13-
return filesize(assetsList.reduce(
14-
(total, asset) => total + (asset.size || 0),
15-
0
16-
));
12+
function _getTotalSize(assetsList) {
13+
return filesize(assetsList.reduce((total, asset) => total + (asset.size || 0), 0));
1714
}
1815

19-
function printAssets(assetsList) {
16+
function _printAssets(assetsList) {
2017
return [["Name", "Size"]]
21-
.concat(assetsList.map(asset =>
22-
[asset.name, getAssetSize(asset)]
23-
))
24-
.concat(
25-
[["Total", getTotalSize(assetsList)]]
26-
);
18+
.concat(assetsList.map(asset => [asset.name, _getAssetSize(asset)]))
19+
.concat([["Total", _getTotalSize(assetsList)]]);
2720
}
2821

2922
function formatAssets(assets) {
@@ -33,7 +26,10 @@ function formatAssets(assets) {
3326
size: assets[name].meta.full
3427
}));
3528

36-
return printAssets(assetsList);
29+
return _printAssets(assetsList);
3730
}
3831

39-
module.exports = formatAssets;
32+
module.exports = { formatAssets,
33+
_getAssetSize,
34+
_getTotalSize,
35+
_printAssets };

0 commit comments

Comments
 (0)