Add tests

Signed-off-by: Kevin Snyder <kevin.snyder@gusto.com>
This commit is contained in:
Kevin Snyder
2025-04-07 11:56:16 -07:00
parent e784ba9de0
commit 995d0249e9
11 changed files with 208 additions and 56 deletions
@@ -54,6 +54,9 @@ const initRepoAndPushMocked = initRepoAndPush as jest.Mock<
Promise<{ commitHash: string }>
>;
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
users: {
@@ -81,11 +84,7 @@ const mockOctokit = {
request: jest.fn(),
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('publish:github', () => {
@@ -114,6 +113,7 @@ describe('publish:github', () => {
});
beforeEach(() => {
octokitMock.mockImplementation(() => mockOctokit);
initRepoAndPushMocked.mockResolvedValue({
commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6',
});
@@ -139,6 +139,18 @@ describe('publish:github', () => {
afterEach(jest.resetAllMocks);
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should fail to create if the team is not found in the org', async () => {
mockOctokit.rest.users.getByUsername.mockResolvedValue({
data: { type: 'Organization' },
@@ -24,6 +24,9 @@ import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
import { createGithubActionsDispatchAction } from './githubActionsDispatch';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
actions: {
@@ -32,11 +35,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:actions:dispatch', () => {
@@ -63,6 +62,7 @@ describe('github:actions:dispatch', () => {
beforeEach(() => {
jest.resetAllMocks();
octokitMock.mockImplementation(() => mockOctokit);
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubActionsDispatchAction({
@@ -71,6 +71,18 @@ describe('github:actions:dispatch', () => {
});
});
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call the githubApis for creating WorkflowDispatch without an input object', async () => {
mockOctokit.rest.actions.createWorkflowDispatch.mockResolvedValue({
data: {
@@ -25,6 +25,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { createGithubAutolinksAction } from './githubAutolinks';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
repos: {
@@ -33,11 +36,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:autolinks:create', () => {
@@ -55,6 +54,34 @@ describe('github:autolinks:create', () => {
let action: TemplateAction<any, any>;
const workspacePath = createMockDirectory().resolve('workspace');
it('should pass context logger to Octokit client', async () => {
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubAutolinksAction({
integrations,
githubCredentialsProvider,
});
octokitMock.mockImplementation(() => mockOctokit);
const mockContext = createMockActionContext({
input: {
repoUrl: 'github.com?repo=repo&owner=owner',
},
workspacePath,
});
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call the githubApis for creating alphanumeric autolink reference', async () => {
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
@@ -20,6 +20,9 @@ import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { ConfigReader } from '@backstage/config';
import { ScmIntegrations } from '@backstage/integration';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
repos: {
@@ -29,13 +32,8 @@ const mockOctokit = {
},
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:branch-protection:create', () => {
@@ -59,6 +57,8 @@ describe('github:branch-protection:create', () => {
});
beforeEach(() => {
octokitMock.mockImplementation(() => mockOctokit);
mockOctokit.rest.repos.get.mockResolvedValue({
data: {
default_branch: 'master',
@@ -72,6 +72,18 @@ describe('github:branch-protection:create', () => {
afterEach(jest.resetAllMocks);
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should work with default params', async () => {
await action.handler(mockContext);
@@ -20,6 +20,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-
import { ConfigReader } from '@backstage/config';
import { ScmIntegrations } from '@backstage/integration';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
actions: {
@@ -32,11 +35,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU=';
@@ -66,11 +65,25 @@ describe('github:deployKey:create', () => {
beforeEach(() => {
jest.resetAllMocks();
octokitMock.mockImplementation(() => mockOctokit);
action = createGithubDeployKeyAction({
integrations,
});
});
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should work happy path', async () => {
mockOctokit.rest.actions.getRepoPublicKey.mockResolvedValue({
data: {
@@ -22,6 +22,10 @@ import { ScmIntegrations } from '@backstage/integration';
import { CatalogApi } from '@backstage/catalog-client';
import { mockCredentials, mockServices } from '@backstage/backend-test-utils';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
actions: {
@@ -48,11 +52,7 @@ const mockCatalogClient: Partial<CatalogApi> = {
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
jest.mock('@backstage/catalog-client', () => ({
@@ -91,6 +91,8 @@ describe('github:environment:create', () => {
});
beforeEach(() => {
octokitMock.mockImplementation(() => mockOctokit);
mockOctokit.rest.actions.getEnvironmentPublicKey.mockResolvedValue({
data: {
key: publicKey,
@@ -138,6 +140,18 @@ describe('github:environment:create', () => {
afterEach(jest.resetAllMocks);
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should work happy path', async () => {
await action.handler(mockContext);
@@ -31,6 +31,9 @@ jest.mock('../util', () => {
};
});
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
issues: {
@@ -39,11 +42,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:issues:label', () => {
@@ -71,6 +70,7 @@ describe('github:issues:label', () => {
beforeEach(() => {
jest.resetAllMocks();
octokitMock.mockImplementation(() => mockOctokit);
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubIssuesLabelAction({
@@ -79,6 +79,18 @@ describe('github:issues:label', () => {
});
});
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call the githubApi for adding labels', async () => {
await action.handler(mockContext);
expect(mockOctokit.rest.issues.addLabels).toHaveBeenCalledWith({
@@ -24,16 +24,16 @@ import {
} from '@backstage/integration';
import { createGithubPagesEnableAction } from './githubPagesEnable';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
request: jest.fn(),
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:pages', () => {
@@ -61,6 +61,7 @@ describe('github:pages', () => {
});
beforeEach(() => {
octokitMock.mockImplementation(() => mockOctokit);
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubPagesEnableAction({
@@ -71,6 +72,18 @@ describe('github:pages', () => {
afterEach(jest.resetAllMocks);
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should work happy path', async () => {
await action.handler(mockContext);
@@ -35,6 +35,9 @@ import { entityRefToName } from './gitHelpers';
const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU=';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
users: {
@@ -62,11 +65,7 @@ const mockOctokit = {
request: jest.fn(),
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:repo:create', () => {
@@ -93,6 +92,7 @@ describe('github:repo:create', () => {
});
beforeEach(() => {
octokitMock.mockImplementation(() => mockOctokit);
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubRepoCreateAction({
@@ -110,6 +110,18 @@ describe('github:repo:create', () => {
afterEach(jest.resetAllMocks);
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call the githubApis with the correct values for createInOrg', async () => {
mockOctokit.rest.users.getByUsername.mockResolvedValue({
data: { type: 'Organization' },
@@ -52,6 +52,9 @@ const initRepoAndPushMocked = initRepoAndPush as jest.Mock<
Promise<{ commitHash: string }>
>;
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
repos: {
@@ -60,11 +63,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:repo:push', () => {
@@ -93,6 +92,8 @@ describe('github:repo:push', () => {
beforeEach(() => {
jest.resetAllMocks();
octokitMock.mockImplementation(() => mockOctokit);
initRepoAndPushMocked.mockResolvedValue({ commitHash: 'test123' });
githubCredentialsProvider =
@@ -104,6 +105,18 @@ describe('github:repo:push', () => {
});
});
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call initRepoAndPush with the correct values', async () => {
mockOctokit.rest.repos.get.mockResolvedValue({
data: {
@@ -24,6 +24,9 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-
import { ConfigReader } from '@backstage/config';
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { Octokit } from 'octokit';
const octokitMock = Octokit as unknown as jest.Mock;
const mockOctokit = {
rest: {
repos: {
@@ -32,11 +35,7 @@ const mockOctokit = {
},
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
return mockOctokit;
}
},
Octokit: jest.fn(),
}));
describe('github:repository:webhook:create', () => {
@@ -56,6 +55,7 @@ describe('github:repository:webhook:create', () => {
beforeEach(() => {
jest.resetAllMocks();
octokitMock.mockImplementation(() => mockOctokit);
githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
action = createGithubWebhookAction({
@@ -72,6 +72,18 @@ describe('github:repository:webhook:create', () => {
},
});
it('should pass context logger to Octokit client', async () => {
try {
await action.handler(mockContext);
} catch (e) {
// no-op
}
expect(octokitMock).toHaveBeenCalledWith(
expect.objectContaining({ log: mockContext.logger }),
);
});
it('should call the githubApi for creating repository Webhook', async () => {
const repoUrl = 'github.com?repo=repo&owner=owner';
const webhookUrl = 'https://example.com/payload';