Files
create-release/tests/create-release.test.js

200 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

2019-09-26 15:24:23 -05:00
jest.mock('@actions/core');
jest.mock('@actions/github');
jest.mock('fs');
2019-09-26 15:24:23 -05:00
const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
const fs = require('fs');
2019-09-27 14:19:53 -05:00
const run = require('../src/create-release.js');
2019-09-26 15:24:23 -05:00
2019-09-20 15:18:26 -05:00
/* eslint-disable no-undef */
2019-09-27 09:06:50 -05:00
describe('Create Release', () => {
2019-09-26 15:24:23 -05:00
let createRelease;
beforeEach(() => {
2019-09-26 15:50:59 -05:00
createRelease = jest.fn().mockReturnValueOnce({
data: {
id: 'releaseId',
html_url: 'htmlUrl',
upload_url: 'uploadUrl'
}
});
2019-09-26 15:24:23 -05:00
context.repo = {
owner: 'owner',
repo: 'repo'
};
2019-12-29 09:36:31 +01:00
context.sha = 'sha';
2019-09-26 15:24:23 -05:00
const github = {
repos: {
createRelease
}
};
GitHub.mockImplementation(() => github);
});
test('Create release endpoint is called', async () => {
2019-09-26 15:50:59 -05:00
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
2019-09-26 15:50:59 -05:00
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
2019-09-26 15:24:23 -05:00
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
2019-09-26 15:24:23 -05:00
draft: false,
2019-12-29 09:36:31 +01:00
prerelease: false,
target_commitish: 'sha'
2019-09-26 15:24:23 -05:00
});
});
2019-09-20 15:29:25 -05:00
2019-09-26 15:50:59 -05:00
test('Draft release is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
2019-09-26 15:50:59 -05:00
.mockReturnValueOnce('true')
.mockReturnValueOnce('false');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
2019-09-26 15:50:59 -05:00
draft: true,
2019-12-29 09:36:31 +01:00
prerelease: false,
target_commitish: 'sha'
2019-09-26 15:50:59 -05:00
});
});
test('Pre-release release is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
2019-09-26 15:50:59 -05:00
.mockReturnValueOnce('false')
.mockReturnValueOnce('true');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: 'myBody',
2019-09-26 15:50:59 -05:00
draft: false,
2019-12-29 09:36:31 +01:00
prerelease: true,
target_commitish: 'sha'
2019-09-26 15:50:59 -05:00
});
});
test('Release with empty body is created', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('') // <-- The default value for body in action.yml
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: '',
draft: false,
2019-12-29 09:36:31 +01:00
prerelease: false,
target_commitish: 'sha'
});
});
test('Release body based on file', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
2020-02-05 22:47:13 -06:00
.mockReturnValueOnce('') // <-- The default value for body in action.yml
.mockReturnValueOnce('false')
2020-02-05 22:47:13 -06:00
.mockReturnValueOnce('false')
.mockReturnValueOnce(null)
2020-02-05 22:47:13 -06:00
.mockReturnValueOnce('notes.md');
fs.readFileSync = jest.fn().mockReturnValueOnce('# this is a release\nThe markdown is strong in this one.');
await run();
expect(createRelease).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
tag_name: 'v1.0.0',
name: 'myRelease',
body: '# this is a release\nThe markdown is strong in this one.',
draft: false,
prerelease: false,
target_commitish: 'sha'
});
});
2019-09-26 15:50:59 -05:00
test('Outputs are set', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
2019-09-26 15:50:59 -05:00
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
core.setOutput = jest.fn();
await run();
expect(core.setOutput).toHaveBeenNthCalledWith(1, 'id', 'releaseId');
expect(core.setOutput).toHaveBeenNthCalledWith(2, 'html_url', 'htmlUrl');
expect(core.setOutput).toHaveBeenNthCalledWith(3, 'upload_url', 'uploadUrl');
});
test('Action fails elegantly', async () => {
core.getInput = jest
.fn()
.mockReturnValueOnce('refs/tags/v1.0.0')
.mockReturnValueOnce('myRelease')
.mockReturnValueOnce('myBody')
2019-09-26 15:50:59 -05:00
.mockReturnValueOnce('false')
.mockReturnValueOnce('false');
createRelease.mockRestore();
createRelease.mockImplementation(() => {
throw new Error('Error creating release');
});
core.setOutput = jest.fn();
core.setFailed = jest.fn();
await run();
expect(createRelease).toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith('Error creating release');
expect(core.setOutput).toHaveBeenCalledTimes(0);
});
2019-09-20 15:29:25 -05:00
});