/* Copyright 2012 the JSDoc Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* global jsdoc */ describe('@jsdoc/core/lib/plugins', () => { const path = require('path'); const plugins = require('../../../lib/plugins'); it('has an `installPlugins` method', () => { expect(plugins.installPlugins).toBeFunction(); }); describe('installPlugins', () => { let eventCounts; let parser; const events = [ 'parseBegin', 'fileBegin', 'beforeParse', 'jsdocCommentFound', 'symbolFound', 'newDoclet', 'fileComplete', 'parseComplete', 'processingComplete', ]; function countEvents(emitter) { events.forEach((eventName) => { emitter.on(eventName, () => eventCounts[eventName]++); }); } beforeEach(() => { eventCounts = {}; events.forEach((eventName) => (eventCounts[eventName] = 0)); parser = jsdoc.createParser(); }); it('adds event handlers to the parser', () => { let pluginPath = path.resolve(__dirname, '../../fixtures/plugin-test-handlers.js'); const plugin = require(pluginPath); plugins.installPlugins([pluginPath], parser, jsdoc.deps); countEvents(plugin); jsdoc.getDocSetFromFile( path.resolve(__dirname, '../../fixtures/plugin-source-file.js'), parser ); // The `processingComplete` event is triggered by code that wouldn't normally be called during // this test, so we trigger the event manually. parser.fireProcessingComplete(); // Confirm that each type of event was emitted at least once. events.forEach((eventName) => { expect(eventCounts[eventName]).toBeGreaterThanOrEqual(1); }); }); it('adds AST node visitors to the parser', () => { const nodes = []; let pluginPath = path.resolve(__dirname, '../../fixtures/plugin-test-ast-visitor.js'); const plugin = require(pluginPath); plugins.installPlugins([pluginPath], parser, jsdoc.deps); plugin.on('visitNode', (node) => nodes.push(node)); jsdoc.getDocSetFromFile( path.resolve(__dirname, '../../fixtures/plugin-source-file.js'), parser ); expect(nodes.length).toBe(1); expect(nodes[0].init.value).toBe('bar'); }); it('adds tags to the dictionary', () => { const doclets = []; let pluginPath = path.resolve(__dirname, '../../fixtures/plugin-test-tags.js'); plugins.installPlugins([pluginPath], parser, jsdoc.deps); parser.on('newDoclet', (e) => { if (e.doclet.longname === 'test') { doclets.push(e.doclet); } }); jsdoc.getDocSetFromFile( path.resolve(__dirname, '../../fixtures/plugin-source-file.js'), parser ); expect(doclets.length).toBe(1); expect(doclets[0].foo).toBeTrue(); }); }); });