2023-01-24 07:44:31 +08:00
|
|
|
import {describe, expect, jest, it, beforeEach} from '@jest/globals';
|
2023-01-23 17:07:14 +08:00
|
|
|
import {Context} from '@actions/github/lib/context';
|
|
|
|
|
|
|
|
import {GitHub, Payload, ReposGetResponseData} from '../src/github';
|
|
|
|
|
2023-01-24 07:44:31 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2023-01-23 17:07:14 +08:00
|
|
|
jest.spyOn(GitHub.prototype, 'context').mockImplementation((): Context => {
|
|
|
|
return new Context();
|
|
|
|
});
|
2023-01-17 18:53:57 +08:00
|
|
|
|
|
|
|
import * as repoFixture from './fixtures/repo.json';
|
2023-01-23 17:07:14 +08:00
|
|
|
jest.spyOn(GitHub.prototype, 'repo').mockImplementation((): Promise<ReposGetResponseData> => {
|
|
|
|
return <Promise<ReposGetResponseData>>(repoFixture as unknown);
|
|
|
|
});
|
|
|
|
|
|
|
|
import * as payloadFixture from './fixtures/github-payload.json';
|
|
|
|
jest.spyOn(GitHub.prototype as any, 'payload').mockImplementation((): Promise<Payload> => {
|
|
|
|
return <Promise<Payload>>(payloadFixture as unknown);
|
|
|
|
});
|
|
|
|
jest.spyOn(GitHub.prototype as any, 'ref').mockImplementation((): string => {
|
|
|
|
return 'refs/heads/master';
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('gitContext', () => {
|
|
|
|
it('returns refs/heads/master', async () => {
|
|
|
|
expect(GitHub.getInstance().gitContext()).toEqual('https://github.com/docker/test-docker-action.git#refs/heads/master');
|
|
|
|
});
|
2023-01-17 18:53:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('repo', () => {
|
|
|
|
it('returns GitHub repository', async () => {
|
2023-01-23 17:07:14 +08:00
|
|
|
const repo = await GitHub.getInstance().repo(process.env.GITHUB_TOKEN || '');
|
|
|
|
expect(repo.name).toEqual('Hello-World');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fromPayload', () => {
|
|
|
|
it('returns repository name from payload', async () => {
|
|
|
|
const repoName = await GitHub.getInstance().fromPayload('repository.name');
|
|
|
|
expect(repoName).toEqual('test-docker-action');
|
2023-01-17 18:53:57 +08:00
|
|
|
});
|
|
|
|
});
|