Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
josephearl committed Apr 13, 2017
1 parent 7556943 commit 9c34ef4
Show file tree
Hide file tree
Showing 4 changed files with 464 additions and 583 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"file-loader": "^0.8.5",
"identity-obj-proxy": "^3.0.0",
"imports-loader": "^0.6.5",
"jest-cli": "^16.0.1",
"jest-cli": "19.1.0-alpha.eed82034",
"lint-staged": "^3.1.0",
"node-sass": "^3.10.0",
"npm-check": "^5.2.3",
Expand Down Expand Up @@ -101,7 +101,7 @@
"history": "^2.1.2",
"immutability-helper": "^2.0.0",
"immutable": "^3.7.6",
"jest": "^17.0.0",
"jest": "19.1.0-alpha.eed82034",
"js-base64": "^2.1.9",
"js-yaml": "^3.7.0",
"json-loader": "^0.5.4",
Expand Down
5 changes: 2 additions & 3 deletions src/backends/github/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default class API {
.then(branchData => this.updateTree(branchData.commit.sha, "/", fileTree))
.then(changeTree => this.commit(options.commitMessage, changeTree))
.then(commitResponse => this.createBranch(branchName, commitResponse.sha))
.then(branchResponse => this.createPR(options.commitMessage, branchName, this.branch))
.then(branchResponse => this.createPR(options.commitMessage, branchName))
.then(prResponse => this.user().then(user => user.name ? user.name : user.login)
.then(username => this.storeMetadata(contentKey, {
type: "PR",
Expand Down Expand Up @@ -386,7 +386,7 @@ export default class API {
return this.deleteRef("heads", branchName);
}

createPR(title, head, base = "master") {
createPR(title, head, base = this.branch) {
const body = "Automatically generated by Netlify CMS";
return this.request(`${ this.repoURL }/pulls`, {
method: "POST",
Expand Down Expand Up @@ -511,5 +511,4 @@ export default class API {
body: JSON.stringify({ message, tree, parents }),
});
}

}
39 changes: 39 additions & 0 deletions src/backends/github/__tests__/API.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import AssetProxy from "../../../valueObjects/AssetProxy";
import API from "../API";

describe('github API', () => {
const mockAPI = (api, responses) => {
api.request = (path, options = {}) => {
const normalizedPath = path.indexOf('?') !== -1 ? path.substr(0, path.indexOf('?')) : path;
const response = responses[normalizedPath];
return typeof response === 'function'
? Promise.resolve(response(options))
: Promise.reject(new Error(`No response for path '${normalizedPath}'`))
};
}

it('should create PR with correct base branch name when publishing with editorial workflow', () => {
let prBaseBranch = null;
const api = new API({ branch: 'gh-pages', repo: 'my-repo' });
const responses = {
'/repos/my-repo/branches/gh-pages': () => ({ commit: { sha: 'def' } }),
'/repos/my-repo/git/trees/def': () => ({ tree: [] }),
'/repos/my-repo/git/trees': () => ({}),
'/repos/my-repo/git/commits': () => ({}),
'/repos/my-repo/git/refs': () => ({}),
'/repos/my-repo/pulls': (pullRequest) => {
prBaseBranch = JSON.parse(pullRequest.body).base;
return { head: { sha: 'cbd' } };
},
'/user': () => ({}),
'/repos/my-repo/git/blobs': () => ({}),
'/repos/my-repo/git/refs/meta/_netlify_cms': () => ({ 'object': {} })
};
mockAPI(api, responses);

return expect(
api.editorialWorkflowGit(null, { slug: 'entry', sha: 'abc' }, null, {})
.then(() => prBaseBranch)
).resolves.toEqual('gh-pages')
});
});
Loading

0 comments on commit 9c34ef4

Please sign in to comment.