// localLookupStub returns the text of what the path it will be looking up
// Doesn't really return the actual result from the service
// This is fair enough from a unit test point of view
// If this is too brittle, we can convert it into a proper integration test.
const { getProjectPathStub, fixturesBasePath: base, async, localLookupStub } = require('../spec-helper');

// This module is pretty crucial to how babelmodule operates
// Use the fixtures folder as your test dummy
const lookupAlias = require('../../lib/utils/lookup-alias');

describe('module lookup: babel',() => {
  let subject;
  let findBabelConfigMock = jasmine.createSpy('findBabelConfig');

  beforeEach(() => {
    subject = new (require('../../lib/lookups/module/babel'))
      (getProjectPathStub, require('path'),
      findBabelConfigMock, localLookupStub, lookupAlias);
  });

  describe('no config', () => {
    beforeEach(() => {
      // if c === null, the config wasn't found
      findBabelConfigMock.andReturn(Promise.resolve(null));
    });
    it('should not lookup anything', async((done) => {
      subject.getList('someprefix', '/some/path')
      .then(result => {
        done(() => {
          expect(result.length).toBe(0);});
      });
    }));
  });

  describe('v1 - babel-plugin-module-alias', () => {
    beforeEach(() => {
      findBabelConfigMock.andReturn(Promise.resolve({ config: {
        'plugins': [
          ['module-alias', [
            { 'src': './subfolder/innerFolder', 'expose': 'inny' },
            { 'src': './src/fake', 'expose': 'notexist' }
          ]]
        ]}}));
    });

    it('should return aliased suggestions', async((done) => {
      subject.getList('inny/function', `${base}/testbed.js`)
      .then((results) => {
        done(() => {
          expect(results.length).toBe(1);
          expect(results[0].text).toBe(`${base}/subfolder/innerFolder`);
        });
      }).catch(e => { throw new Error(e); });
    }));
  });

  describe('v2 - babel-plugin-module-resolver', () => {
    let v2Config;

    beforeEach(() => {
      v2Config = { config: {
          'plugins': [
            ["module-resolver", {
              "alias": {
                "nonexist": "./src/fake",
                "inny": "./subfolder/innerFolder" }
              }]
          ]}
        };
      findBabelConfigMock.andReturn(Promise.resolve(v2Config));
    });

    it('should return aliased suggestions', async((done) => {
      subject.getList('inny/function', `${base}/testbed.js`)
      .then((results) => {
        done(() => {
          expect(results.length).toBe(1);
          expect(results[0].text).toBe(`${base}/subfolder/innerFolder`);
        });
      }).catch(e => { throw new Error(e); });
    }));

    describe('with custom root directories', () => {
        beforeEach(() => {
          v2Config = { config: {
              'plugins': [
                ["module-resolver", {
                  "root": ["./subfolder"],
                  "alias": {
                    "wrong": "./other_modules/something",
                    "inny": "./subfolder/innerFolder" }
                }]
              ]}
            };
          findBabelConfigMock.andReturn(Promise.resolve(v2Config));
        });

        it('should return aliased suggestions and root suggestions', async((done) => {
          subject.getList('inny/function', `${base}/testbed.js`)
          .then((results) => {
            done(() => {
              expect(results.length).toBe(2);
            });
          }).catch(e => { throw new Error(e); });
        }));

        it('should return root suggestions', async(done => {
          subject
          .getList('namedFunction', `${base}/testbed.js`)
          .then(results => {
            done(() => {
                expect(results).toHaveLength(1);
                expect(results[0].text).toBe(`${base}/subfolder`);
            });
          })
          .catch(e => {
            throw new Error(e);
          });
      }));
    });
  });
});

@@ -3966,24 +3966,789 @@
 ;%0A      %7D));
+%0A%0A        describe('root as .', () =%3E %7B%0A          beforeEach(() =%3E %7B%0A            v2Config.config.plugins = %5B%0A                  %5B%22module-resolver%22, %7B%0A                    %22root%22: %5B%22.%22%5D%0A                  %7D%5D%0A                %5D;%0A            findBabelConfigMock.andReturn(Promise.resolve(v2Config));%0A          %7D);%0A%0A          it('should return suggestions from base', async(done =%3E %7B%0A            subject%0A            .getList('subfolder/innerFolder', %60$%7Bbase%7D/testbed.js%60)%0A            .then(results =%3E %7B%0A              done(() =%3E %7B%0A                  expect(results).toHaveLength(1);%0A                  expect(results%5B0%5D.text).toBe(%60$%7Bbase%7D%60);%0A              %7D);%0A            %7D)%0A            .catch(e =%3E %7B%0A              throw new Error(e);%0A            %7D);%0A        %7D));%0A        %7D);
 %0A    %7D);%0A  %7D

Include additonal sanity spec for babel