Skip to content

Commit

Permalink
test: improve coverage (#5221)
Browse files Browse the repository at this point in the history
* test: improve coverage

* test(processors): improve coverage

* test(processors): improve coverage

* test(helpers): improve coverage

* test(console): improve coverage
  • Loading branch information
D-Sketon authored Jun 2, 2023
1 parent 5837bb8 commit 5ee3e6d
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 1 deletion.
14 changes: 14 additions & 0 deletions test/scripts/console/list_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ describe('Console list', () => {
sinonAssert.calledWithMatch(logStub, 'Hello World');
sinonAssert.calledWithMatch(logStub, 'foo');
});

it('page with unicode', async () => {
await Page.insert({
source: 'foo',
title: '\u0100',
path: 'bar'
});
listPages();
sinonAssert.calledWithMatch(logStub, 'Date');
sinonAssert.calledWithMatch(logStub, 'Title');
sinonAssert.calledWithMatch(logStub, 'Path');
sinonAssert.calledWithMatch(logStub, '\u0100');
sinonAssert.calledWithMatch(logStub, 'foo');
});
});
11 changes: 10 additions & 1 deletion test/scripts/console/list_post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const Promise = require('bluebird');
const { stub, assert: sinonAssert } = require('sinon');

describe('Console list', () => {
Expand Down Expand Up @@ -34,8 +35,15 @@ describe('Console list', () => {
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
];

const tags = [
['foo'],
['baz'],
['baz']
];

await hexo.init();
await Post.insert(posts);
const output = await Post.insert(posts);
await Promise.each(tags, (tags, i) => output[i].setTags(tags));
await hexo.locals.invalidate();

listPosts();
Expand All @@ -48,6 +56,7 @@ describe('Console list', () => {
sinonAssert.calledWithMatch(logStub, posts[i].source);
sinonAssert.calledWithMatch(logStub, posts[i].slug);
sinonAssert.calledWithMatch(logStub, posts[i].title);
sinonAssert.calledWithMatch(logStub, tags[i][0]);
}
});
});
10 changes: 10 additions & 0 deletions test/scripts/console/list_route.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ describe('Console list', () => {

listRoutes();
sinonAssert.calledWithMatch(logStub, 'Total: 1');
route.remove('test');
});

it('route with nodes', async () => {
route.set('test0/test1', 'foo');

listRoutes();
sinonAssert.calledWithMatch(logStub, 'Total: 1');
sinonAssert.calledWithMatch(logStub, '└─┬ test0');
sinonAssert.calledWithMatch(logStub, ' └── test1');
});
});
31 changes: 31 additions & 0 deletions test/scripts/console/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const moment = require('moment');
const { join } = require('path');
const Promise = require('bluebird');
const { useFakeTimers } = require('sinon');
const { spy } = require('sinon');

describe('new', () => {
const Hexo = require('../../../dist/hexo');
Expand Down Expand Up @@ -39,6 +40,16 @@ describe('new', () => {
return rmdir(hexo.base_dir);
});

it('no args', async () => {
hexo.call = spy();
await n({
_: []
});
hexo.call.calledOnce.should.be.true;
hexo.call.args[0][0].should.eql('help');
hexo.call.args[0][1]._[0].should.eql('new');
});

it('title', async () => {
const date = moment(now);
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
Expand Down Expand Up @@ -157,6 +168,26 @@ describe('new', () => {
await unlink(path);
});

it('without _', async () => {
const date = moment(now);
const path = join(hexo.source_dir, '_posts', 'bar.md');
const body = [
'title: bar',
'date: ' + date.format('YYYY-MM-DD HH:mm:ss'),
'tags:',
'---'
].join('\n') + '\n';

await n({
_: [],
path: 'bar'
});
const content = await readFile(path);
content.should.eql(body);

await unlink(path);
});

it('rename if target existed', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World-1.md');

Expand Down
18 changes: 18 additions & 0 deletions test/scripts/helpers/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,22 @@ describe('paginator', () => {
'<a class="extend next" rel="next" href="/page/2/">Next</a>'
].join(''));
});

it('force_prev_next - 2', () => {
const result = paginator({
current: 1,
prev_next: false,
force_prev_next: true
});

result.should.eql([
'<span class="extend prev" rel="prev">Prev</span>',
'<span class="page-number current">1</span>',
'<a class="page-number" href="/page/2/">2</a>',
'<a class="page-number" href="/page/3/">3</a>',
'<span class="space">&hellip;</span>',
'<a class="page-number" href="/page/10/">10</a>',
'<span class="extend next" rel="next">Next</span>'
].join(''));
});
});
43 changes: 43 additions & 0 deletions test/scripts/processors/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ describe('asset', () => {
should.not.exist(Asset.findById(id));
});

it('asset - type: delete - not exist', async () => {
const file = newFile({
path: 'foo.jpg',
type: 'delete',
renderable: false
});

const id = 'source/' + file.path;
await process(file);

should.not.exist(Asset.findById(id));
});

it('page - type: create', async () => {
const body = [
'title: "Hello world"',
Expand Down Expand Up @@ -249,6 +262,25 @@ describe('asset', () => {
]);
});

it('page - type: skip', async () => {
const file = newFile({
path: 'hello.njk',
type: 'skip',
renderable: true
});

await Page.insert({
source: file.path,
path: 'hello.html'
});
const page = Page.findOne({source: file.path});
await process(file);
should.exist(page);
await Promise.all([
page.remove()
]);
});

it('page - type: delete', async () => {
const file = newFile({
path: 'hello.njk',
Expand All @@ -264,6 +296,17 @@ describe('asset', () => {
should.not.exist(Page.findOne({ source: file.path }));
});

it('page - type: delete - not exist', async () => {
const file = newFile({
path: 'hello.njk',
type: 'delete',
renderable: true
});

await process(file);
should.not.exist(Page.findOne({ source: file.path }));
});

it('page - use the status of the source file if date not set', async () => {
const file = newFile({
path: 'hello.njk',
Expand Down
27 changes: 27 additions & 0 deletions test/scripts/processors/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ describe('data', () => {
unlink(file.source);
});

it('type: skip', async () => {
const file = newFile({
path: 'users.yml',
type: 'skip'
});

await Data.insert({
_id: 'users',
data: {foo: 'bar'}
});
const data = Data.findById('users');
await process(file);
should.exist(data);
data.remove();
});

it('type: delete', async () => {
const file = newFile({
path: 'users.yml',
Expand All @@ -141,4 +157,15 @@ describe('data', () => {
await process(file);
should.not.exist(Data.findById('users'));
});

it('type: delete - not exist', async () => {
const file = newFile({
path: 'users.yml',
type: 'delete'
});

await process(file);
should.not.exist(Data.findById('users'));
});

});
67 changes: 67 additions & 0 deletions test/scripts/processors/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,31 @@ describe('post', () => {
Post.removeById(postId);
});

it('asset - type: delete - not exist', async () => {
hexo.config.post_asset_folder = true;

const file = newFile({
path: 'foo/bar.jpg',
published: true,
type: 'delete',
renderable: false
});

const id = 'source/' + file.path;

const post = await Post.insert({
source: '_posts/foo.html',
slug: 'foo'
});
const postId = post._id;

await process(file);
should.not.exist(PostAsset.findById(id));

Post.removeById(postId);
});


it('asset - skip if can\'t find a matching post', async () => {
hexo.config.post_asset_folder = true;

Expand Down Expand Up @@ -358,6 +383,36 @@ describe('post', () => {
]);
});

it('post - type: skip', async () => {
const file = newFile({
path: 'foo.html',
published: true,
type: 'skip',
renderable: true
});

await Post.insert({
source: file.path,
slug: 'foo'
});
await process(file);
const post = Post.findOne({ source: file.path });
should.exist(post);
post.remove();
});

it('post - type: delete - not exist', async () => {
const file = newFile({
path: 'foo.html',
published: true,
type: 'delete',
renderable: true
});

await process(file);
should.not.exist(Post.findOne({ source: file.path }));
});

it('post - type: delete', async () => {
const file = newFile({
path: 'foo.html',
Expand All @@ -374,6 +429,18 @@ describe('post', () => {
should.not.exist(Post.findOne({ source: file.path }));
});

it('post - type: delete - not exist', async () => {
const file = newFile({
path: 'foo.html',
published: true,
type: 'delete',
renderable: true
});

await process(file);
should.not.exist(Post.findOne({ source: file.path }));
});

it('post - parse file name', async () => {
const body = [
'title: "Hello world"',
Expand Down
8 changes: 8 additions & 0 deletions test/scripts/renderers/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ describe('nunjucks', () => {
r({ text: forLoop }, data).should.eql('123');
});

it('toarray other case', () => {
const data = {
arr: 1
};

r({ text: forLoop }, data).should.eql('');
});

it('safedump undefined', () => {
const text = [
'{{ items | safedump }}'
Expand Down

0 comments on commit 5ee3e6d

Please sign in to comment.