Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding client tests #170

Merged
merged 2 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 61 additions & 0 deletions __tests__/client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
jest.mock('node-fetch');

const core = require('@actions/core')
const fetch = require('node-fetch')
const { Response } = jest.requireActual('node-fetch');
const client = require('../src/client')

describe('the client', () => {

afterAll(() => {
jest.restoreAllMocks()
})

beforeEach(() => {
jest.clearAllMocks()
})

prepareResponse = (body) => {
return Promise.resolve(new Response(body, { Headers: { 'Content-Type': 'application/json' } }))
}

it('should find the change file', async () => {
const files = [
{
"filename": "CHANGELOG.md",
"status": "modified",
"raw_url": "./path/to/CHANGELOG.md"
}
]

fetch.mockReturnValueOnce(prepareResponse(JSON.stringify(files)))

const changelogFile = await client.findChangelog('token', 'repo', 1, 1, 'CHANGELOG.md')
expect(fetch).toHaveBeenCalled()
expect(changelogFile).toStrictEqual({
"filename": "CHANGELOG.md",
"status": "modified",
"raw_url": "./path/to/CHANGELOG.md"
})
})

it('should not find the change file', async () => {
const firstPage = [
{
"filename": "random.md",
"status": "modified",
"raw_url": "./path/to/random.md"
}
]

const secondPage = []

fetch
.mockReturnValueOnce(prepareResponse(JSON.stringify(firstPage)))
.mockReturnValueOnce(prepareResponse(JSON.stringify(secondPage)))

const changelogFile = await client.findChangelog('token', 'repo', 1, 1, 'CHANGELOG.md')
expect(fetch).toHaveBeenCalledTimes(2)
expect(changelogFile).toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion coverage/badge.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6336,10 +6336,10 @@ async function validateLatestVersion(token, expectedLatestVersion, versionPatter
const fetch = __nccwpck_require__(467)
const core = __nccwpck_require__(2186)

module.exports.findChangelog = async function(token, repository, pullRequestNumber, pageSize, changeLogPath) {
module.exports.findChangelog = async function (token, repository, pullRequestNumber, pageSize, changeLogPath) {
let complete = false;
let page = 1
while(!complete) {
while (!complete) {
core.debug(`Downloading page ${page} of pull request files from /repos/${repository}/pulls/${pullRequestNumber}/files`)
const options = addAuth(token, {})
const response = await fetch(`https://api.github.com/repos/${repository}/pulls/${pullRequestNumber}/files?per_page=${pageSize}&page=${page}`, options)
Expand All @@ -6362,7 +6362,7 @@ module.exports.findChangelog = async function(token, repository, pullRequestNumb
return undefined
}

module.exports.downloadChangelog = async function(token, changelogUrl) {
module.exports.downloadChangelog = async function (token, changelogUrl) {
core.debug(`Downloading changelog from ${changelogUrl}`)
const options = addAuth(token, {})
const response = await fetch(`${changelogUrl}`, options)
Expand Down