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

Skip to content

Commit af2a138

Browse files
committed
Merge branch 'master' into feature/subscriptions-graphiql
# Conflicts: # api/index.js # package.json
2 parents 591cbb8 + b4fd0c4 commit af2a138

File tree

13 files changed

+470
-274
lines changed

13 files changed

+470
-274
lines changed

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"env": {
33
"node": true,
4-
"mocha": true,
4+
"jest": true
55
},
66
"extends": "airbnb",
77
"parser": "babel-eslint",
@@ -11,6 +11,8 @@
1111
"class-methods-use-this": 0,
1212
"import/prefer-default-export": 0,
1313
"import/no-extraneous-dependencies": 0,
14-
"import/imports-first": 0
14+
"import/imports-first": 0,
15+
"no-use-before-define": 0,
16+
"no-underscore-dangle": 0
1517
}
1618
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
.DS_Store
33
dev.sqlite3
4+
test.sqlite3
45
.env
56
.idea/

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
language: node_js
22
node_js:
33
- "6"
4-
- "4"
54
notifications:
6-
email: false
5+
email: false
6+
script:
7+
- npm run test:setup
8+
- npm test

__mocks__/request-promise.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let requestQueue = [];
2+
3+
export default function rp(requestOptions) {
4+
// Ensure we expected to get more requests
5+
expect(requestQueue.length).not.toBe(0);
6+
7+
const nextRequest = requestQueue.shift();
8+
// Ensure this is the request we expected
9+
expect(requestOptions).toEqual(nextRequest.options);
10+
11+
return new Promise((resolve, reject) => {
12+
if (nextRequest.result) {
13+
resolve(nextRequest.result);
14+
} else if (nextRequest.error) {
15+
reject(nextRequest.error);
16+
} else {
17+
throw new Error('Mocked request must have result or error.');
18+
}
19+
});
20+
}
21+
22+
function pushMockRequest({ options, result, error }) {
23+
const defaultOptions = {
24+
json: true,
25+
headers: {
26+
'user-agent': 'GitHunt',
27+
},
28+
resolveWithFullResponse: true,
29+
};
30+
const { uri, ...rest } = options;
31+
32+
const url = `https://api.github.com${uri}`;
33+
34+
requestQueue.push({
35+
options: {
36+
...defaultOptions,
37+
...rest,
38+
uri: url,
39+
},
40+
result,
41+
error,
42+
});
43+
}
44+
45+
function flushRequestQueue() {
46+
requestQueue = [];
47+
}
48+
49+
function noRequestsLeft() {
50+
expect(requestQueue.length).toBe(0);
51+
}
52+
53+
rp.__pushMockRequest = pushMockRequest; // eslint-disable-line no-underscore-dangle
54+
rp.__flushRequestQueue = flushRequestQueue; // eslint-disable-line no-underscore-dangle
55+
rp.__noRequestsLeft = noRequestsLeft; // eslint-disable-line no-underscore-dangle
56+
57+
rp.actual = require.requireActual('request-promise');

__tests__/__snapshots__/basic.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`accepts a query 1`] = `
4+
Object {
5+
"data": Object {
6+
"feed": Array [
7+
Object {
8+
"postedBy": Object {
9+
"login": "stubailo",
10+
},
11+
"repository": Object {
12+
"name": "apollo-client",
13+
"owner": Object {
14+
"login": "apollographql",
15+
},
16+
},
17+
},
18+
Object {
19+
"postedBy": Object {
20+
"login": "helfer",
21+
},
22+
"repository": Object {
23+
"name": "graphql-server",
24+
"owner": Object {
25+
"login": "apollographql",
26+
},
27+
},
28+
},
29+
Object {
30+
"postedBy": Object {
31+
"login": "tmeasday",
32+
},
33+
"repository": Object {
34+
"name": "meteor",
35+
"owner": Object {
36+
"login": "meteor",
37+
},
38+
},
39+
},
40+
Object {
41+
"postedBy": Object {
42+
"login": "Slava",
43+
},
44+
"repository": Object {
45+
"name": "bootstrap",
46+
"owner": Object {
47+
"login": "twbs",
48+
},
49+
},
50+
},
51+
Object {
52+
"postedBy": Object {
53+
"login": "Slava",
54+
},
55+
"repository": Object {
56+
"name": "d3",
57+
"owner": Object {
58+
"login": "d3",
59+
},
60+
},
61+
},
62+
],
63+
},
64+
}
65+
`;

__tests__/basic.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import rp from 'request-promise';
2+
import casual from 'casual';
3+
4+
import { run } from '../api/server';
5+
6+
const testPort = 6789;
7+
const endpointUrl = `http://localhost:${testPort}/graphql`;
8+
9+
let server;
10+
beforeAll(() => {
11+
server = run({ PORT: testPort });
12+
});
13+
14+
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+
65+
const result = await fetchGraphQL(`
66+
{
67+
feed (type: NEW, limit: 5) {
68+
repository {
69+
owner { login }
70+
name
71+
}
72+
73+
postedBy { login }
74+
}
75+
}
76+
`);
77+
78+
expect(result).toMatchSnapshot();
79+
});
80+
81+
afterAll(() => {
82+
server.close();
83+
server = null;
84+
});
85+
86+
function fetchGraphQL(query, variables) {
87+
return rp.actual(endpointUrl, {
88+
method: 'post',
89+
body: { query, variables },
90+
json: true,
91+
});
92+
}

0 commit comments

Comments
 (0)