Skip to content

Commit

Permalink
Adding client tests (#170)
Browse files Browse the repository at this point in the history
* Adding client tests

* Auto-package action

Co-authored-by: dangoslen <dangoslen@users.noreply.github.com>
  • Loading branch information
dangoslen and dangoslen committed Nov 14, 2021
1 parent 9cc576a commit 3d90780
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
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.

0 comments on commit 3d90780

Please sign in to comment.