hexo-yam/test/html.test.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

/* eslint-env jest */
'use strict'
const Hexo = require('hexo')
2020-09-05 11:22:36 +00:00
const { minify: htmlMinify } = require('html-minifier')
describe('html', () => {
const hexo = new Hexo(__dirname)
const h = require('../lib/filter').minifyHtml.bind(hexo)
const input = '<p id="">foo</p>'
const path = 'index.html'
const defaultCfg = {
html: {
enable: true,
verbose: false,
exclude: [],
collapseBooleanAttributes: true,
collapseWhitespace: true,
ignoreCustomComments: [/^\s*more/],
removeComments: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}
}
2020-09-05 11:22:36 +00:00
const expected = htmlMinify(input, defaultCfg.html)
beforeEach(() => {
hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg))
})
test('default', () => {
const result = h(input, { path })
expect(result).toBe(expected)
})
test('disable', () => {
hexo.config.minify.html.enable = false
const result = h(input, { path })
expect(result).toBeUndefined()
})
test('empty file', () => {
const result = h('', { path })
expect(result).toBeUndefined()
})
test('option', () => {
const customOpt = { removeEmptyAttributes: false }
hexo.config.minify.html = customOpt
const result = h(input, { path })
2020-09-05 11:22:36 +00:00
const expected = htmlMinify(input, customOpt)
expect(result).toBe(input)
expect(result).toBe(expected)
})
test('option - verbose', () => {
hexo.config.minify.html.verbose = true
hexo.log.log = jest.fn()
h(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`html: ${path}`)
})
test('exclude', () => {
const exclude = '*.min.html'
hexo.config.minify.html.exclude = exclude
const result = h(input, { path: 'foo/bar.min.html' })
expect(result).toBe(input)
})
test('exclude - slash in pattern', () => {
const exclude = '**/lectus/**/*.html'
hexo.config.minify.html.exclude = exclude
const result = h(input, { path: 'eleifend/lectus/nullam/dapibus/netus.html' })
expect(result).toBe(input)
})
test('exclude - basename is true + slash', () => {
const exclude = ['**/lectus/**/*.html', 'bar.html']
const globOptions = { basename: true }
hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions
const result = h(input, { path: 'foo/bar.html' })
expect(result).toBe(input)
})
test('exclude - basename is false + slash', () => {
const exclude = ['**/lectus/**/*.html', 'bar.html']
const globOptions = { basename: false }
hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions
const result = h(input, { path: 'foo/bar.html' })
expect(result).toBe(expected)
})
test('null', () => {
hexo.config.minify.html.exclude = null
hexo.config.minify.html.globOptions = null
const result = h(input, { path: null })
expect(result).toBe(expected)
})
test('invalid string', () => {
const invalid = '<html><>?:"{}|_+</html>'
expect(() => {
h(invalid, { path })
2020-09-05 11:58:20 +00:00
}).toThrow(`Path: ${path}\nError: Parse Error`)
})
})