chore(scaffolder): added some more tests for the github publisher

This commit is contained in:
blam
2020-07-01 18:30:53 +02:00
parent cc4683d185
commit 365e5921b2
2 changed files with 82 additions and 4 deletions
@@ -14,18 +14,18 @@
* limitations under the License.
*/
const mockIndex = {
export const mockIndex = {
addAll: jest.fn(),
write: jest.fn(),
writeTree: jest.fn().mockResolvedValue('mockoid'),
};
const mockRepo = {
export const mockRepo = {
refreshIndex: jest.fn().mockResolvedValue(mockIndex),
createCommit: jest.fn(),
};
const mockRemote = {
export const mockRemote = {
push: jest.fn(),
};
@@ -18,6 +18,7 @@ jest.mock('@octokit/rest');
jest.mock('nodegit');
import { Octokit } from '@octokit/rest';
import * as NodeGit from 'nodegit';
import { OctokitResponse, ReposCreateInOrgResponseData } from '@octokit/types';
import { GithubPublisher } from './github';
@@ -25,6 +26,16 @@ const { mockGithubClient } = require('@octokit/rest') as {
mockGithubClient: { repos: jest.Mocked<Octokit['repos']> };
};
const { Repository, mockRepo, mockIndex, Signature } = require('nodegit') as {
Repository: jest.Mocked<{ init: any }>;
Signature: jest.Mocked<{ now: any }>;
Cred: jest.Mocked<{ init: any }>;
Remote: jest.Mocked<{ push: any }>;
mockIndex: jest.Mocked<NodeGit.Index>;
mockRepo: jest.Mocked<NodeGit.Repository>;
};
describe('Github Publisher', () => {
const publisher = new GithubPublisher({ client: new Octokit() });
@@ -78,5 +89,72 @@ describe('Github Publisher', () => {
});
});
describe('publish: createGitDirectory', () => {});
describe('publish: createGitDirectory', () => {
const values = {
isOrg: true,
storePath: 'blam/test',
owner: 'lols',
};
const mockDir = '/tmp/test/dir';
mockGithubClient.repos.createInOrg.mockResolvedValue({
data: {
clone_url: 'mockclone',
},
} as OctokitResponse<ReposCreateInOrgResponseData>);
it('should call init on the repo with the directory', async () => {
await publisher.publish({
values,
directory: mockDir,
});
expect(Repository.init).toHaveBeenCalledWith(mockDir, 0);
});
it('should call refresh index on the index and write the new files', async () => {
await publisher.publish({
values,
directory: mockDir,
});
expect(mockRepo.refreshIndex).toHaveBeenCalled();
});
it('should call add all files and write', async () => {
await publisher.publish({
values,
directory: mockDir,
});
expect(mockIndex.addAll).toHaveBeenCalled();
expect(mockIndex.write).toHaveBeenCalled();
expect(mockIndex.writeTree).toHaveBeenCalled();
});
it('should create a commit with on head with the right name and commiter', async () => {
const mockSignature = { mockSignature: 'bloblly' };
Signature.now.mockReturnValue(mockSignature);
await publisher.publish({
values,
directory: mockDir,
});
expect(Signature.now).toHaveBeenCalledTimes(2);
expect(Signature.now).toHaveBeenCalledWith(
'Scaffolder',
'scaffolder@backstage.io',
);
expect(mockRepo.createCommit).toHaveBeenCalledWith(
'HEAD',
mockSignature,
mockSignature,
'initial commit',
'mockoid',
[],
);
});
});
});