github-pages-deploy-action/__tests__/main.test.ts

92 lines
2.2 KiB
TypeScript
Raw Normal View History

2020-12-07 23:12:15 +08:00
/* eslint-disable import/first */
2020-01-20 05:16:28 +08:00
// Initial env variable setup for tests.
2020-03-05 21:19:45 +08:00
process.env['INPUT_FOLDER'] = 'build'
process.env['GITHUB_SHA'] = '123'
process.env['INPUT_DEBUG'] = 'debug'
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
import '../src/main'
import {action} from '../src/constants'
import run from '../src/lib'
import {execute} from '../src/execute'
import {rmRF} from '@actions/io'
import {setFailed, exportVariable} from '@actions/core'
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
const originalAction = JSON.stringify(action)
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
jest.mock('../src/execute', () => ({
2020-01-20 05:16:28 +08:00
execute: jest.fn()
2020-03-05 21:19:45 +08:00
}))
2020-01-20 05:16:28 +08:00
jest.mock('@actions/io', () => ({
rmRF: jest.fn()
}))
2020-03-05 21:19:45 +08:00
jest.mock('@actions/core', () => ({
2020-01-20 05:16:28 +08:00
setFailed: jest.fn(),
getInput: jest.fn(),
2020-03-28 22:35:26 +08:00
exportVariable: jest.fn(),
isDebug: jest.fn(),
info: jest.fn()
2020-03-05 21:19:45 +08:00
}))
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
describe('main', () => {
2020-01-20 05:16:28 +08:00
afterEach(() => {
2020-03-05 21:19:45 +08:00
Object.assign(action, JSON.parse(originalAction))
})
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
it('should run through the commands', async () => {
2020-01-20 05:16:28 +08:00
Object.assign(action, {
2020-03-05 21:19:45 +08:00
repositoryPath: 'JamesIves/github-pages-deploy-action',
2020-06-06 22:55:49 +08:00
folder: 'assets',
2020-03-05 21:19:45 +08:00
branch: 'branch',
gitHubToken: '123',
2020-01-20 05:16:28 +08:00
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
2020-01-20 05:16:28 +08:00
},
isTest: false,
debug: true
2020-03-05 21:19:45 +08:00
})
await run(action)
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
expect(exportVariable).toBeCalledTimes(1)
})
it('should run through the commands and succeed', async () => {
Object.assign(action, {
repositoryPath: 'JamesIves/github-pages-deploy-action',
2020-06-06 22:55:49 +08:00
folder: 'assets',
branch: 'branch',
gitHubToken: '123',
pusher: {
name: 'asd',
email: 'as@cat'
}
})
await run(action)
expect(execute).toBeCalledTimes(12)
expect(rmRF).toBeCalledTimes(1)
expect(exportVariable).toBeCalledTimes(1)
2020-03-05 21:19:45 +08:00
})
2020-01-20 05:16:28 +08:00
2020-03-05 21:19:45 +08:00
it('should throw if an error is encountered', async () => {
2020-01-20 05:16:28 +08:00
Object.assign(action, {
2020-06-06 22:55:49 +08:00
folder: 'assets',
2020-03-05 21:19:45 +08:00
branch: 'branch',
2020-01-20 05:16:28 +08:00
gitHubToken: null,
ssh: null,
2020-01-20 05:16:28 +08:00
accessToken: null,
pusher: {
2020-03-05 21:19:45 +08:00
name: 'asd',
email: 'as@cat'
2020-01-20 05:16:28 +08:00
},
isTest: true
2020-03-05 21:19:45 +08:00
})
await run(action)
expect(execute).toBeCalledTimes(0)
expect(setFailed).toBeCalledTimes(1)
expect(exportVariable).toBeCalledTimes(1)
2020-03-05 21:19:45 +08:00
})
})