Skip to content

Commit

Permalink
Test main action function that handles input
Browse files Browse the repository at this point in the history
  • Loading branch information
laupow committed Apr 3, 2023
1 parent ea802da commit a704fa1
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 121 deletions.
175 changes: 91 additions & 84 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe('compileAction', () => {

afterEach(() => {
jest.resetModules();
});

it('should use compile locally with docker by default', async () => {
const dockerCheckMock = jest.fn();
const dockerBuildpackCompileMock = jest.fn();
jest.mock('./docker', () => ({
dockerCheck: dockerCheckMock,
dockerBuildpackCompile: dockerBuildpackCompileMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(dockerCheckMock).toHaveBeenCalled();
expect(dockerBuildpackCompileMock).toHaveBeenCalled();
});

it('should use compile in the cloud if access token is provided', async () => {
// mock particle-access-token input
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation((name: string) => {
if (name === 'particle-access-token') {
return 'test-token';
}
return '';
}),
setFailed: jest.fn(),
info: jest.fn(),
setOutput: jest.fn()
}));

const particleCloudCompileMock = jest.fn().mockResolvedValue('test-binary-id');
const particleDownloadBinaryMock = jest.fn().mockResolvedValue('test-path');
jest.mock('./particle-api', () => ({
particleCloudCompile: particleCloudCompileMock,
particleDownloadBinary: particleDownloadBinaryMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(particleCloudCompileMock).toHaveBeenCalled();
expect(particleDownloadBinaryMock).toHaveBeenCalled();
});

it('should throw an error if cloud compilation fails', async () => {
// mock particle-access-token input
const setFailedMock = jest.fn();
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation((name: string) => {
if (name === 'particle-access-token') {
return 'test-token';
}
return '';
}),
setFailed: setFailedMock,
info: jest.fn(),
setOutput: jest.fn()
}));

const particleCloudCompileMock = jest.fn().mockResolvedValue(null);
jest.mock('./particle-api', () => ({
particleCloudCompile: particleCloudCompileMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(setFailedMock).toHaveBeenCalled();
expect(setFailedMock).toHaveBeenCalledWith('Failed to compile code in cloud');
});
});
Loading

0 comments on commit a704fa1

Please sign in to comment.