Skip to content

Commit 6d8f5a2

Browse files
committed
test(urlMiddleware): Add tests according to #35
1 parent 6387d32 commit 6d8f5a2

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"build-es": "rimraf es && BABEL_ENV=es babel src -d es",
7777
"lint": "eslint src test *.js",
7878
"coverage": "nyc npm run test:all",
79-
"test": "BABEL_ENV=lib mocha --require test/mocha-bootload --compilers js:babel-register test/*.test.js",
79+
"test": "BABEL_ENV=lib mocha --require test/mocha-bootload --compilers js:babel-register test/*.test.js test/**/*.test.js",
8080
"test:all": "npm run lint && npm run test",
8181
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
8282
}

test/middleware/url.test.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { assert } from 'chai';
2+
import fetchMock from 'fetch-mock';
3+
import { RelayNetworkLayer } from '../../src';
4+
import { mockReq } from '../testutils';
5+
import urlMiddleware from '../../src/middleware/url';
6+
7+
describe('Middleware / url', () => {
8+
describe('url option as string', () => {
9+
const rnl = new RelayNetworkLayer([
10+
urlMiddleware({
11+
url: '/other/url',
12+
}),
13+
]);
14+
15+
beforeEach(() => {
16+
fetchMock.restore();
17+
18+
fetchMock.mock({
19+
matcher: '/other/url',
20+
response: {
21+
status: 200,
22+
body: { data: 'PAYLOAD' },
23+
sendAsJson: true,
24+
},
25+
method: 'POST',
26+
});
27+
});
28+
29+
it('should work with query', () => {
30+
const req1 = mockReq();
31+
rnl.sendQueries([req1]).then(() => {
32+
assert.equal(req1.payload.response, 'PAYLOAD');
33+
});
34+
});
35+
36+
it('should work with mutation', () => {
37+
const req1 = mockReq();
38+
return assert.isFulfilled(
39+
rnl.sendMutation(req1).then(() => {
40+
assert.equal(req1.payload.response, 'PAYLOAD');
41+
})
42+
);
43+
});
44+
});
45+
46+
describe('url option as thunk', () => {
47+
const rnl = new RelayNetworkLayer([
48+
urlMiddleware({
49+
url: (_) => '/thunk_url', // eslint-disable-line
50+
}),
51+
]);
52+
53+
beforeEach(() => {
54+
fetchMock.restore();
55+
56+
fetchMock.mock({
57+
matcher: '/thunk_url',
58+
response: {
59+
status: 200,
60+
body: { data: 'PAYLOAD' },
61+
sendAsJson: true,
62+
},
63+
method: 'POST',
64+
});
65+
});
66+
67+
it('should work with query', () => {
68+
const req1 = mockReq();
69+
rnl.sendQueries([req1]).then(() => {
70+
assert.equal(req1.payload.response, 'PAYLOAD');
71+
});
72+
});
73+
74+
it('should work with mutation', () => {
75+
const req1 = mockReq();
76+
return assert.isFulfilled(
77+
rnl.sendMutation(req1).then(() => {
78+
assert.equal(req1.payload.response, 'PAYLOAD');
79+
})
80+
);
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)