docsify/test/e2e/plugins.test.js
Joe Pea 62d756c447 refactor: convert to ES Modules and remove traces of CommonJS except in Rollup config because some dependencies are still CommonJS
BREAKING: The new project layout might break in some tooling setups.

We've added an exports field to `package.json` to specify where
statements like `import ... from 'docsify'` will import from, and left
the `main` and `unpkg` fields as-is for backwards compatibility with the
global <script> import method. Most people who use a non-module
`<script>` tag to import Docsify will not notice a difference. Anyone
else who is importing Docsify into a specilized build setup using
`import` statements has a chance of being broken, so we've marked this
as BREAKING.
2023-06-29 19:02:08 -07:00

156 lines
3.6 KiB
JavaScript

import docsifyInit from '../helpers/docsify-init.js';
import { test, expect } from './fixtures/docsify-init-fixture.js';
test.describe('Plugins', () => {
test('Hook order', async ({ page }) => {
const consoleMsgs = [];
const expectedMsgs = [
'init',
'mounted',
'beforeEach-async',
'beforeEach',
'afterEach-async',
'afterEach',
'doneEach',
'ready',
];
page.on('console', msg => consoleMsgs.push(msg.text()));
await docsifyInit({
config: {
plugins: [
function (hook, vm) {
hook.init(function () {
console.log('init');
});
hook.mounted(function () {
console.log('mounted');
});
hook.beforeEach(function (markdown, next) {
setTimeout(function () {
console.log('beforeEach-async');
next(markdown);
}, 100);
});
hook.beforeEach(function (markdown) {
console.log('beforeEach');
return markdown;
});
hook.afterEach(function (html, next) {
setTimeout(function () {
console.log('afterEach-async');
next(html);
}, 100);
});
hook.afterEach(function (html) {
console.log('afterEach');
return html;
});
hook.doneEach(function () {
console.log('doneEach');
});
hook.ready(function () {
console.log('ready');
});
},
],
},
markdown: {
homepage: '# Hello World',
},
// _logHTML: true,
});
expect(consoleMsgs).toEqual(expectedMsgs);
});
test('beforeEach() return value', async ({ page }) => {
await docsifyInit({
config: {
plugins: [
function (hook, vm) {
hook.beforeEach(function (markdown) {
return 'beforeEach';
});
},
],
},
// _logHTML: true,
});
await expect(page.locator('#main')).toContainText('beforeEach');
});
test('beforeEach() async return value', async ({ page }) => {
await docsifyInit({
config: {
plugins: [
function (hook, vm) {
hook.beforeEach(function (markdown, next) {
setTimeout(function () {
next('beforeEach');
}, 100);
});
},
],
},
markdown: {
homepage: '# Hello World',
},
// _logHTML: true,
});
await expect(page.locator('#main')).toContainText('beforeEach');
});
test('afterEach() return value', async ({ page }) => {
await docsifyInit({
config: {
plugins: [
function (hook, vm) {
hook.afterEach(function (html) {
return '<p>afterEach</p>';
});
},
],
},
markdown: {
homepage: '# Hello World',
},
// _logHTML: true,
});
await expect(page.locator('#main')).toContainText('afterEach');
});
test('afterEach() async return value', async ({ page }) => {
await docsifyInit({
config: {
plugins: [
function (hook, vm) {
hook.afterEach(function (html, next) {
setTimeout(function () {
next('<p>afterEach</p>');
}, 100);
});
},
],
},
markdown: {
homepage: '# Hello World',
},
// _logHTML: true,
});
await expect(page.locator('#main')).toContainText('afterEach');
});
});