From ff608929228edabfc2f892420d733ba780761fec Mon Sep 17 00:00:00 2001 From: jangerhard Date: Tue, 8 May 2018 15:10:18 +0200 Subject: [PATCH] #8 - Initial test added for GetStatsService --- src/Services/__mocks__/GetStatsService.js | 117 +++++------------- .../__tests__/GetStatsService.test.js | 27 ++++ 2 files changed, 56 insertions(+), 88 deletions(-) create mode 100644 src/Services/__tests__/GetStatsService.test.js diff --git a/src/Services/__mocks__/GetStatsService.js b/src/Services/__mocks__/GetStatsService.js index 4783d95..e37886e 100644 --- a/src/Services/__mocks__/GetStatsService.js +++ b/src/Services/__mocks__/GetStatsService.js @@ -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); }); -}; +} diff --git a/src/Services/__tests__/GetStatsService.test.js b/src/Services/__tests__/GetStatsService.test.js new file mode 100644 index 0000000..c12403b --- /dev/null +++ b/src/Services/__tests__/GetStatsService.test.js @@ -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', () => { + }) +}); \ No newline at end of file