 AvailablePackageView = require '../lib/available-package-view'
+PackageManager = require '../lib/package-manager'
 
 describe "AvailablePackageView", ->
   setPackageStatusSpies = (opts) ->



  beforeEach ->
    @packageManager = jasmine.createSpyObj('packageManager', ['on', 'getClient', 'emit', 'install', 'uninstall'])
    @packageManager.getClient.andCallFake -> jasmine.createSpyObj('client', ['avatar', 'package'])

  it "doesn't show the disable control for a theme", ->
    setPackageStatusSpies {installed: true, disabled: false}

    view.installButton.click()
    expect(@packageManager.install).toHaveBeenCalled()

  it "can be installed if currently not installed and package latest release engine match atom version", ->
    @packageManager.requestPackage.andCallFake (packageName, callback) ->
      pack =
        name: packageName
        releases:
          latest: '0.1.0'
        versions:
          '0.0.1':
            name: packageName
            version: '0.0.1'
            engines:
              atom: '>0.0.0'
          '0.1.0':
            name: packageName
            version: '0.1.0'
            engines:
              atom: '>0.50.0'

      callback(null, pack)

    setPackageStatusSpies {installed: false, disabled: false}

    view = new AvailablePackageView {
      name: 'test-package'
      version: '0.1.0'
      engines:
        atom: '>0.50.0'
    }, @packageManager

    expect(view.installButton.css('display')).not.toBe('none')
    expect(view.uninstallButton.css('display')).toBe('none')
    view.installButton.click()
    expect(@packageManager.install).toHaveBeenCalled()
    expect(@packageManager.install.mostRecentCall.args[0]).toEqual({
      name: 'test-package'
      version: '0.1.0'
      engines:
        atom: '>0.50.0'
    })

  it "can be installed with a previous version whose engine match the current atom version", ->
    @packageManager.requestPackage.andCallFake (packageName, callback) ->
      pack =
        name: packageName
        releases:
          latest: '0.1.0'
        versions:
          '0.0.1':
            name: packageName
            version: '0.0.1'
            engines:
              atom: '>0.50.0'
          '0.1.0':
            name: packageName
            version: '0.0.1'
            engines:
              atom: '>99.0.0'

      callback(null, pack)

    setPackageStatusSpies {installed: false, disabled: false}

    view = new AvailablePackageView {
      name: 'test-package'
      version: '0.1.0'
      engines:
        atom: '>99.0.0'
    }, @packageManager

    expect(view.installButton.css('display')).not.toBe('none')
    expect(view.uninstallButton.css('display')).toBe('none')
    view.installButton.click()
    expect(@packageManager.install).toHaveBeenCalled()
    expect(@packageManager.install.mostRecentCall.args[0]).toEqual({
      name: 'test-package'
      version: '0.0.1'
      engines:
        atom: '>0.50.0'
    })

  it "can't be installed if there is no version compatible with the current atom version", ->
    @packageManager.requestPackage.andCallFake (packageName, callback) ->
      pack =
        name: packageName
        releases:
          latest: '0.1.0'
        versions:
          '0.1.0':
            engines:
              atom: '>99.0.0'

      callback(null, pack)

    setPackageStatusSpies {installed: false, disabled: false}

    view = new AvailablePackageView {
