Skip to content

Commit 553095f

Browse files
thoovryanto
authored andcommitted
Refactor the code to allow importing (#183)
Move most of the logic to helper functions which allow the indivdual pieces imported and thus made more modular.
1 parent 0eed166 commit 553095f

3 files changed

Lines changed: 133 additions & 88 deletions

File tree

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = {
2929
'blueprints/*/index.js',
3030
'config/**/*.js',
3131
'tests/dummy/config/**/*.js',
32-
'node/**/*.js'
32+
'node/**/*.js',
33+
'lib/**/*.js'
3334
],
3435
excludedFiles: [
3536
'addon/**',

index.js

Lines changed: 24 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict';
22

3-
let FastBoot = require('fastboot');
4-
let url = require('url');
53
let resolve = require('resolve');
6-
let nock = require('nock');
7-
let bodyParser = require('body-parser');
8-
let path = require('path');
9-
let fs = require('fs');
104
let minimist = require('minimist');
11-
let JSONfn = require('json-fn');
5+
6+
let {
7+
createCleanUpMocks,
8+
createFastbootEcho,
9+
createFastbootTest,
10+
createMockRequest,
11+
reloadServer,
12+
createServer,
13+
makeFastbootTestingConfig
14+
} = require('./lib/helpers');
1215

1316
module.exports = {
1417
name: 'ember-cli-fastboot-testing',
@@ -46,95 +49,31 @@ module.exports = {
4649

4750
postBuild(result) {
4851
let distPath = result.directory;
49-
let options = this.makeFastbootTestingConfig({ distPath });
52+
let { pkg } = this.project;
5053

5154
if (this.fastboot) {
52-
this.fastboot.reload({ distPath });
53-
options.setupFastboot(this.fastboot);
55+
let options = makeFastbootTestingConfig({ distPath }, pkg);
56+
reloadServer(this.fastboot, distPath, options);
5457
} else {
55-
this._createServer(distPath)
58+
this.fastboot = createServer(distPath, pkg);
5659
}
5760

5861
return result;
5962
},
6063

61-
makeFastbootTestingConfig(config) {
62-
let defaults = {
63-
setupFastboot() {}
64-
};
65-
66-
let configPath = 'config';
67-
let pkg = this.project.pkg;
68-
69-
if (pkg['ember-addon'] && pkg['ember-addon']['configPath']) {
70-
configPath = pkg['ember-addon']['configPath'];
71-
}
72-
73-
let fastbootTestConfigPath = path.resolve(configPath, 'fastboot-testing.js');
74-
75-
let customized = fs.existsSync(fastbootTestConfigPath) ?
76-
require(fastbootTestConfigPath) :
77-
{};
78-
79-
return Object.assign({}, config, defaults, customized);
80-
},
81-
82-
_createServer(distPath) {
83-
let options = this.makeFastbootTestingConfig({ distPath });
84-
this.fastboot = new FastBoot(options);
85-
options.setupFastboot(this.fastboot);
86-
},
87-
8864
_fastbootRenderingMiddleware(app) {
89-
app.post('/__mock-request', bodyParser.json({ limit: '50mb' }), (req, res) => {
90-
let mock = nock(req.headers.origin)
91-
.persist()
92-
.intercept(req.body.path, req.body.method)
93-
.reply(req.body.statusCode, req.body.response);
94-
95-
res.json({ mocks: mock.pendingMocks() });
96-
});
97-
98-
app.use('/__cleanup-mocks', (req, res) => {
99-
nock.cleanAll()
100-
101-
res.json({ ok: true });
102-
});
103-
104-
app.post('/__fastboot-testing', bodyParser.json(), (req, res) => {
105-
let urlToVisit = decodeURIComponent(req.body.url);
106-
let parsed = url.parse(urlToVisit, true);
107-
108-
let headers = Object.assign(
109-
{},
110-
req.headers,
111-
JSONfn.parse(req.body.options).headers || {}
112-
);
113-
114-
let defaultOptions = {
115-
request: {
116-
method: 'GET',
117-
protocol: 'http',
118-
url: parsed.path,
119-
query: parsed.query,
120-
headers,
121-
},
122-
response: {},
123-
};
124-
125-
let options = Object.assign(defaultOptions, JSONfn.parse(req.body.options));
126-
127-
res.set('x-fastboot-testing', true);
128-
65+
createMockRequest(app);
66+
createCleanUpMocks(app);
67+
createFastbootTest(app, ({res, options, urlToVisit}) => {
12968
if (!this.fastboot) {
13069
const path = minimist(process.argv.slice(2)).path;
13170
if (path) {
132-
this._createServer(path);
71+
this.fastboot = createServer(path, this.project.pkg);
13372
} else {
13473
return res.json({ err: 'no path found' });
13574
}
13675
}
137-
76+
13877
this.fastboot
13978
.visit(urlToVisit, options)
14079
.then(page => {
@@ -151,11 +90,11 @@ module.exports = {
15190
.catch(err => {
15291
let errorObject;
15392
let jsonError = {};
154-
93+
15594
errorObject = (typeof err === 'string') ?
15695
new Error(err) :
15796
err;
158-
97+
15998
// we need to copy these properties off the error
16099
// object into a pojo that can be serialized and
161100
// sent over the wire to the test runner.
@@ -168,18 +107,16 @@ module.exports = {
168107
'number',
169108
'stack'
170109
];
171-
110+
172111
errorProps.forEach(key => jsonError[key] = errorObject[key]);
173-
112+
174113
res.json({ err: jsonError });
175114
});
176115
});
177116

178117
if (this.app && this.app.name === "dummy") {
179118
// our dummy app has an echo endpoint!
180-
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
181-
res.send(req.body);
182-
});
119+
createFastbootEcho(app);
183120
}
184121
},
185122
};

lib/helpers.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'use strict';
2+
3+
let fs = require('fs');
4+
let path = require('path');
5+
let nock = require('nock');
6+
let url = require('url');
7+
let JSONfn = require('json-fn');
8+
let FastBoot = require('fastboot');
9+
let bodyParser = require('body-parser');
10+
11+
function createMockRequest(app) {
12+
app.post('/__mock-request', bodyParser.json({ limit: '50mb' }), (req, res) => {
13+
let mock = nock(req.headers.origin)
14+
.persist()
15+
.intercept(req.body.path, req.body.method)
16+
.reply(req.body.statusCode, req.body.response);
17+
18+
res.json({ mocks: mock.pendingMocks() });
19+
});
20+
}
21+
22+
function createCleanUpMocks(app) {
23+
app.use('/__cleanup-mocks', (req, res) => {
24+
nock.cleanAll()
25+
26+
res.json({ ok: true });
27+
});
28+
}
29+
30+
function createFastbootTest(app, callback) {
31+
app.post('/__fastboot-testing', bodyParser.json(), (req, res) => {
32+
let urlToVisit = decodeURIComponent(req.body.url);
33+
let parsed = url.parse(urlToVisit, true);
34+
35+
let headers = Object.assign(
36+
{},
37+
req.headers,
38+
JSONfn.parse(req.body.options).headers || {}
39+
);
40+
41+
let defaultOptions = {
42+
request: {
43+
method: 'GET',
44+
protocol: 'http',
45+
url: parsed.path,
46+
query: parsed.query,
47+
headers,
48+
},
49+
response: {},
50+
};
51+
52+
let options = Object.assign(defaultOptions, JSONfn.parse(req.body.options));
53+
54+
res.set('x-fastboot-testing', true);
55+
56+
callback({ req, res, options, urlToVisit });
57+
});
58+
}
59+
60+
function createFastbootEcho(app) {
61+
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
62+
res.send(req.body);
63+
});
64+
}
65+
66+
function reloadServer(fastboot, distPath, options) {
67+
fastboot.reload({ distPath });
68+
options.setupFastboot(fastboot);
69+
}
70+
71+
function createServer(distPath, pkg) {
72+
let options = makeFastbootTestingConfig({ distPath }, pkg);
73+
let fastboot = new FastBoot(options);
74+
options.setupFastboot(fastboot);
75+
76+
return fastboot;
77+
}
78+
79+
function makeFastbootTestingConfig(config, pkg) {
80+
let defaults = {
81+
setupFastboot() {}
82+
};
83+
84+
let configPath = 'config';
85+
86+
if (pkg['ember-addon'] && pkg['ember-addon']['configPath']) {
87+
configPath = pkg['ember-addon']['configPath'];
88+
}
89+
90+
let fastbootTestConfigPath = path.resolve(configPath, 'fastboot-testing.js');
91+
92+
let customized = fs.existsSync(fastbootTestConfigPath) ?
93+
require(fastbootTestConfigPath) :
94+
{};
95+
96+
return Object.assign({}, config, defaults, customized);
97+
}
98+
99+
module.exports = {
100+
createMockRequest,
101+
createCleanUpMocks,
102+
createFastbootTest,
103+
createFastbootEcho,
104+
reloadServer,
105+
createServer,
106+
makeFastbootTestingConfig
107+
};

0 commit comments

Comments
 (0)