const testPackage = require('../../helpers/test-package');
const processorFactory = require('./renderLinkInfo');
const extractLinks = require('dgeni-packages/base/services/extractLinks')();
const Dgeni = require('dgeni');
describe('renderLinkInfo processor', () => {
  it('should be available on the injector', () => {
    const dgeni = new Dgeni([testPackage('angular-base-package')]);
    const injector = dgeni.configureInjector();
    const processor = injector.get('renderLinkInfo');
    expect(processor.$process).toBeDefined();
  });
  it('should run before the correct processor', () => {
    const processor = processorFactory(extractLinks);
    expect(processor.$runBefore).toEqual(['convertToJsonProcessor']);
  });
  it('should run after the correct processor', () => {
    const processor = processorFactory(extractLinks);
    expect(processor.$runAfter).toEqual(['fixInternalDocumentLinks']);
  });
  it('should add HTML comments for links out of docs', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
      { path: 'test-2', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
      {
        path: 'test-2',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
    ]);
  });
  it('should order links alphabetically', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
    ]);
  });
  it('should list repeated links only once', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
    ]);
  });
  it('should list internal links before external', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
    ]);
  });
  it('should ignore docs that do not have the specified docType', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
      { path: 'test-2', docType: 'test2', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
                         '\n' +
                         ''
      },
      {
        path: 'test-2',
        docType: 'test2',
        renderedContent: ''
      },
    ]);
  });
  it('should add HTML comments for links into docs', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
      { path: 'test-2', docType: 'test', renderedContent: '' },
      { path: 'test-3', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
      {
        path: 'test-2',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
      {
        path: 'test-3',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
    ]);
  });
  it('should not include links to themselves', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
      { path: 'test-2', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
      {
        path: 'test-2',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
    ]);
  });
  it('should match links that contain fragments or queries', () => {
    const processor = processorFactory(extractLinks);
    processor.docTypes = ['test'];
    const docs = [
      { path: 'test-1', docType: 'test', renderedContent: '' },
      { path: 'test-2', docType: 'test', renderedContent: '' },
      { path: 'test-3', docType: 'test', renderedContent: '' },
    ];
    processor.$process(docs);
    expect(docs).toEqual([
      {
        path: 'test-1',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
      {
        path: 'test-2',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
      {
        path: 'test-3',
        docType: 'test',
        renderedContent: '\n' +
        '\n' +
        ''
      },
    ]);
  });
});