var child = require("child_process");
var expect = require("expect");
var installer = require("../src/installer");

describe("installer", function() {
  describe(".check", function() {
    context("given resolveable dependencies", function() {
      it("should return []", function() {
        expect(installer.check(["expect", "mocha"])).toEqual([]);
      });
    });

    context("given relative paths", function() {
      it("should return []", function() {
        expect(installer.check(["./something"])).toEqual([]);
      });
    });

    context("given un-resolveable dependencies", function() {
      it("should return them", function() {
        expect(installer.check(["does-not-exist"])).toEqual(["does-not-exist"]);
      });

      context("that import deeply", function() {
        it("should return their module name", function() {
          expect(installer.check(["does-not-exist/lib/test"])).toEqual(["does-not-exist"]);
        });
      });

      context("that import multiple times from the same module", function() {
        it("should return their module name once", function() {
          expect(installer.check(["d-n-e/a", "d-n-e/b"])).toEqual(["d-n-e"]);
        });
      });

      context("that exists in alternative directories", function() {
        it("should return []", function() {
          expect(installer.check(["test"], [process.cwd()])).toEqual([]);
        });
      });
    });

  });

  describe(".install", function() {
    beforeEach(function() {
      this.spy = expect.spyOn(child, "spawnSync");

      expect.spyOn(console, "info");
    });

    afterEach(function() {
      expect.restoreSpies();
    });

    context("given falsey values", function() {
      it("should return undefined", function() {
        expect(installer.install()).toEqual(undefined);
        expect(installer.install(0)).toEqual(undefined);
        expect(installer.install("")).toEqual(undefined);
        expect(installer.install([])).toEqual(undefined);
      });
    });

    context("given empty dependencies", function() {
      it("should return undefined", function() {
        expect(installer.install([])).toEqual(undefined);
      });
    });

    context("given dependencies", function() {
      it("should install them", function() {
        var result = installer.install(["foo", "bar"]);

        expect(this.spy).toHaveBeenCalled();
        expect(this.spy.calls.length).toEqual(1);
        expect(this.spy.calls[0].arguments[0]).toEqual("npm");
        expect(this.spy.calls[0].arguments[1]).toEqual(["install", "foo", "bar"]);
      });
    });
  });
});

@@ -2567,32 +2567,717 @@
 ar%22%5D);%0A      %7D);
+%0A%0A      context(%22given options%22, function() %7B%0A        it(%22should pass them to child process%22, function() %7B%0A          var result = installer.install(%5B%22foo%22, %22bar%22%5D, %7B%0A            save: true,%0A            saveExact: false,%0A            registry: %22https://registry.npmjs.com/%22,%0A          %7D);%0A%0A          expect(this.spy).toHaveBeenCalled();%0A          expect(this.spy.calls.length).toEqual(1);%0A          expect(this.spy.calls%5B0%5D.arguments%5B0%5D).toEqual(%22npm%22);%0A          expect(this.spy.calls%5B0%5D.arguments%5B1%5D).toEqual(%5B%0A            %22install%22,%0A            %22foo%22,%0A            %22bar%22,%0A            %22--save%22,%0A            %22--registry='https://registry.npmjs.com/'%22,%0A          %5D);%0A        %7D);%0A      %7D);
 %0A    %7D);%0A  %7D);%0A%7D

Test for installer.install(deps, options)