Skip to content

Commit

Permalink
#8 - Initial test added for GetStatsService
Browse files Browse the repository at this point in the history
  • Loading branch information
jangerhard committed May 8, 2018
1 parent a3d4e52 commit ff60892
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 88 deletions.
117 changes: 29 additions & 88 deletions src/Services/__mocks__/GetStatsService.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,33 @@
import {query} from "../GetStatsService";
const API_KEY = 'validKey';
const variables = {
"username": "jangerhard",
"numRepositories": 1,
"numCommits": 1
};
const fakeData = {
"avatarUrl": "testUrl.com",
"name": "Test Testington",
"repositories": {}
};

const apiKeyError = 'Invalid API-Key';
const variablesError = 'Invalid variables';

const API = 'https://api.github.com/graphql';
export const getStatsFor = (apiKey, variables, callback) => {
return new Promise((resolve, reject) => {

const fakeData = [
{
"avatarUrl": "https://avatars2.githubusercontent.com/u/6482205?v=4",
"name": "Jan Gerhard Schoepp",
"repositories": {
"edges": [
{
"node": {
"name": "react-github-showcase",
"url": "https://github.com/jangerhard/react-github-showcase",
"ref": {
"target": {
"history": {
"nodes": [
{
"message": "Update README.md",
"abbreviatedOid": "a6e242e",
"committedDate": "2018-05-01T10:28:23Z"
},
{
"message": "Extract Github Service and use component shorthand and destructering (#1)\n\n* 0.0.11\r\n\r\n* Use component shorthand and destructering\r\n\r\n* Extract service\r\n\r\n* Finish up extracting service",
"abbreviatedOid": "c12cffd",
"committedDate": "2018-05-01T09:30:22Z"
}
]
}
}
}
}
},
{
"node": {
"name": "SchoeppBrewery",
"url": "https://github.com/jangerhard/SchoeppBrewery",
"ref": {
"target": {
"history": {
"nodes": [
{
"message": "Fix layouts",
"abbreviatedOid": "d5c27ea",
"committedDate": "2018-04-14T14:31:37Z"
},
{
"message": "Set middle row of grid to auto",
"abbreviatedOid": "075443f",
"committedDate": "2018-04-14T11:50:51Z"
}
]
}
}
}
}
},
{
"node": {
"name": "ReactPluralsight",
"url": "https://github.com/jangerhard/ReactPluralsight",
"ref": {
"target": {
"history": {
"nodes": [
{
"message": "Finish course",
"abbreviatedOid": "d96e05b",
"committedDate": "2018-04-11T19:03:53Z"
},
{
"message": "Add tests using Enzyme and Testing Utils",
"abbreviatedOid": "8feb574",
"committedDate": "2018-03-03T19:35:04Z"
}
]
}
}
}
}
}
]
}
}
];
// API_KEY === apiKey ? resolve(fakeData)
// : reject({
// error: 'Invalid API key'
// })

if (API_KEY !== apiKey)
reject(apiKeyError);
if (variables.name === username || variables.numRepositories === undefined || variables.numCommits === undefined)
reject(variablesError);

resolve(fakeData)
}).catch(() => {

export const getStatsFor = () => {
return new Promise(resolve => {
resolve(fakeData);
});
};
}
27 changes: 27 additions & 0 deletions src/Services/__tests__/GetStatsService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getStatsFor } from "../GetStatsService"

jest.mock('../GetStatsService');

const variables = {
"username": "jangerhard",
"numRepositories": 1,
"numCommits": 1
};

describe('#getStatsFor() using Promises', () => {
it('should load user data', () => {
return getStatsFor("invalidKey", variables)
.then(user => {
expect(user).toBeDefined()
expect(user.avatarUrl).toEqual('testUrl.com')
expect(user.name).toEqual('Sir Test Testington')
})
.catch(errors => {

})
})
it('should handle errors', () => {
})
it('should display a nicer error message', () => {
})
});

0 comments on commit ff60892

Please sign in to comment.