|
| 1 | +/* eslint-disable no-global-assign */ |
| 2 | +require = require('esm')(module /* , options */); |
| 3 | + |
| 4 | +const path = require('path'); |
| 5 | +const { expect } = require('chai'); |
| 6 | +const { initJSDOM } = require('../_helper'); |
| 7 | + |
| 8 | +const docsifySite = path.join(__dirname, '..', '..', 'docs'); |
| 9 | + |
| 10 | +const markup = /* html */ `<!DOCTYPE html> |
| 11 | + <html> |
| 12 | + <head></head> |
| 13 | + <body> |
| 14 | + <div id="app"></div> |
| 15 | + </body> |
| 16 | + </html> |
| 17 | +`; |
| 18 | + |
| 19 | +describe('Docsify public API', () => { |
| 20 | + it('is available', async () => { |
| 21 | + const DOM = initJSDOM(markup); |
| 22 | + DOM.reconfigure({ url: 'file:///' + docsifySite }); |
| 23 | + |
| 24 | + const { documentReady } = require('../../src/core/index'); |
| 25 | + await new Promise(resolve => documentReady(resolve)); |
| 26 | + |
| 27 | + expect(typeof window.Docsify).to.equal('object'); |
| 28 | + expect(typeof window.Docsify.util).to.equal('object'); |
| 29 | + expect(typeof window.Docsify.dom).to.equal('object'); |
| 30 | + expect(typeof window.Docsify.get).to.equal('function'); |
| 31 | + expect(typeof window.Docsify.slugify).to.equal('function'); |
| 32 | + expect(typeof window.Docsify.version).to.equal('string'); |
| 33 | + |
| 34 | + expect(window.DocsifyCompiler).to.be.an.instanceof(Function); |
| 35 | + expect(window.marked).to.be.an.instanceof(Function); |
| 36 | + expect(typeof window.Prism).to.equal('object'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('Docsify config function', function() { |
| 41 | + it('allows $docsify to be a function', async function() { |
| 42 | + const DOM = initJSDOM(markup); |
| 43 | + DOM.reconfigure({ url: 'file:///' + docsifySite }); |
| 44 | + |
| 45 | + window.configFunctionCalled = false; |
| 46 | + |
| 47 | + window.$docsify = function(vm) { |
| 48 | + // Check public API (that which is available at this point) |
| 49 | + expect(vm).to.be.an.instanceof(Object); |
| 50 | + expect(vm.constructor.name).to.equal('Docsify'); |
| 51 | + expect(vm.$fetch).to.be.an.instanceof(Function); |
| 52 | + expect(vm.$resetEvents).to.be.an.instanceof(Function); |
| 53 | + expect(vm.route).to.be.an.instanceof(Object); |
| 54 | + |
| 55 | + window.configFunctionCalled = true; |
| 56 | + |
| 57 | + return {}; |
| 58 | + }; |
| 59 | + |
| 60 | + const { documentReady, Docsify } = require('../../src/core/index'); |
| 61 | + await new Promise(resolve => documentReady(resolve)); |
| 62 | + |
| 63 | + new Docsify(); // eslint-disable-line |
| 64 | + |
| 65 | + expect(window.configFunctionCalled).to.equal(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('provides the hooks and vm API to plugins', async function() { |
| 69 | + const DOM = initJSDOM(markup); |
| 70 | + DOM.reconfigure({ url: 'file:///' + docsifySite }); |
| 71 | + |
| 72 | + window.pluginFunctionCalled = false; |
| 73 | + |
| 74 | + window.$docsify = function(vm) { |
| 75 | + const vm1 = vm; |
| 76 | + return { |
| 77 | + plugins: [ |
| 78 | + function(hook, vm2) { |
| 79 | + expect(vm1).to.equal(vm2); |
| 80 | + |
| 81 | + expect(hook.init).to.be.an.instanceof(Function); |
| 82 | + expect(hook.beforeEach).to.be.an.instanceof(Function); |
| 83 | + expect(hook.afterEach).to.be.an.instanceof(Function); |
| 84 | + expect(hook.doneEach).to.be.an.instanceof(Function); |
| 85 | + expect(hook.mounted).to.be.an.instanceof(Function); |
| 86 | + expect(hook.ready).to.be.an.instanceof(Function); |
| 87 | + |
| 88 | + window.pluginFunctionCalled = true; |
| 89 | + }, |
| 90 | + ], |
| 91 | + }; |
| 92 | + }; |
| 93 | + |
| 94 | + const { documentReady, Docsify } = require('../../src/core/index'); |
| 95 | + await new Promise(resolve => documentReady(resolve)); |
| 96 | + |
| 97 | + new Docsify(); // eslint-disable-line |
| 98 | + |
| 99 | + expect(window.pluginFunctionCalled).to.equal(true); |
| 100 | + }); |
| 101 | +}); |
0 commit comments