Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for analyzer (starting with handleJsDoc) #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/analyzer/test-helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import ts from 'typescript';

/**
* Helper function that returns categorized Nodes based on criteria
* @param {string} file
* @param {{name: string, fn: () => boolean}[]} criteria
* @example
* ```js
* const foundNodes = getNodesByCriteria(file, [
* { name: 'jsDoc', fn: (node) => Boolean(node.jsDoc) },
* ]);
* const firstJsDocNode = foundNodes?.jsDoc?.[0];
* ```
* @returns {{[categoryName:string]: Node[]}}
*/
export function getNodesByCriteria(file, criteria) {
const node = ts.createSourceFile('test.js', file, ts.ScriptTarget.ES2015, true);
const foundNodes = {};
(function find(node) {
criteria.forEach((cObj) => {
if (cObj.fn(node)) {
foundNodes[cObj.name] = [...(foundNodes[cObj.name] || []), node];
}
});
ts.forEachChild(node, find);
})(node);
return foundNodes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe } from '@asdgf/cli';
import * as assert from 'uvu/assert';
import { handleJsDoc } from '../../../../src/features/analyse-phase/creators/handlers.js';
import { getNodesByCriteria } from '../../../../test-helpers/index.js';

/**
* Helper function that finds first jsdoc node of file and runs handleJsDoc to get the result that
* would be put in cem.
* @param {string} file
*/
function getJsDocOutputFromFile(file) {
const foundNodes = getNodesByCriteria(file, [
{ name: 'jsDoc', fn: (node) => Boolean(node.jsDoc) },
]);
const jsDocNode = foundNodes?.jsDoc?.[0];
return handleJsDoc({}, jsDocNode);
}

const createFile = (/** @type {string} */ jsdocLines) => `
class X extends HTMLElement {
/**
${jsdocLines}
*/
callMe() {
return 'thanks';
}
}`;

function getJsDocOutputFromFragment(fragment) {
return getJsDocOutputFromFile(createFile(fragment));
}

describe('handleJsDoc', ({ it }) => {
describe('Parameters', () => {
it("creates a 'parameters' key (of type {name: string, type: {text: string}})", async () => {
const res1 = getJsDocOutputFromFragment('* @param {string} x');
assert.equal(res1.parameters, [
{
name: 'x',
type: {
text: 'string',
},
},
]);
const res2 = getJsDocOutputFromFragment('');
assert.equal(res2.parameters, undefined);
});

it('supports descriptions', async () => {
const result = getJsDocOutputFromFragment(`* @param {string} a text here`);

assert.equal(result.parameters, [
{
description: 'text here',
name: 'a',
type: {
text: 'string',
},
},
]);
});

it('recognizes multiple params', async () => {
const result = getJsDocOutputFromFragment(`
* @param {string} a
* @protected
* @param {number} b`);

assert.equal(result.parameters, [
{
name: 'a',
type: {
text: 'string',
},
},
{
name: 'b',
type: {
text: 'number',
},
},
]);
});

// TODO: implement this in code
it.skip('recognizes object params built from multiple `@param` tags', async () => {
const result = getJsDocOutputFromFragment(`
* @param {object} opts
* @param {string} [opts.currency]`);

assert.equal(result, parameters, [
{
name: 'opts',
type: {
text: '{currency?: string}',
},
},
]);
});
});

describe('Privacy', () => {
it("creates a 'privacy' key (of type 'public'|'protected'|'private')", async () => {
assert.equal(getJsDocOutputFromFragment('* @public').privacy, 'public');
assert.equal(getJsDocOutputFromFragment('* @protected').privacy, 'protected');
assert.equal(getJsDocOutputFromFragment('* @private').privacy, 'private');
});

it("creates no 'privacy' key when not explicitly declared via @public tag", async () => {
assert.equal(getJsDocOutputFromFragment('').privacy, undefined);
});
});
});