var DOM = require('../src/dom.js').DOM;
var normalizeHeaderToId = require('../src/dom.js').normalizeHeaderToId;
describe('dom', function() {
  var dom;
  beforeEach(function() {
    dom = new DOM();
  });
  describe('html', function() {
    it('should add ids to all h tags', function() {
      dom.html('
Some Header
');
      expect(dom.toString()).toContain('');
    });
    it('should collect  anchors too', function() {
      dom.html('Xxx  and bar ');
      expect(dom.anchors).toContain('foo');
      expect(dom.anchors).toContain('bar');
    })
  });
  it('should collect h tag ids', function() {
    dom.h('Page Title', function() {
      dom.html('Second
xxx Third
');
      dom.h('Another Header', function() {});
    });
    expect(dom.anchors).toContain('page-title');
    expect(dom.anchors).toContain('second');
    expect(dom.anchors).toContain('second_third');
    expect(dom.anchors).toContain('another-header');
  });
  describe('h', function() {
    it('should render using function', function() {
      var cbThis;
      var cdValue;
      dom.h('heading', 'content', function(value){
        cbThis = this;
        cbValue = value;
      });
      expect(cbThis).toEqual(dom);
      expect(cbValue).toEqual('content');
    });
    it('should update heading numbers', function() {
      dom.h('heading', function() {
        this.html('sub-heading
');
      });
      expect(dom.toString()).toContain('heading
');
      expect(dom.toString()).toContain('sub-heading
');
    });
    it('should properly number nested headings', function() {
      dom.h('heading', function() {
        dom.h('heading2', function() {
          this.html('heading3
');
        });
      });
      dom.h('other1', function() {
        this.html('other2
');
      });
      expect(dom.toString()).toContain('heading
');
      expect(dom.toString()).toContain('heading2
');
      expect(dom.toString()).toContain('heading3
');
      expect(dom.toString()).toContain('other1
');
      expect(dom.toString()).toContain('other2
');
    });
    it('should add nested ids to all h tags', function() {
      dom.h('Page Title', function() {
        dom.h('Second', function() {
          dom.html('some Third
');
        });
      });
      var resultingHtml = dom.toString();
      expect(resultingHtml).toContain('Page Title
');
      expect(resultingHtml).toContain('Second
');
      expect(resultingHtml).toContain('Third
');
    });
  });
  describe('normalizeHeaderToId', function() {
    it('should ignore content in the parenthesis', function() {
      expect(normalizeHeaderToId('One (more)')).toBe('one');
    });
    it('should ignore html content', function() {
      expect(normalizeHeaderToId('Section ')).toBe('section');
    });
    it('should ignore special characters', function() {
      expect(normalizeHeaderToId('Section \'!?')).toBe('section');
    });
    it('should ignore html entities', function() {
      expect(normalizeHeaderToId('angular's-jqlite')).toBe('angulars-jqlite');
    });
  });
});