-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"extends": "hexo", | ||
"root": true | ||
"root": true, | ||
"parserOptions": { | ||
"ecmaVersion": 2018 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,97 @@ | ||
'use strict'; | ||
|
||
const should = require('chai').should(); // eslint-disable-line | ||
const Hexo = require('hexo'); | ||
const cheerio = require('cheerio'); | ||
var should = require('chai').should(); // eslint-disable-line | ||
var Hexo = require('hexo'); | ||
var cheerio = require('cheerio'); | ||
|
||
describe('Sitemap generator', () => { | ||
const hexo = new Hexo(__dirname, {silent: true}); | ||
describe('Sitemap generator', function() { | ||
var hexo = new Hexo(__dirname, {silent: true}); | ||
hexo.config.sitemap = { | ||
path: 'sitemap.xml' | ||
}; | ||
const Post = hexo.model('Post'); | ||
const Page = hexo.model('Page'); | ||
const generator = require('../lib/generator').bind(hexo); | ||
const sitemapTmpl = require('../lib/template')(hexo.config); | ||
let posts = {}; | ||
let pages = {}; | ||
let locals = {}; | ||
|
||
before(() => { | ||
return hexo.init().then(() => { | ||
return Post.insert([ | ||
{source: 'foo', slug: 'foo', updated: 1e8}, | ||
{source: 'bar', slug: 'bar', updated: 1e8 + 1}, | ||
{source: 'baz', slug: 'baz', updated: 1e8 - 1} | ||
]).then(data => { | ||
{source: 'bar', slug: 'bar', updated: 1e8 - 1}, | ||
{source: 'baz', slug: 'baz', updated: 1e8 - 2} | ||
]).then(() => { | ||
return Page.insert([ | ||
{source: 'bio/index.md', path: 'bio/', updated: 1e8 - 3}, | ||
{source: 'about/index.md', path: 'about/', updated: 1e8 - 4} | ||
]); | ||
}).then(() => { | ||
posts = Post.sort('-updated'); | ||
pages = Page.sort('-update'); | ||
locals = hexo.locals.toObject(); | ||
}); | ||
}); | ||
}); | ||
|
||
// Check the whole sitemap.xml | ||
it('default', () => { | ||
const result = generator(locals); | ||
|
||
result.path.should.eql('sitemap.xml'); | ||
result.data.should.eql(sitemapTmpl.render({ | ||
config: hexo.config, | ||
posts: posts.toArray() | ||
posts: posts.toArray().concat(pages.toArray()) | ||
})); | ||
}); | ||
|
||
// Check each element in sitemap.xml | ||
it('Each element', () => { | ||
const result = generator(locals); | ||
|
||
const $ = cheerio.load(result.data); | ||
const allPosts = Object.assign({}, posts.data.concat(pages.data)); | ||
|
||
$('url').each((index, element) => { | ||
$(element).children('loc').text().should.eql(allPosts[index].permalink); | ||
$(element).children('lastmod').text().should.eql(allPosts[index].updated.toISOString()); | ||
}); | ||
}); | ||
|
||
// urls should not ends with 'index.html' | ||
it('Canonical url', () => { | ||
const result = generator(locals); | ||
|
||
const $ = cheerio.load(result.data); | ||
|
||
$('url').each((index, element) => { | ||
$(element).children('loc').text().should.eql(posts.eq(index).permalink); | ||
$(element).children('lastmod').text().should.eql(posts.eq(index).updated.toISOString()); | ||
$(element).children('loc').text().endsWith('index.html').should.be.false; | ||
}); | ||
}); | ||
|
||
describe('skip_render', () => { | ||
it('array', () => { | ||
describe('skip_render', function() { | ||
it('array', function() { | ||
hexo.config.skip_render = ['foo']; | ||
|
||
const result = generator(locals); | ||
var result = generator(locals); | ||
result.data.should.not.contain('foo'); | ||
}); | ||
|
||
it('string', () => { | ||
it('string', function() { | ||
hexo.config.skip_render = 'bar'; | ||
|
||
const result = generator(locals); | ||
var result = generator(locals); | ||
result.data.should.not.contain('bar'); | ||
}); | ||
|
||
it('off', () => { | ||
it('off', function() { | ||
hexo.config.skip_render = null; | ||
|
||
const result = generator(locals); | ||
var result = generator(locals); | ||
result.should.be.ok; | ||
}); | ||
}); | ||
}); | ||
|