<commit_before>import * as addonManager from 'disco/addonManager';
import { unexpectedSuccess } from 'tests/client/helpers';
import {
  globalEventStatusMap,
  installEventList,
  SET_ENABLE_NOT_AVAILABLE,
} from 'disco/constants';


describe('addonManager', () => {
  let fakeAddon;
  let fakeCallback;
  let fakeInstallObj;
  let fakeInstallUrl;
  let fakeMozAddonManager;

  beforeEach(() => {
    fakeCallback = sinon.stub();
    fakeInstallUrl = 'https://fake-install-url';
    fakeAddon = {
      uninstall: sinon.stub(),
    };
    fakeInstallObj = {
      addEventListener: sinon.spy(function addEventListener(eventName, cb) {
        this[`${eventName}Listener`] = cb;
      }),
      install: sinon.spy(function install() {
        this.onInstallEndedListener();
      }),
    };
    fakeMozAddonManager = {
      createInstall: sinon.stub().returns(Promise.resolve(fakeInstallObj)),
      getAddonByID: sinon.stub(),
      addEventListener: sinon.stub(),
    };
  });

  describe('getAddon()', () => {
    it('should call mozAddonManager.getAddonByID() with id', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      return addonManager.getAddon('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          assert.ok(fakeMozAddonManager.getAddonByID.calledWith('test-id'));
        });
    });

    it('should reject if mozAddonManager.getAddonByID() resolves with falsey addon', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(false));
      return addonManager.getAddon('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Addon not found'));
    });
  });

  describe('install()', () => {
    it(
      'should call mozAddonManager.createInstall() with url',
      () => addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          assert.ok(fakeMozAddonManager.createInstall.calledWith({ url: fakeInstallUrl }));
        }));

    it(
      'should call installObj.addEventListener to setup events',
      () => addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
          .then(() => {
            // It registers an extra onInstallFailed and onInstallEnded listener.
            assert.equal(fakeInstallObj.addEventListener.callCount, installEventList.length + 2);
          }));

    it('should call installObj.install()', () => addonManager.install(
      fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          assert.ok(fakeInstallObj.install.called);
        }));

    it('rejects if the install fails', () => {
      fakeInstallObj.install = sinon.spy(function install() {
        this.onInstallFailedListener();
      });
      return addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(
          unexpectedSuccess,
          () => assert.ok(fakeInstallObj.install.called));
    });

    it('passes the installObj, the event and the id to the callback', () => {
      const fakeEvent = { type: 'fakeEvent' };
      return addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
          .then(() => {
            fakeInstallObj.onDownloadProgressListener(fakeEvent);
            assert.ok(fakeCallback.calledWith(fakeInstallObj, fakeEvent));
          });
    });
  });

  describe('uninstall()', () => {
    it('should reject if getAddonByID resolves with falsey value', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(false));
      // If the code doesn't resolve this will blow up.
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Addon not found'));
    });

    it('should reject if addon.uninstall resolves with false', () => {
      fakeAddon.uninstall.returns(Promise.resolve(false));
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Uninstall failed'));
    });

    it('should resolve if addon.uninstall resolves with true', () => {
      fakeAddon.uninstall.returns(Promise.resolve(true));
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      // If the code doesn't resolve this will blow up.
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager });
    });<commit_msg>Fix lint<commit_after>import * as addonManager from 'disco/addonManager';
import { unexpectedSuccess } from 'tests/client/helpers';
import {
  globalEventStatusMap,
  installEventList,
  SET_ENABLE_NOT_AVAILABLE,
} from 'disco/constants';


describe('addonManager', () => {
  let fakeAddon;
  let fakeCallback;
  let fakeInstallObj;
  let fakeInstallUrl;
  let fakeMozAddonManager;

  beforeEach(() => {
    fakeCallback = sinon.stub();
    fakeInstallUrl = 'https://fake-install-url';
    fakeAddon = {
      uninstall: sinon.stub(),
    };
    fakeInstallObj = {
      addEventListener: sinon.spy(function addEventListener(eventName, cb) {
        this[`${eventName}Listener`] = cb;
      }),
      install: sinon.spy(function install() {
        this.onInstallEndedListener();
      }),
    };
    fakeMozAddonManager = {
      createInstall: sinon.stub().returns(Promise.resolve(fakeInstallObj)),
      getAddonByID: sinon.stub(),
      addEventListener: sinon.stub(),
    };
  });

  describe('getAddon()', () => {
    it('should call mozAddonManager.getAddonByID() with id', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      return addonManager.getAddon('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          assert.ok(fakeMozAddonManager.getAddonByID.calledWith('test-id'));
        });
    });

    it('should reject if mozAddonManager.getAddonByID() resolves with falsey addon', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(false));
      return addonManager.getAddon('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Addon not found'));
    });
  });

  describe('install()', () => {
    it(
      'should call mozAddonManager.createInstall() with url',
      () => addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          assert.ok(fakeMozAddonManager.createInstall.calledWith({ url: fakeInstallUrl }));
        }));

    it(
      'should call installObj.addEventListener to setup events',
      () => addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          // It registers an extra onInstallFailed and onInstallEnded listener.
          assert.equal(fakeInstallObj.addEventListener.callCount, installEventList.length + 2);
        }));

    it('should call installObj.install()', () => addonManager.install(
      fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
      .then(() => {
        assert.ok(fakeInstallObj.install.called);
      }));

    it('rejects if the install fails', () => {
      fakeInstallObj.install = sinon.spy(function install() {
        this.onInstallFailedListener();
      });
      return addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(
          unexpectedSuccess,
          () => assert.ok(fakeInstallObj.install.called));
    });

    it('passes the installObj, the event and the id to the callback', () => {
      const fakeEvent = { type: 'fakeEvent' };
      return addonManager.install(
        fakeInstallUrl, fakeCallback, { _mozAddonManager: fakeMozAddonManager })
        .then(() => {
          fakeInstallObj.onDownloadProgressListener(fakeEvent);
          assert.ok(fakeCallback.calledWith(fakeInstallObj, fakeEvent));
        });
    });
  });

  describe('uninstall()', () => {
    it('should reject if getAddonByID resolves with falsey value', () => {
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(false));
      // If the code doesn't resolve this will blow up.
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Addon not found'));
    });

    it('should reject if addon.uninstall resolves with false', () => {
      fakeAddon.uninstall.returns(Promise.resolve(false));
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager })
        .then(unexpectedSuccess,
          (err) => assert.equal(err.message, 'Uninstall failed'));
    });

    it('should resolve if addon.uninstall resolves with true', () => {
      fakeAddon.uninstall.returns(Promise.resolve(true));
      fakeMozAddonManager.getAddonByID.returns(Promise.resolve(fakeAddon));
      // If the code doesn't resolve this will blow up.
      return addonManager.uninstall('test-id', { _mozAddonManager: fakeMozAddonManager });
    });
{'fraction_non_alphanumeric': 0.13423243072384364, 'fraction_numerical': 0.00021072595090085343, 'mean_word_length': 3.295022624434389, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6598794', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2828, 'n_words': 493}