Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinedc committed Jan 10, 2024
1 parent ec101e0 commit 5f0e782
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion run/jobs/updateExplorerSyncingProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = async job => {

if (data.reset) {
await pm2.reset(explorer.slug, explorer.workspaceId);
return 'Process reset';
return 'Process reset.';
}
else if (!explorer && existingProcess) {
await pm2.delete(data.explorerSlug);
Expand Down
16 changes: 16 additions & 0 deletions run/tests/jobs/updateExplorerSyncingProcess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ beforeEach(() => jest.clearAllMocks());
const hasReachedTransactionQuota = jest.fn().mockResolvedValue(false);

describe('updateExplorerSyncingProcess', () => {
it('Should reset if flag is passed', (done) => {
const reset = jest.fn();
PM2.mockImplementationOnce(() => ({
reset,
find: jest.fn().mockResolvedValue({ data: { pm2_env: { status: 'online' }}})
}));
jest.spyOn(Explorer, 'findOne').mockResolvedValue({ slug: 'slug', workspaceId: 1 });

updateExplorerSyncingProcess({ data: { explorerSlug: 'explorer', reset: true }})
.then(res => {
expect(reset).toHaveBeenCalledWith('slug', 1);
expect(res).toEqual('Process reset.');
done();
});
});

it('Should delete if no explorer', (done) => {
PM2.mockImplementationOnce(() => ({
delete: jest.fn(),
Expand Down
25 changes: 24 additions & 1 deletion run/tests/lib/pm2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,30 @@ const PM2 = require('../../lib/pm2');
beforeEach(() => jest.clearAllMocks());

const host = 'http://pm2';
const secret= 'secret';
const secret = 'secret';

describe('reset', () => {
it('Should throw an error if missing parameter', (done) => {
const pm2 = new PM2(host, secret);
pm2.reset()
.catch(error => {
expect(error).toEqual(new Error('Missing parameter'));
done();
});
});

it('Should reset the process', (done) => {
jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: null });

const pm2 = new PM2(host, secret);
pm2.reset('slug', 1)
.then(() => {
expect(axios.post).toHaveBeenCalledWith('http://pm2/processes/slug/delete?secret=secret');
expect(axios.post).toHaveBeenCalledWith('http://pm2/processes?secret=secret', { slug: 'slug', workspaceId: 1 });
done();
});
});
});

describe('restart', () => {
it('Should throw an error if missing parameter', () => {
Expand Down

0 comments on commit 5f0e782

Please sign in to comment.