Skip to content

Commit

Permalink
fix: various reflection path resolution fixes, better test code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
GerkinDev committed Mar 3, 2022
1 parent e2a2a13 commit 314f173
Show file tree
Hide file tree
Showing 44 changed files with 1,255 additions and 284 deletions.
13 changes: 13 additions & 0 deletions packages/plugin-code-blocks/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { JSDOM } from 'jsdom';

export const formatExpanded = ( file: string, content: string ) => `<details class="code-block" open=""><summary><p>From ${file}</p></summary>${content}\n</details>`;
export const checkDef = ( dom: JSDOM, id: string, codeBlock: string ) => {
const link = dom.window.document.getElementById( id );
expect( link ).toBeTruthy();
const section = link!.parentElement;
expect( section ).toBeTruthy();
const blocks = section!.querySelectorAll( '.code-block' );
expect( blocks ).toHaveLength( 1 );
const block = blocks[0]!;
expect( block.outerHTML ).toEqual( codeBlock );
};

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

42 changes: 42 additions & 0 deletions packages/plugin-code-blocks/__tests__/integration/monorepo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { readFile } from 'fs/promises';
import { resolve } from 'path';

import { JSDOM } from 'jsdom';

import { formatHtml, runPlugin } from '@knodes/typedoc-plugintestbed';

import { checkDef, formatExpanded } from '../helpers';

const rootDir = resolve( __dirname, '../mock-fs/monorepo' );
jest.setTimeout( 30000 );
beforeEach( () => {
process.chdir( rootDir );
} );
describe( 'Real behavior', () => {
it( 'should render correctly', async () => {
await runPlugin( rootDir, resolve( __dirname, '../../src/index' ) );
const testJson = '<pre><code class="language-json">'+
'<span class="hl-0">{</span><span class="hl-1">"Hello"</span><span class="hl-0">: </span><span class="hl-2">"World"</span><span class="hl-0">}</span>\n'+
'</code></pre>';
const testJsonA = testJson.replace( 'Hello', 'pkg' ).replace( 'World', 'a' );
const testJsonB = testJson.replace( 'Hello', 'pkg' ).replace( 'World', 'b' );

const pkgA = await readFile( resolve( rootDir, 'docs/modules/pkg_a.html' ), 'utf-8' );
const domA = new JSDOM( pkgA );
checkDef( domA, 'testInProjA', formatExpanded( './packages/a/blocks/test.json', testJsonA ) );
checkDef( domA, 'testInProjB', formatExpanded( './packages/b/blocks/test.json', testJsonB ) );
checkDef( domA, 'testNoPrefixImplicitInBlocks', formatExpanded( './packages/a/blocks/test.json', testJsonA ) );
checkDef( domA, 'testNoPrefixInBlocks', formatExpanded( './packages/a/blocks/test.json', testJsonA ) );
expect( pkgA ).toMatch( /<link\s+rel="stylesheet"\s+href="([^"]*?\/)?assets\/code-blocks\.css"\s*\/>/ );
expect( formatHtml( pkgA ) ).toMatchSnapshot();

const pkgB = await readFile( resolve( rootDir, 'docs/modules/pkg_b.html' ), 'utf-8' );
const domB = new JSDOM( pkgB );
checkDef( domB, 'testInProjA', formatExpanded( './packages/a/blocks/test.json', testJsonA ) );
checkDef( domB, 'testInProjB', formatExpanded( './packages/b/blocks/test.json', testJsonB ) );
checkDef( domB, 'testNoPrefixImplicitInBlocks', formatExpanded( './packages/b/blocks/test.json', testJsonB ) );
checkDef( domB, 'testNoPrefixInBlocks', formatExpanded( './packages/b/blocks/test.json', testJsonB ) );
expect( pkgB ).toMatch( /<link\s+rel="stylesheet"\s+href="([^"]*?\/)?assets\/code-blocks\.css"\s*\/>/ );
expect( formatHtml( pkgB ) ).toMatchSnapshot();
} );
} );
32 changes: 32 additions & 0 deletions packages/plugin-code-blocks/__tests__/integration/simple.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { readFile } from 'fs/promises';
import { resolve } from 'path';

import { JSDOM } from 'jsdom';

import { formatHtml, runPlugin } from '@knodes/typedoc-plugintestbed';

import { checkDef, formatExpanded } from '../helpers';

const rootDir = resolve( __dirname, '../mock-fs/simple' );
jest.setTimeout( 30000 );
beforeEach( () => {
process.chdir( rootDir );
} );
describe( 'Real behavior', () => {
it( 'should render correctly', async () => {
await runPlugin( rootDir, resolve( __dirname, '../../src/index' ) );
const c = await readFile( resolve( rootDir, 'docs/modules.html' ), 'utf-8' );
const dom = new JSDOM( c );
const testJson = '<pre><code class="language-json">'+
'<span class="hl-0">{</span><span class="hl-1">"Hello"</span><span class="hl-0">: </span><span class="hl-2">"World"</span><span class="hl-0">}</span>\n'+
'</code></pre>';
const testJsonFooBar = testJson.replace( 'Hello', 'Foo' ).replace( 'World', 'Bar' );
checkDef( dom, 'testProjImplicitInBlocks', formatExpanded( './blocks/test.json', testJson ) );
checkDef( dom, 'testProjInBlocks', formatExpanded( './blocks/test.json', testJson ) );
checkDef( dom, 'testNoPrefixImplicitInBlocks', formatExpanded( './blocks/test.json', testJson ) );
checkDef( dom, 'testNoPrefixInBlocks', formatExpanded( './blocks/test.json', testJson ) );
checkDef( dom, 'testRel', formatExpanded( './src/test.json', testJsonFooBar ) );
expect( c ).toMatch( /<link\s+rel="stylesheet"\s+href="([^"]*?\/)?assets\/code-blocks\.css"\s*\/>/ );
expect( formatHtml( c ) ).toMatchSnapshot();
} );
} );
37 changes: 0 additions & 37 deletions packages/plugin-code-blocks/__tests__/integration/test.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pkg": "a"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-a",
"typedocMain": "./src"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const stub = () => 1;

// #region inExplicitPackage
/**
* A test code block targetting project A
*
* {@codeblock ~pkg-a/test.json}
*/
export const testInProjA = stub;

/**
* A test code block targetting project B
*
* {@codeblock ~pkg-b/test.json}
*/
export const testInProjB = stub;
// #endregion

// #region inPackage
/**
* A test code block for unprefixed path implicitly in blocks directory
*
* {@codeblock test.json}
*/
export const testNoPrefixImplicitInBlocks = stub;

/**
* A test code block for unprefixed path in blocks directory
*
* {@codeblock blocks/test.json}
*/
export const testNoPrefixInBlocks = stub;
// #endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pkg": "b"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pkg-b",
"typedocMain": "./src"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const stub = () => 1;

// #region inExplicitPackage
/**
* A test code block targetting project A
*
* {@codeblock ~pkg-a/test.json}
*/
export const testInProjA = stub;

/**
* A test code block targetting project B
*
* {@codeblock ~pkg-b/test.json}
*/
export const testInProjB = stub;
// #endregion

// #region inPackage
/**
* A test code block for unprefixed path implicitly in blocks directory
*
* {@codeblock test.json}
*/
export const testNoPrefixImplicitInBlocks = stub;

/**
* A test code block for unprefixed path in blocks directory
*
* {@codeblock blocks/test.json}
*/
export const testNoPrefixInBlocks = stub;
// #endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS"
},
"include": ["**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS"
},
"include": ["**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
'entryPoints': [
'packages/*',
],
'entryPointStrategy': 'packages',
'pluginCodeBlocks:source': 'blocks',
};
Empty file.
Loading

0 comments on commit 314f173

Please sign in to comment.