Skip to content

Commit 30a67ca

Browse files
author
Ben Demboski
committed
Fix index require path :(
Also add unit test to make sure I don't screw up like this again.
1 parent 3bfa6e7 commit 30a67ca

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const createHandler = require('./lib/create-handler');
1+
const createHandler = require('./lib/handler');
22

33
function requiredParam(param, errorMessage) {
44
if (!param) {

lib/handler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function createHandler({
2222

2323
if (host !== endpoint) {
2424
callback({ error: `Unrecognized ${name}:// endpoint: '${host}'` });
25-
25+
2626
return;
2727
}
2828

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
"mocha": "^3.2.0",
1919
"sinon": "^1.17.7"
2020
},
21-
"dependencies": {}
21+
"dependencies": {
22+
"mock-require": "^2.0.1"
23+
}
2224
}

tests/unit/index-test.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const { assert } = require('chai');
2+
const mock = require('mock-require');
3+
const { join } = require('path');
4+
5+
describe('handler', () => {
6+
let app;
7+
let protocol;
8+
let onAppReady;
9+
let protocolName;
10+
let handlerOptions;
11+
12+
function register(options) {
13+
require('../..')(Object.assign({
14+
cwd: '.',
15+
app,
16+
protocol,
17+
}, options));
18+
}
19+
20+
before(() => {
21+
mock('../../lib/handler', options => handlerOptions = options);
22+
});
23+
24+
after(() => {
25+
mock.stop('../../lib/handler');
26+
});
27+
28+
beforeEach(() => {
29+
app = {
30+
on(evt, cb) {
31+
if (evt === 'ready') {
32+
onAppReady = cb;
33+
}
34+
},
35+
};
36+
37+
protocol = {
38+
registerFileProtocol(name/*, handler, errorHandler*/) {
39+
protocolName = name;
40+
},
41+
};
42+
});
43+
44+
it('works', () => {
45+
register();
46+
assert.ok(onAppReady);
47+
onAppReady();
48+
49+
assert.equal(protocolName, 'serve');
50+
assert.deepEqual(handlerOptions, {
51+
cwd: '.',
52+
name: 'serve',
53+
endpoint: 'dist',
54+
directoryIndexFile: 'index.html',
55+
indexPath: undefined,
56+
});
57+
});
58+
59+
it('works with non-default arguments', () => {
60+
register({
61+
cwd: join('my', 'old'),
62+
name: 'friend',
63+
endpoint: 'so',
64+
directoryIndexFile: 'we.html',
65+
indexPath: join('meet', 'again'),
66+
});
67+
assert.ok(onAppReady);
68+
onAppReady();
69+
70+
assert.equal(protocolName, 'friend');
71+
assert.deepEqual(handlerOptions, {
72+
cwd: join('my', 'old'),
73+
name: 'friend',
74+
endpoint: 'so',
75+
directoryIndexFile: 'we.html',
76+
indexPath: join('meet', 'again'),
77+
});
78+
});
79+
80+
it('required a cwd', () => {
81+
assert.throws(() => register({ cwd: undefined }));
82+
});
83+
84+
it('required an app', () => {
85+
assert.throws(() => register({ app: undefined }));
86+
});
87+
88+
it('required a protocol', () => {
89+
assert.throws(() => register({ protocol: undefined }));
90+
});
91+
});

yarn.lock

+16
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ buffer-shims@^1.0.0:
9494
version "1.0.0"
9595
resolved "https://registry.yarnpkg.com/buffer-shims/-/buffer-shims-1.0.0.tgz#9978ce317388c649ad8793028c3477ef044a8b51"
9696

97+
caller-id@^0.1.0:
98+
version "0.1.0"
99+
resolved "https://registry.yarnpkg.com/caller-id/-/caller-id-0.1.0.tgz#59bdac0893d12c3871408279231f97458364f07b"
100+
dependencies:
101+
stack-trace "~0.0.7"
102+
97103
caller-path@^0.1.0:
98104
version "0.1.0"
99105
resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f"
@@ -685,6 +691,12 @@ mocha@^3.2.0:
685691
mkdirp "0.5.1"
686692
supports-color "3.1.2"
687693

694+
mock-require@^2.0.1:
695+
version "2.0.1"
696+
resolved "https://registry.yarnpkg.com/mock-require/-/mock-require-2.0.1.tgz#7b0ebc79b8af5dfb1e9caefeb977804e71d2e004"
697+
dependencies:
698+
caller-id "^0.1.0"
699+
688700
ms@0.7.1:
689701
version "0.7.1"
690702
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"
@@ -871,6 +883,10 @@ sprintf-js@~1.0.2:
871883
version "1.0.3"
872884
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
873885

886+
stack-trace@~0.0.7:
887+
version "0.0.9"
888+
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.9.tgz#a8f6eaeca90674c333e7c43953f275b451510695"
889+
874890
string-width@^1.0.1:
875891
version "1.0.2"
876892
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"

0 commit comments

Comments
 (0)