Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98bb3f5

Browse files
author
Sashko Stubailo
committed
Mock out GitHub API
1 parent 18e4b74 commit 98bb3f5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

__mocks__/request-promise.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ function noRequestsLeft() {
5353
rp.__pushMockRequest = pushMockRequest; // eslint-disable-line no-underscore-dangle
5454
rp.__flushRequestQueue = flushRequestQueue; // eslint-disable-line no-underscore-dangle
5555
rp.__noRequestsLeft = noRequestsLeft; // eslint-disable-line no-underscore-dangle
56+
57+
rp.actual = require.requireActual('request-promise');

__tests__/basic.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { run } from '../api/server';
21
import rp from 'request-promise';
2+
import casual from 'casual';
3+
4+
import { run } from '../api/server';
35

46
const testPort = 6789;
57
const endpointUrl = `http://localhost:${testPort}/graphql`;
@@ -10,6 +12,56 @@ beforeAll(() => {
1012
});
1113

1214
it('accepts a query', async () => {
15+
casual.seed(123);
16+
17+
[
18+
['apollographql/apollo-client', 'stubailo'],
19+
['apollographql/graphql-server', 'helfer'],
20+
['meteor/meteor', 'tmeasday'],
21+
['twbs/bootstrap', 'Slava'],
22+
['d3/d3', 'Slava'],
23+
].forEach(([full_name, postedBy]) => {
24+
// First, it will request the repository;
25+
rp.__pushMockRequest({
26+
options: {
27+
uri: `/repos/${full_name}`,
28+
},
29+
result: {
30+
headers: {
31+
etag: casual.string,
32+
},
33+
body: {
34+
name: full_name.split('/')[1],
35+
full_name,
36+
description: casual.sentence,
37+
html_url: casual.url,
38+
stargazers_count: casual.integer(0),
39+
open_issues_count: casual.integer(0),
40+
owner: {
41+
login: full_name.split('/')[0],
42+
avatar_url: casual.url,
43+
html_url: casual.url,
44+
},
45+
},
46+
},
47+
});
48+
49+
// Then the user who posted it
50+
rp.__pushMockRequest({
51+
options: {
52+
uri: `/users/${postedBy}`,
53+
},
54+
result: {
55+
headers: {
56+
etag: casual.string,
57+
},
58+
body: {
59+
login: postedBy,
60+
},
61+
},
62+
});
63+
});
64+
1365
const result = await fetchGraphQL(`
1466
{
1567
feed (type: NEW, limit: 5) {
@@ -32,7 +84,7 @@ afterAll(() => {
3284
});
3385

3486
function fetchGraphQL(query, variables) {
35-
return rp(endpointUrl, {
87+
return rp.actual(endpointUrl, {
3688
method: 'post',
3789
body: { query, variables },
3890
json: true,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"babel-eslint": "7.1.1",
2828
"babel-jest": "^19.0.0",
2929
"babel-register": "6.23.0",
30+
"casual": "^1.5.11",
3031
"eslint": "3.17.0",
3132
"eslint-config-airbnb": "14.1.0",
3233
"eslint-plugin-babel": "4.1.1",

0 commit comments

Comments
 (0)