Skip to content

Commit

Permalink
chore: add github tests (#4)
Browse files Browse the repository at this point in the history
* add github tests

This adds tests of the 'github' module.

Signed-off-by: Mike Ball <[email protected]>

* remove line break

Signed-off-by: Mike Ball <[email protected]>

---------

Signed-off-by: Mike Ball <[email protected]>
  • Loading branch information
mdb authored Feb 10, 2023
1 parent 761f607 commit 4f6f1e6
Showing 1 changed file with 132 additions and 8 deletions.
140 changes: 132 additions & 8 deletions src/github.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,145 @@
/* eslint-env jest */
const core = require('@actions/core')
// const github = require('./github')
const github = require('./github')

jest.spyOn(core, 'setOutput')
const mockOctokit = {
request: jest.fn()
}

jest.spyOn(mockOctokit, 'request')

jest.mock('@actions/github', () => {
return {
getOctokit: jest.fn(() => mockOctokit)
}
})

describe('github', () => {
beforeEach(() => {
describe('getOctokit', () => {
it('wraps @actions/github\'s getOctokit', () => {
expect(github.getOctokit()).toBe(mockOctokit)
})
})

describe('getLatestTag', () => {
let tag

afterEach(() => {
tag = undefined
})

describe('when the targeted repo has existing tags', () => {
beforeEach(async () => {
mockOctokit.request.mockImplementation(() => {
return {
data: ['1.0.0']
}
})

tag = await github.getLatestTag(mockOctokit, 'owner', 'repo')
})

it('fetches the repo\'s tags via the GitHub API', () => {
expect(mockOctokit.request).toHaveBeenCalledWith('GET /repos/{owner}/{repo}/tags?per_page=1', {
owner: 'owner',
repo: 'repo'
})
})

it('returns the latest tag returned by the GitHub API', () => {
expect(tag).toBe('1.0.0')
})
})

describe('when the targeted repo has no existing tags', () => {
beforeEach(async () => {
mockOctokit.request.mockImplementation(() => {
return {
data: []
}
})

tag = await github.getLatestTag(mockOctokit, 'owner', 'repo')
})

it('fetches the repo\'s tags via the GitHub API', () => {
expect(mockOctokit.request).toHaveBeenCalledWith('GET /repos/{owner}/{repo}/tags?per_page=1', {
owner: 'owner',
repo: 'repo'
})
})

it('returns undefined', () => {
expect(tag).toBe(undefined)
})
})

describe('when the underlying octokit API request fails', () => {
it('throws an error', async () => {
const result = 'error'

mockOctokit.request.mockRejectedValueOnce(result)

await expect(github.getLatestTag(mockOctokit, 'owner', 'repo')).rejects.toMatch(result)
})
})
})

afterEach(() => {
jest.resetAllMocks()
describe('compareCommits', () => {
let result

beforeEach(async () => {
mockOctokit.request.mockImplementation(() => {
return {
data: {
commits: [{
sha: 'commit-sha',
commit: {
message: 'commit-message'
}
}]
}
}
})

result = await github.compareCommits(mockOctokit, 'owner', 'repo', 'base', 'head')
})

it('fetches a comparison of the specified base and head commits via the GitHub API', () => {
expect(mockOctokit.request).toHaveBeenCalledWith('GET /repos/{owner}/{repo}/compare/{basehead}', {
owner: 'owner',
repo: 'repo',
basehead: 'base...head'
})
})

it('returns a list of commits', () => {
expect(result).toStrictEqual([{
message: 'commit-message',
sha: 'commit-sha'
}])
})
})

describe('setVersionOutputs()', () => {
it('should set output for all values', () => {
describe('createRelease', () => {
let result

beforeEach(async () => {
mockOctokit.request.mockImplementation(() => 'success')

result = await github.createRelease(mockOctokit, 'owner', 'repo', '1.0.0')
})

it('creates a release via the GitHub API', () => {
expect(mockOctokit.request).toHaveBeenCalledWith('POST /repos/{owner}/{repo}/releases', {
owner: 'owner',
repo: 'repo',
tag_name: '1.0.0',
generate_release_notes: true
})
})

it('returns the result of the underlying octokit GitHub API request', () => {
expect(result).toBe('success')
})
})
})

0 comments on commit 4f6f1e6

Please sign in to comment.