import { stripIndent } from 'common-tags';
import docsifyInit from '../helpers/docsify-init.js';
import { waitForText } from '../helpers/wait-for.js';
// Suite
// -----------------------------------------------------------------------------
describe('render', function () {
// Helpers
// ---------------------------------------------------------------------------
describe('callouts', () => {
beforeEach(async () => {
await docsifyInit();
});
test('caution', () => {
const output = window.marked('> [!CAUTION]\n> Text');
expect(output).toMatchInlineSnapshot(`
"
"
`);
});
test('important', () => {
const output = window.marked('> [!IMPORTANT]\n> Text');
expect(output).toMatchInlineSnapshot(`
""
`);
});
test('note', () => {
const output = window.marked('> [!NOTE]\n> Text');
expect(output).toMatchInlineSnapshot(`
""
`);
});
test('tip', () => {
const output = window.marked('> [!TIP]\n> Text');
expect(output).toMatchInlineSnapshot(`
""
`);
});
test('warning', () => {
const output = window.marked('> [!WARNING]\n> Text');
expect(output).toMatchInlineSnapshot(`
""
`);
});
test('important (legacy)', () => {
const output = window.marked('!> Important content');
expect(output).toMatchInlineSnapshot(
`"Important content
"`,
);
});
test('tip (legacy)', () => {
const output = window.marked('?> General tip');
expect(output).toMatchInlineSnapshot(
`"General tip
"`,
);
});
});
// Lists
// ---------------------------------------------------------------------------
describe('lists', function () {
beforeEach(async () => {
await docsifyInit();
});
test('as unordered task list', async function () {
const output = window.marked(stripIndent`
- [x] Task 1
- [ ] Task 2
- [ ] Task 3
`);
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('as ordered task list', async function () {
const output = window.marked(stripIndent`
1. [ ] Task 1
2. [x] Task 2
`);
expect(output).toMatchInlineSnapshot(
'" Task 1 Task 2 "',
);
});
test('normal unordered', async function () {
const output = window.marked(stripIndent`
- [linktext](link)
- just text
`);
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('unordered with custom start', async function () {
const output = window.marked(stripIndent`
1. first
2. second
text
3. third
`);
expect(output).toMatchInlineSnapshot(
'"first second text
third "',
);
});
test('nested', async function () {
const output = window.marked(stripIndent`
- 1
- 2
- 2 a
- 2 b
- 3
`);
expect(output).toMatchInlineSnapshot(
'""',
);
});
});
// Images
// ---------------------------------------------------------------------------
describe('images', function () {
beforeEach(async () => {
await docsifyInit();
});
test('regular', async function () {
const output = window.marked('');
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
test('class', async function () {
const output = window.marked(
"",
);
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
test('id', async function () {
const output = window.marked(
"",
);
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
test('no-zoom', async function () {
const output = window.marked("");
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
test('width and height', async function () {
const output = window.marked(
"",
);
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
test('width', async function () {
const output = window.marked("");
expect(output).toMatchInlineSnapshot(
'"
"',
);
});
});
// Headings
// ---------------------------------------------------------------------------
describe('headings', function () {
beforeEach(async () => {
await docsifyInit();
});
test('h1', async function () {
const output = window.marked('# h1 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('h2', async function () {
const output = window.marked('## h2 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('h3', async function () {
const output = window.marked('### h3 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('h4', async function () {
const output = window.marked('#### h4 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('h5', async function () {
const output = window.marked('##### h5 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
test('h6', async function () {
const output = window.marked('###### h6 tag');
expect(output).toMatchInlineSnapshot(
'""',
);
});
});
// Links
// ---------------------------------------------------------------------------
describe('link', function () {
beforeEach(async () => {
await docsifyInit();
});
test('regular', async function () {
const output = window.marked('[alt text](http://url)');
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('linkrel', async function () {
// const { docsify } = await init('default', {
// externalLinkTarget: '_blank',
// externalLinkRel: 'noopener',
// });
const output = window.marked('[alt text](http://www.example.com)');
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('disabled', async function () {
const output = window.marked("[alt text](http://url ':disabled')");
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('target for absolute path', async function () {
const output = window.marked("[alt text](http://url ':target=_self')");
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('target for relative path', async function () {
const output = window.marked("[alt text](/url ':target=_blank')");
expect(output).toMatchInlineSnapshot(
'"alt text
"',
);
});
test('class', async function () {
const output = window.marked(
"[alt text](http://url ':class=someCssClass')",
);
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('multi class config', async function () {
const output = window.marked(
"[alt text](http://url ':class=someCssClass :class=anotherCssClass')",
);
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
test('id', async function () {
const output = window.marked("[alt text](http://url ':id=someCssID')");
expect(output).toMatchInlineSnapshot(
`"alt text
"`,
);
});
});
// Skip Link
// ---------------------------------------------------------------------------
describe('skip link', () => {
test('renders default skip link and label', async () => {
await docsifyInit();
const elm = document.getElementById('skip-to-content');
const expectText = 'Skip to main content';
expect(elm.textContent).toBe(expectText);
expect(elm.outerHTML).toMatchInlineSnapshot(
`"Skip to main content "`,
);
});
test('renders custom label from config string', async () => {
const expectText = 'test';
await docsifyInit({
config: {
skipLink: expectText,
},
});
const elm = document.getElementById('skip-to-content');
expect(elm.textContent).toBe(expectText);
});
test('renders custom label from config object', async () => {
const getSkipLinkText = () =>
document.getElementById('skip-to-content').textContent;
await docsifyInit({
config: {
skipLink: {
'/dir1/dir2/': 'baz',
'/dir1/': 'bar',
},
},
});
window.location.hash = '/dir1/dir2/';
await waitForText('#skip-to-content', 'baz');
expect(getSkipLinkText()).toBe('baz');
window.location.hash = '/dir1/';
await waitForText('#skip-to-content', 'bar');
expect(getSkipLinkText()).toBe('bar');
// Fallback to default
window.location.hash = '';
await waitForText('#skip-to-content', 'Skip to main content');
expect(getSkipLinkText()).toBe('Skip to main content');
});
test('does not render skip link when false', async () => {
await docsifyInit({
config: {
skipLink: false,
},
});
const elm = document.getElementById('skip-to-content') || false;
expect(elm).toBe(false);
});
});
});