Skip to content

Commit

Permalink
chore: Added tests for api.js
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed Aug 26, 2024
1 parent 40e81e8 commit a6008d5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
105 changes: 105 additions & 0 deletions __tests__/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const {
addComment,
approvePullRequest,
comparePullRequest,
getPullRequest
} = require('../src/api')

// Mock for the GitHub API client
const github = {
rest: {
issues: {
createComment: jest.fn()
},
pulls: {
createReview: jest.fn()
},
repos: {
compare: jest.fn(),
get: jest.fn()
}
}
}

// Mock data for the repository and pull request
const repo = {
owner: {
login: 'test-owner'
},
name: 'test-repo'
}

const pullRequest = {
base: {
ref: 'base-branch'
},
number: 123,
head: {
ref: 'head-branch'
}
}

describe('Tests for `addComment` function', () => {
test('should add a comment to the pull request', async () => {
const body = 'This is a test comment.'

await addComment(github, repo, pullRequest, body)

expect(github.rest.issues.createComment).toHaveBeenCalledWith({
owner: repo.owner.login,
repo: repo.name,
issue_number: pullRequest.number,
body
})
})
test('should approve the pull request', async () => {
const body = 'This is a test review.'

await approvePullRequest(github, repo, pullRequest, body)

expect(github.rest.pulls.createReview).toHaveBeenCalledWith({
owner: repo.owner.login,
repo: repo.name,
pull_number: pullRequest.number,
event: 'APPROVE',
body
})
})
test('should compare a pull request in a GitHub repository', async () => {
const baseRef = 'base-branch'
const headRef = 'head-branch'

const comparisonResult = {
// Mock comparison result
}

github.rest.repos.compare = jest.fn().mockResolvedValue(comparisonResult)

const result = await comparePullRequest(github, repo, pullRequest)

expect(github.rest.repos.compare).toHaveBeenCalledWith({
owner: repo.owner.login,
repo: repo.name,
basehead: `${baseRef}...${headRef}`
})

expect(result).toBe(comparisonResult)
})
test('should retrieve a pull request from a GitHub repository', async () => {
const pullRequestData = {
// Mock pull request data
}

github.rest.pulls.get = jest.fn().mockResolvedValue(pullRequestData)

const result = await getPullRequest(github, repo, pullRequest)

expect(github.rest.pulls.get).toHaveBeenCalledWith({
owner: repo.owner.login,
repo: repo.name,
pull_number: pullRequest.number
})

expect(result).toBe(pullRequestData)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a6008d5

Please sign in to comment.