{"size": 3192, "ext": "js", "max_stars_repo_path": "__test__/actions/install.spec.js", "max_stars_repo_name": "compose-generator/go-npm", "max_stars_repo_stars_event_min_datetime": "2021-08-12T06:25:09.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-26T03:09:04.000Z", "max_issues_repo_path": "__test__/actions/install.spec.js", "max_issues_repo_name": "compose-generator/go-npm", "max_issues_repo_issues_event_min_datetime": "2021-08-31T11:41:48.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-01T10:42:19.000Z", "max_forks_repo_path": "__test__/actions/install.spec.js", "max_forks_repo_name": "compose-generator/go-npm", "max_forks_repo_forks_event_min_datetime": "2021-08-13T20:00:00.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-20T23:10:49.000Z"}
{"max_stars_count": 2, "max_issues_count": 2, "max_forks_count": 4, "avg_line_length": 30.4, "max_line_length": 109, "alphanum_fraction": 0.6754385965}
const { EventEmitter } = require('events');
const request = require('request');
const common = require('../../src/common');
const install = require('../../src/actions/install');
const move = require('../../src/assets/move');
const untar = require('../../src/assets/untar');
const unzip = require('../../src/assets/unzip');
const verifyAndPlaceCallback = require('../../src/assets/binary');

jest.mock('fs');
jest.mock('mkdirp');
jest.mock('request');
jest.mock('../../src/common');
jest.mock('../../src/assets/move');
jest.mock('../../src/assets/untar');
jest.mock('../../src/assets/unzip');
jest.mock('../../src/assets/binary');

describe('install()', () => {

  let callback, requestEvents;

  beforeEach(() => {

    callback = jest.fn();

    requestEvents = new EventEmitter();
  });

  it('should call callback with error if package.json did not return value' , () => {
    common.parsePackageJson.mockReturnValueOnce(undefined);

    install(callback);

    expect(callback).toHaveBeenCalledWith('Invalid inputs');
  });

  it('should call callback with error on request error', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });

    install(callback);

    requestEvents.emit('error');

    expect(callback).toHaveBeenCalledWith('Error downloading from URL: http://url');
  });

  it('should call callback with error on response with status code different than 200', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });

    install(callback);

    requestEvents.emit('response', { statusCode: 404 });

    expect(callback).toHaveBeenCalledWith('Error downloading binary. HTTP Status Code: 404');
  });

  it('should pick move strategy if url is an uncompressed binary', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });

    install(callback);

    requestEvents.emit('response', { statusCode: 200 });

    expect(move).toHaveBeenCalled();
  });

  it('should pick untar strategy if url ends with .tar.gz', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url.tar.gz' });

    install(callback);

    requestEvents.emit('response', { statusCode: 200 });

    expect(untar).toHaveBeenCalled();
  });

  it('should pick unzip strategy if url ends with .zip', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url.zip' });

    install(callback);

    requestEvents.emit('response', { statusCode: 200 });

    expect(unzip).toHaveBeenCalled();
  });

  it('should call verifyAndPlaceCallback on success', () => {
    request.mockReturnValueOnce(requestEvents);
    common.parsePackageJson.mockReturnValueOnce({ url: 'http://url', binName: 'command', binPath: './bin' });
    move.mockImplementationOnce(({ onSuccess }) => onSuccess());

    install(callback);

    requestEvents.emit('response', { statusCode: 200 });

    expect(verifyAndPlaceCallback).toHaveBeenCalledWith('command', './bin', callback);
  });
});
