Skip to content

Commit

Permalink
test: temporarily set ecmaversion
Browse files Browse the repository at this point in the history
  • Loading branch information
weyusi committed Jul 10, 2019
1 parent 077e375 commit 45a5c1d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "hexo",
"root": true
"root": true,
"parserOptions": {
"ecmaVersion": 2018
}
}
6 changes: 5 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ module.exports = function(locals) {
})
.sort(function(a, b) {
return b.updated - a.updated;
});
})
.map((post) => ({
...post,
permalink: post.permalink.replace('index.html', '')
}));

const xml = template(config).render({
config: config,
Expand Down
63 changes: 45 additions & 18 deletions test/index.js
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;
});
});
});

0 comments on commit 45a5c1d

Please sign in to comment.