From 1305dd7bd9f4b917ae9388e26c367ead854167d0 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Tue, 2 Oct 2018 20:47:43 +0200 Subject: [PATCH 1/3] Add a test --- src/index.ts | 7 +- test/fixtures/issues.opened.json | 159 +++++++++++++++++++++++++++++++ test/index.test.ts | 41 +++++++- tsconfig.json | 3 +- 4 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/issues.opened.json diff --git a/src/index.ts b/src/index.ts index 2137b0e..0064557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,14 @@ import { Application } from 'probot' -export = (app: Application) => { +export default (app: Application) => { // Your code here app.log('Yay, the app was loaded!') + app.on('issues.opened', async context => { + const issueComment = context.issue({ body: 'Thanks for opening this issue!' }) + context.github.issues.createComment(issueComment) + }) + // For more information on building apps: // https://probot.github.io/docs/ diff --git a/test/fixtures/issues.opened.json b/test/fixtures/issues.opened.json new file mode 100644 index 0000000..84ebf04 --- /dev/null +++ b/test/fixtures/issues.opened.json @@ -0,0 +1,159 @@ +{ + "action": "opened", + "issue": { + "url": "https://api.github.com/repos/Codertocat/Hello-World/issues/2", + "repository_url": "https://api.github.com/repos/Codertocat/Hello-World", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/2/labels{/name}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/2/comments", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/2/events", + "html_url": "https://github.com/Codertocat/Hello-World/issues/2", + "id": 327883527, + "node_id": "MDU6SXNzdWUzMjc4ODM1Mjc=", + "number": 2, + "title": "Spelling error in the README file", + "user": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "state": "open", + "locked": false, + "assignee": null, + "assignees": [], + "milestone": null, + "comments": 0, + "created_at": "2018-05-30T20:18:32Z", + "updated_at": "2018-05-30T20:18:32Z", + "closed_at": null, + "author_association": "OWNER", + "body": "It looks like you accidently spelled 'commit' with two 't's." + }, + "repository": { + "id": 135493233, + "node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=", + "name": "Hello-World", + "full_name": "Codertocat/Hello-World", + "owner": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/Codertocat/Hello-World", + "description": null, + "fork": false, + "url": "https://api.github.com/repos/Codertocat/Hello-World", + "forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks", + "keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams", + "hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks", + "issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}", + "events_url": "https://api.github.com/repos/Codertocat/Hello-World/events", + "assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}", + "branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}", + "tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags", + "blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}", + "languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages", + "stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers", + "contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors", + "subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription", + "commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}", + "compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges", + "archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads", + "issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}", + "pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}", + "milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}", + "releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}", + "deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments", + "created_at": "2018-05-30T20:18:04Z", + "updated_at": "2018-05-30T20:18:10Z", + "pushed_at": "2018-05-30T20:18:30Z", + "git_url": "git://github.com/Codertocat/Hello-World.git", + "ssh_url": "git@github.com:Codertocat/Hello-World.git", + "clone_url": "https://github.com/Codertocat/Hello-World.git", + "svn_url": "https://github.com/Codertocat/Hello-World", + "homepage": null, + "size": 0, + "stargazers_count": 0, + "watchers_count": 0, + "language": null, + "has_issues": true, + "has_projects": true, + "has_downloads": true, + "has_wiki": true, + "has_pages": true, + "forks_count": 0, + "mirror_url": null, + "archived": false, + "open_issues_count": 2, + "license": null, + "forks": 0, + "open_issues": 2, + "watchers": 0, + "default_branch": "master" + }, + "sender": { + "login": "Codertocat", + "id": 21031067, + "node_id": "MDQ6VXNlcjIxMDMxMDY3", + "avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/Codertocat", + "html_url": "https://github.com/Codertocat", + "followers_url": "https://api.github.com/users/Codertocat/followers", + "following_url": "https://api.github.com/users/Codertocat/following{/other_user}", + "gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions", + "organizations_url": "https://api.github.com/users/Codertocat/orgs", + "repos_url": "https://api.github.com/users/Codertocat/repos", + "events_url": "https://api.github.com/users/Codertocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/Codertocat/received_events", + "type": "User", + "site_admin": false + } +} \ No newline at end of file diff --git a/test/index.test.ts b/test/index.test.ts index c800167..6987795 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,10 +1,43 @@ -// You can import your modules -// import index from '../src/index' +import { Application } from 'probot' +// Requiring our app implementation +import myProbotApp from '../src' + +const issuesOpenedPayload = require('./fixtures/issues.opened.json') test('that we can run tests', () => { // your real tests go here expect(1 + 2 + 3).toBe(6) }) -// For more information about using TypeScript in your tests, Jest recommends: -// https://github.com/kulshekhar/ts-jest +describe('My Probot app', () => { + let app: Application + let github: any + + beforeEach(() => { + app = new Application() + // Initialize the app based on the code from index.js + app.load(myProbotApp) + // This is an easy way to mock out the GitHub API + github = { + issues: { + createComment: jest.fn().mockReturnValue(Promise.resolve({})) + } + } + // Passes the mocked out GitHub API into out app instance + app.auth = () => Promise.resolve(github) + }) + + test('creates a comment when an issue is opened', async () => { + // Simulates delivery of an issues.opened webhook + await app.receive({ + name: 'issues.opened', + payload: issuesOpenedPayload + }) + + // This test passes if the code in your index.js file calls `context.github.issues.createComment` + expect(github.issues.createComment).toHaveBeenCalled() + }) +}) + +// For more information about testing with Jest see: +// https://facebook.github.io/jest/ diff --git a/tsconfig.json b/tsconfig.json index e4c4618..fd1eeb2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,7 +18,8 @@ "declaration": true }, "include": [ - "src/**/*" + "src/**/*", + "test/**/*" ], "compileOnSave": false } From 3fcf34a6e13ccb61c0052c68ecb8f56a9643c768 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 3 Oct 2018 14:04:19 +0200 Subject: [PATCH 2/3] Change index.js to index.ts in comments --- test/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 6987795..4677b97 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -15,7 +15,7 @@ describe('My Probot app', () => { beforeEach(() => { app = new Application() - // Initialize the app based on the code from index.js + // Initialize the app based on the code from index.ts app.load(myProbotApp) // This is an easy way to mock out the GitHub API github = { @@ -34,7 +34,7 @@ describe('My Probot app', () => { payload: issuesOpenedPayload }) - // This test passes if the code in your index.js file calls `context.github.issues.createComment` + // This test passes if the code in your index.ts file calls `context.github.issues.createComment` expect(github.issues.createComment).toHaveBeenCalled() }) }) From a22902ec23768be2c3d1fc0da9d7f0576c358c29 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Tue, 9 Oct 2018 13:49:53 +0200 Subject: [PATCH 3/3] Remove test directory from include in tsconfig.json --- tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index fd1eeb2..e4c4618 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,8 +18,7 @@ "declaration": true }, "include": [ - "src/**/*", - "test/**/*" + "src/**/*" ], "compileOnSave": false }