generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8372672
commit 69426bc
Showing
2 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / TypeScript Tests
|
||
|
||
// 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 GitHub Actions / TypeScript Tests
|
||
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' | ||
) | ||
}) | ||
}) |