var createTestPackage = require('../../helpers/test-package');
var Dgeni = require('dgeni');
describe('autoLinkCode post-processor', () => {
let processor, autoLinkCode, aliasMap, filterPipes;
beforeEach(() => {
const testPackage = createTestPackage('angular-base-package');
const dgeni = new Dgeni([testPackage]);
const injector = dgeni.configureInjector();
autoLinkCode = injector.get('autoLinkCode');
autoLinkCode.docTypes = ['class', 'pipe', 'function', 'const', 'member'];
aliasMap = injector.get('aliasMap');
processor = injector.get('postProcessHtml');
processor.docTypes = ['test-doc'];
processor.plugins = [autoLinkCode];
filterPipes = injector.get('filterPipes');
});
it('should insert an anchor into every code item that matches the id of an API doc', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass
');
});
it('should insert an anchor into every code item that matches an alias of an API doc', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass', 'foo.MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'foo.MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('foo.MyClass
');
});
it('should match code items within a block of code that contain a dot in their identifier', () => {
aliasMap.addDoc({ docType: 'member', id: 'MyEnum.Value', aliases: ['Value', 'MyEnum.Value'], path: 'a/b/myenum' });
const doc = { docType: 'test-doc', renderedContent: 'someFn(): MyEnum.Value
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('someFn(): MyEnum.Value
');
});
it('should ignore code items that do not match a link to an API doc', () => {
aliasMap.addDoc({ docType: 'guide', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass
');
});
it('should ignore code items that are already inside a link', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass
');
});
it('should ignore code items match an API doc but are not in the list of acceptable docTypes', () => {
aliasMap.addDoc({ docType: 'directive', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass
');
});
it('should ignore code items that match an API doc but are attached to other text via a dash', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'xyz-MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('xyz-MyClass
');
});
it('should ignore code items that are filtered out by custom filters', () => {
autoLinkCode.customFilters = [filterPipes];
aliasMap.addDoc({ docType: 'pipe', id: 'MyClass', aliases: ['MyClass', 'myClass'], path: 'a/b/myclass', pipeOptions: { name: '\'myClass\'' } });
const doc = { docType: 'test-doc', renderedContent: '{ xyz | myClass } { xyz|myClass } MyClass myClass OtherClass|MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('' +
'{ xyz | myClass } ' +
'{ xyz|myClass } ' +
'MyClass ' +
'myClass OtherClass|MyClass' +
'
');
});
it('should ignore code items that match an internal API doc', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass', internal: true });
const doc = { docType: 'test-doc', renderedContent: 'MyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass
');
});
it('should insert anchors for individual text nodes within a code block', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClassMyClass
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClassMyClass
');
});
it('should insert anchors for words that match within text nodes in a code block', () => {
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
aliasMap.addDoc({ docType: 'function', id: 'myFunc', aliases: ['myFunc'], path: 'ng/myfunc' });
aliasMap.addDoc({ docType: 'const', id: 'MY_CONST', aliases: ['MY_CONST'], path: 'ng/my_const' });
const doc = { docType: 'test-doc', renderedContent: 'myFunc() {\n return new MyClass(MY_CONST);\n}
' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('myFunc() {\n return new MyClass(MY_CONST);\n}
');
});
it('should work with custom elements', () => {
autoLinkCode.codeElements = ['code-example'];
aliasMap.addDoc({ docType: 'class', id: 'MyClass', aliases: ['MyClass'], path: 'a/b/myclass' });
const doc = { docType: 'test-doc', renderedContent: 'MyClass' };
processor.$process([doc]);
expect(doc.renderedContent).toEqual('MyClass');
});
});