Skip to content

Commit dab3a8b

Browse files
committed
nock server
1 parent 03311a1 commit dab3a8b

11 files changed

Lines changed: 692 additions & 598 deletions

File tree

addon-test-support/index.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,54 @@ import { fetch } from 'whatwg-fetch';
22
import { setupContext, teardownContext } from '@ember/test-helpers';
33
import param from 'jquery-param';
44

5+
let cleanupMocks = function() {
6+
return fetch('/__cleanup-mocks');
7+
};
8+
9+
let createMock = async function(path, method, statusCode, response) {
10+
return await fetch('/__mock-request', {
11+
method: 'post',
12+
headers: {
13+
"Content-Type": "application/json",
14+
},
15+
body: JSON.stringify({
16+
path,
17+
method,
18+
statusCode,
19+
response
20+
}),
21+
});
22+
}
23+
24+
export let mockServer = {
25+
async get(path, response, status = 200) {
26+
return createMock(path, "GET", status, response);
27+
},
28+
29+
async post(path, response, status = 200) {
30+
return createMock(path, "POST", status, response);
31+
},
32+
33+
async patch(path, response, status = 200) {
34+
return createMock(path, "PATCH", status, response);
35+
},
36+
37+
async put(path, response, status = 200) {
38+
return createMock(path, "PUT", status, response);
39+
},
40+
41+
async delete(path, response, status = 200) {
42+
return createMock(path, "DELETE", status, response);
43+
}
44+
};
45+
546
export function setup(hooks) {
647
hooks.beforeEach(function() {
748
return setupContext(this);
849
});
950

10-
hooks.afterEach(function() {
51+
hooks.afterEach(async function() {
52+
await cleanupMocks();
1153
return teardownContext(this);
1254
});
1355
}

blueprints/fastboot-test/files/tests/fastboot/__name__-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { module, test } from 'qunit';
2-
import { setup, visit } from 'ember-cli-fastboot-testing/test-support';
2+
import { setup, visit, /* mockServer */ } from 'ember-cli-fastboot-testing/test-support';
33

44
module('FastBoot | <%= dasherizedModuleName %> test', function(hooks) {
55
setup(hooks);

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
let FastBoot = require('fastboot');
44
let url = require('url');
55
let resolve = require('resolve');
6+
let nock = require('nock');
7+
let bodyParser = require('body-parser')
68

79
module.exports = {
810
name: 'ember-cli-fastboot-testing',
@@ -39,6 +41,23 @@ module.exports = {
3941
},
4042

4143
_fastbootRenderingMiddleware(app) {
44+
45+
app.use(bodyParser.json());
46+
app.post('/__mock-request', (req, res) => {
47+
let mock = nock(req.headers.origin)
48+
.persist()
49+
.intercept(req.body.path, req.body.method)
50+
.reply(req.body.statusCode, req.body.response);
51+
52+
res.json({ mocks: mock.pendingMocks() });
53+
});
54+
55+
app.use('/__cleanup-mocks', (req, res) => {
56+
nock.cleanAll()
57+
58+
res.json({ ok: true });
59+
});
60+
4261
app.use('/__fastboot-testing', (req, res) => {
4362
let urlToVisit = decodeURIComponent(req.query.url);
4463
let parsed = url.parse(urlToVisit, true);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
"test:all": "ember try:each"
2222
},
2323
"dependencies": {
24+
"body-parser": "^1.18.3",
2425
"ember-auto-import": "^1.2.15",
2526
"ember-cli-babel": "^6.6.0",
2627
"fastboot": "1.2.* || 2.*",
2728
"jquery-param": "^1.0.1",
2829
"resolve": "^1.10.0",
30+
"nock": "^10.0.6",
2931
"whatwg-fetch": "^3.0.0"
3032
},
3133
"devDependencies": {
@@ -53,7 +55,7 @@
5355
"ember-cli-uglify": "^2.0.0",
5456
"ember-disable-prototype-extensions": "^1.1.2",
5557
"ember-export-application-global": "^2.0.0",
56-
"ember-fetch": "^6.4.0",
58+
"ember-fetch": "^6.5.0",
5759
"ember-load-initializers": "^1.1.0",
5860
"ember-maybe-import-regenerator": "^0.1.6",
5961
"ember-resolver": "^4.0.0",

tests/dummy/app/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Router.map(function() {
2727

2828
this.route('request-object');
2929

30+
this.route('load-data');
31+
this.route('load-data-post');
32+
3033
this.route('errors', function() {
3134
this.route('throw-message');
3235
this.route('throw-error-object');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Route from '@ember/routing/route';
2+
import fetch from 'fetch';
3+
4+
export default Route.extend({
5+
6+
async model() {
7+
let response = await fetch('/api/posts', { method: 'post' });
8+
return await response.json();
9+
}
10+
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Route from '@ember/routing/route';
2+
import fetch from 'fetch';
3+
4+
export default Route.extend({
5+
6+
async model() {
7+
let response = await fetch('/api/posts');
8+
return await response.json();
9+
}
10+
11+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The data loaded from the server is:
2+
3+
{{#each model as |post|}}
4+
<div data-test-id="title-{{post.id}}">
5+
{{post.title}}
6+
</div>
7+
{{/each}}
8+
9+
10+
model: {{model}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The data loaded from the server is:
2+
3+
{{#each model as |post|}}
4+
<div data-test-id="title-{{post.id}}">
5+
{{post.title}}
6+
</div>
7+
{{/each}}
8+
9+
10+
model: {{model}}

tests/fastboot/mock-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { module, test } from 'qunit';
2+
import { setup, visit, mockServer } from 'ember-cli-fastboot-testing/test-support';
3+
4+
module('Fastboot | mock', function(hooks) {
5+
setup(hooks);
6+
7+
hooks.beforeEach(async function() {
8+
await mockServer.get('/api/')
9+
10+
});
11+
12+
test('it can mock a get request', async function(assert) {
13+
await mockServer.get('/api/posts', [
14+
{ id: 1, title: 'test post'},
15+
{ id: 2, title: 'test 2'},
16+
]);
17+
18+
await visit('/load-data');
19+
20+
await this.pauseTest();
21+
22+
assert.dom('[data-test-id="title-1"]').hasText("test post")
23+
});
24+
25+
test('it can mock a post request', async function(assert) {
26+
await mockServer.post('/api/posts', [
27+
{ id: 1, title: 'post post'},
28+
]);
29+
30+
await visit('/load-data-post');
31+
32+
assert.dom('[data-test-id="title-1"]').hasText("post post")
33+
});
34+
35+
test('')
36+
});

0 commit comments

Comments
 (0)