import Skypager from '../src'

describe('Require Context Generation', function() {
  it('can generate a require context', function() {
    const project = Skypager.load(process.cwd(), {
      sync: false
    })

    const context = project.select('require-context')

    context.should.be.a('function').that.has.property('keys').that.is.a('function')
    context.should.be.a('function').that.has.property('resolve').that.is.a('function')
    context.keys().should.not.be.empty

    context.keys().should.include('./src/index.js')
    context.resolve('./src/index.js').should.equal(project.resolve('./src/index.js'))
  })

  it('can use a nested origin', function() {
    const project = Skypager.load(process.cwd(), {
      sync: false
    })
   
    const moduleMap = project.select('require-context', {base: '*', origin: './src/selectors/index.js', pattern: 'project/*'}).getModuleMap()    

    moduleMap.should.have.property('./project/dependencies.js')
  }) 

  it('can create a skypager registry', function() {
    const project = Skypager.load(process.cwd(), {
      sync: false
    })
    
    const code = project.select('require-context', {base: '*', registry: 'skypager-registry/lib/context', origin: './src/selectors/index.js', stringify: true, pattern: 'project/*'})

    code.should.include('skypager-registry/lib/context')
  }) 

  it('can generate code for webpack to parse', function() {
    const project = Skypager.load(process.cwd(), {
      sync: false
    })

    const context = project.select('require-context', {
      stringify: true,
      loader: 'skypager-document!skypager-babel',
      query: "?name=[name]",
    })
    
    context.should.be.a('string')
    const mod = project.createModule(context)
    mod.should.have.a.property('exports').that.has.a.property('keys').that.is.a('function')    
    const abs = mod.exports.resolve('./src/index.js')
    abs.should.not.be.empty
  })
  
})