Skip to content

Commit

Permalink
[test]: Add test for main function
Browse files Browse the repository at this point in the history
  • Loading branch information
danibonilha committed Dec 28, 2023
1 parent 8372672 commit 69426bc
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
6 changes: 3 additions & 3 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import * as main from '../src/main'

// Mock the action's entrypoint
const runMock = jest.spyOn(main, 'createThread').mockImplementation()
const createThreadMock = jest.spyOn(main, 'createThread').mockImplementation()

describe('index', () => {
it('calls run when imported', async () => {
it('calls createThread when imported', async () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../src/index')

expect(runMock).toHaveBeenCalled()
expect(createThreadMock).toHaveBeenCalled()
})
})
114 changes: 114 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Unit tests for the action's main functionality, src/main.ts
*
* These should be run as if the action was called from a workflow.
* Specifically, the inputs listed in `action.yml` should be set as environment
* variables following the pattern `INPUT_<INPUT_NAME>`.
*/

import * as core from '@actions/core'
import * as main from '../src/main'

// Mock the action's main function
const createThreadMock = jest.spyOn(main, 'createThread')

Check failure on line 13 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

'createThreadMock' is assigned a value but never used

Check failure on line 13 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

'createThreadMock' is assigned a value but never used

// Mock the GitHub Actions core library
let infoMock: jest.SpyInstance
let errorMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
let setFailedMock: jest.SpyInstance
let fetchMock: jest.SpyInstance

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

infoMock = jest.spyOn(core, 'info').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()

setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
fetchMock = jest.spyOn(global, 'fetch').mockImplementation()

getInputMock = jest
.spyOn(core, 'getInput')
.mockImplementation((name: string): string => {
switch (name) {
case 'threadName':
return 'v1'
case 'channelName':
return 'test'
case 'webhookUrl':
return 'webhookUrl'
case 'webhookAuth':
return 'webhookAuth'
case 'message':
return 'message'
default:
return ''
}
})
})

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

it('should call webhook to create thread', async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce({}),
text: jest.fn().mockResolvedValueOnce(''),
status: 200
})

await main.createThread()

// Verify that all of the core library functions were called correctly
expect(infoMock).toHaveBeenNthCalledWith(1, 'Thread created')
expect(errorMock).not.toHaveBeenCalled()

expect(fetchMock).toHaveBeenNthCalledWith(1, 'webhookUrl', {
method: 'POST',
headers: {
'Content-type': 'application/json',
Accept: 'application/json',
Authorization: 'webhookAuth'
},
body: JSON.stringify({
channelName: 'test',
title: 'v1',
message: 'message',
tagUser: '0'
})
})
})

it('it should failed action if return is not ok', async () => {

Check failure on line 86 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

should not have duplicate prefix

Check failure on line 86 in __tests__/main.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

should not have duplicate prefix
fetchMock.mockResolvedValueOnce({
ok: false,
json: jest.fn().mockResolvedValueOnce({}),
text: jest.fn().mockResolvedValueOnce(''),
status: 401
})

await main.createThread()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'HTTP error! status: 401')
expect(errorMock).not.toHaveBeenCalled()
})

it('should error if missig inputs', async () => {
getInputMock.mockImplementationOnce(() => {
throw new Error('Input required and not supplied: webhookUrl')
})

await main.createThread()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Input required and not supplied: webhookUrl'
)
})
})

0 comments on commit 69426bc

Please sign in to comment.