const Octocat = require('./octokat-stub')
const octocat = new Octocat()
const main = require('./main')
main(octocat).then(function(result) {
console.log(octocat.spies.getAll()) // see spy-result.json
})
Created
May 6, 2016 09:32
-
-
Save timaschew/44de7de88d875971a5369a0046d5ba7d to your computer and use it in GitHub Desktop.
stub for octokat.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
class Spies { | |
constructor() { | |
this.db = {} | |
} | |
add(name, input, output) { | |
const item = this.db[name] || { | |
callCount: 0, | |
entries: [] | |
} | |
item.entries.push({ | |
args: input || null, | |
returnValues: output || null | |
}) | |
item.callCount++ | |
this.db[name] = item | |
} | |
getAll() { | |
return this.db | |
} | |
get(name, index) { | |
if (index == null) { | |
return this.db[name] | |
} else { | |
return ((this.db[name] || {}).entries || [])[index] | |
} | |
} | |
} | |
function createRepoStub(spies) { | |
let stub = null | |
stub = { | |
// repo itself | |
fetch: () => { | |
return new Promise((resolve, reject) => { | |
spies.add('repos.fetch', null, null) // no need to pass this stub to the spy | |
resolve(stub) | |
}) | |
}, | |
forks: { | |
create: () => { | |
return new Promise((resolve, reject) => { | |
spies.add('forks.create', null, null) | |
resolve() | |
}) | |
} | |
}, | |
commits: { | |
fetch: (data) => { | |
return new Promise((resolve, reject) => { | |
const result = [ | |
{sha: 'abc'} | |
] | |
spies.add('commits.fetch', data, result) | |
resolve(result) | |
}) | |
} | |
}, | |
git: { | |
refs: (ref) => { | |
spies.add('git.refs', ref) | |
return { | |
update: (data) => { | |
return new Promise((resolve, reject) => { | |
spies.add('git.refs.update', data) | |
resolve() | |
}) | |
} | |
} | |
} | |
}, | |
contents: (filePath) => { | |
spies.add('contents', filePath) | |
return { | |
fetch: (ref) => { | |
return new Promise((resolve, reject) => { | |
const result = {sha: 'def'} | |
spies.add('contents.fetch', ref, result) | |
resolve(result) | |
}) | |
}, | |
add: (config) => { | |
return new Promise((resolve, reject) => { | |
spies.add('contents.add', config) | |
resolve() | |
}) | |
}, | |
remove: (config) => { | |
return new Promise((resolve, reject) => { | |
spies.add('contents.remove', config) | |
// preserve undefined, cast to null when do JSON.stringify later | |
if (config.content == null) { | |
config.content = null | |
} | |
resolve() | |
}) | |
} | |
} | |
}, | |
pulls: (pullRequestNumner) => { | |
spies.add('pulls', pullRequestNumner) | |
return { | |
merge: { | |
add: (data) => { | |
return new Promise((resolve, reject) => { | |
const result = { | |
merged: true, | |
message: 'was merged' | |
} | |
spies.add('pulls.merge.add', data, result) | |
resolve(result) | |
}) | |
} | |
} | |
} | |
} | |
} | |
// patch properties to functions, like git.refs() and pulls() | |
stub.git.refs.fetch = () => { | |
return new Promise((resolve, reject) => { | |
const result = [ | |
{ref: 'refs/heads/master'} | |
] | |
spies.add('git.refs.fetch', null, result) | |
resolve(result) | |
}) | |
} | |
stub.git.refs.create = (data) => { | |
return new Promise((resolve, reject) => { | |
spies.add('git.refs.fetch', data) | |
resolve() | |
}) | |
} | |
stub.pulls.create = (data) => { | |
return new Promise((resolve, reject) => { | |
resolve({ | |
number: 1, | |
head: { | |
sha: 'ghi' | |
} | |
}) | |
}) | |
} | |
stub.pulls.fetch = (data) => { | |
return new Promise((resolve, reject) => { | |
resolve([]) | |
}) | |
} | |
return stub | |
} | |
module.exports = class Octocat { | |
constructor(options) { | |
this.options = options | |
this.spies = new Spies() | |
this.stub = createRepoStub(this.spies) | |
} | |
repos(username, repo) { | |
this.spies.add('repos', [username, repo]) | |
return this.stub | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"repos": { | |
"callCount": 2, | |
"entries": [ | |
{ | |
"args": [ | |
"timaschew", | |
"octocat.js" | |
], | |
"returnValues": null | |
}, | |
{ | |
"args": [ | |
"philschatz", | |
"octocat.js" | |
], | |
"returnValues": null | |
} | |
] | |
}, | |
"forks.create": { | |
"callCount": 1, | |
"entries": [ | |
{ | |
"args": null, | |
"returnValues": null | |
} | |
] | |
}, | |
"repos.fetch": { | |
"callCount": 1, | |
"entries": [ | |
{ | |
"args": null, | |
"returnValues": null | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment