Skip to content

Commit ceee832

Browse files
mienNoahDragon
authored andcommitted
Increased unit test coverage (#2205)
* increased test coverage for console plugin * removed unwanted imports * fixed linting error
1 parent 5234c4a commit ceee832

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

test/scripts/console/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ describe('Console', function() {
99
require('./new');
1010
require('./publish');
1111
require('./render');
12+
require('./list_post');
13+
require('./list_categories');
14+
require('./list_tags');
15+
require('./list_page');
1216
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var Promise = require('bluebird');
4+
var sinon = require('sinon');
5+
var expect = require('chai').expect;
6+
7+
describe('Console list', function() {
8+
var Hexo = require('../../../lib/hexo');
9+
var hexo = new Hexo(__dirname);
10+
var Post = hexo.model('Post');
11+
12+
var listCategories = require('../../../lib/plugins/console/list/category').bind(hexo);
13+
14+
before(function() {
15+
var log = console.log;
16+
sinon.stub(console, 'log', function() {
17+
return log.apply(log, arguments);
18+
});
19+
});
20+
21+
after(function() {
22+
console.log.restore();
23+
});
24+
25+
it('no categories', function() {
26+
listCategories();
27+
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
28+
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
29+
expect(console.log.calledWith(sinon.match('No categories.'))).to.be.true;
30+
});
31+
32+
it('categories', function() {
33+
var posts = [
34+
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
35+
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
36+
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
37+
];
38+
return hexo.init()
39+
.then(function() {
40+
return Post.insert(posts);
41+
}).then(function(posts) {
42+
return Promise.each([
43+
['foo'],
44+
['baz'],
45+
['baz']
46+
], function(tags, i) {
47+
return posts[i].setCategories(tags);
48+
});
49+
}).then(function() {
50+
hexo.locals.invalidate();
51+
})
52+
.then(function() {
53+
listCategories();
54+
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
55+
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
56+
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
57+
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
58+
});
59+
});
60+
});

test/scripts/console/list_page.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var sinon = require('sinon');
4+
var expect = require('chai').expect;
5+
6+
describe('Console list', function() {
7+
var Hexo = require('../../../lib/hexo');
8+
var hexo = new Hexo();
9+
var Page = hexo.model('Page');
10+
var listPages = require('../../../lib/plugins/console/list/page').bind(hexo);
11+
12+
hexo.config.permalink = ':title/';
13+
before(function() {
14+
var log = console.log;
15+
sinon.stub(console, 'log', function() {
16+
return log.apply(log, arguments);
17+
});
18+
});
19+
20+
after(function() {
21+
console.log.restore();
22+
});
23+
24+
it('no page', function() {
25+
listPages();
26+
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
27+
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
28+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
29+
expect(console.log.calledWith(sinon.match('No pages.'))).to.be.true;
30+
});
31+
32+
it('page', function() {
33+
return Page.insert({
34+
source: 'foo',
35+
title: 'Hello World',
36+
path: 'bar'
37+
})
38+
.then(function() {
39+
listPages();
40+
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
41+
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
42+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
43+
expect(console.log.calledWith(sinon.match('Hello World'))).to.be.true;
44+
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
45+
});
46+
});
47+
});

test/scripts/console/list_post.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var sinon = require('sinon');
4+
var expect = require('chai').expect;
5+
6+
describe('Console list', function() {
7+
var Hexo = require('../../../lib/hexo');
8+
var hexo = new Hexo(__dirname);
9+
var Post = hexo.model('Post');
10+
11+
var listPosts = require('../../../lib/plugins/console/list/post').bind(hexo);
12+
13+
before(function() {
14+
var log = console.log;
15+
sinon.stub(console, 'log', function() {
16+
return log.apply(log, arguments);
17+
});
18+
});
19+
20+
after(function() {
21+
console.log.restore();
22+
});
23+
24+
it('no post', function() {
25+
listPosts();
26+
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
27+
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
28+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
29+
expect(console.log.calledWith(sinon.match('Category'))).to.be.true;
30+
expect(console.log.calledWith(sinon.match('Tags'))).to.be.true;
31+
expect(console.log.calledWith(sinon.match('No posts.'))).to.be.true;
32+
});
33+
34+
it('post', function() {
35+
var posts = [
36+
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
37+
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
38+
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
39+
];
40+
return hexo.init()
41+
.then(function() {
42+
return Post.insert(posts);
43+
}).then(function() {
44+
hexo.locals.invalidate();
45+
})
46+
.then(function() {
47+
listPosts();
48+
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
49+
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
50+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
51+
expect(console.log.calledWith(sinon.match('Category'))).to.be.true;
52+
expect(console.log.calledWith(sinon.match('Tags'))).to.be.true;
53+
for (var i = 0; i < posts.length; i++) {
54+
expect(console.log.calledWith(sinon.match(posts[i].source))).to.be.true;
55+
expect(console.log.calledWith(sinon.match(posts[i].slug))).to.be.true;
56+
expect(console.log.calledWith(sinon.match(posts[i].title))).to.be.true;
57+
}
58+
});
59+
});
60+
});

test/scripts/console/list_tags.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
var Promise = require('bluebird');
4+
var sinon = require('sinon');
5+
var expect = require('chai').expect;
6+
7+
describe('Console list', function() {
8+
var Hexo = require('../../../lib/hexo');
9+
var hexo = new Hexo(__dirname);
10+
var Post = hexo.model('Post');
11+
12+
var listTags = require('../../../lib/plugins/console/list/tag').bind(hexo);
13+
14+
hexo.config.permalink = ':title/';
15+
before(function() {
16+
var log = console.log;
17+
sinon.stub(console, 'log', function() {
18+
return log.apply(log, arguments);
19+
});
20+
});
21+
22+
after(function() {
23+
console.log.restore();
24+
});
25+
26+
it('no tags', function() {
27+
listTags();
28+
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
29+
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
30+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
31+
expect(console.log.calledWith(sinon.match('No tags.'))).to.be.true;
32+
});
33+
34+
it('tags', function() {
35+
var posts = [
36+
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
37+
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
38+
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
39+
];
40+
return hexo.init()
41+
.then(function() {
42+
return Post.insert(posts);
43+
}).then(function(posts) {
44+
return Promise.each([
45+
['foo'],
46+
['baz'],
47+
['baz']
48+
], function(tags, i) {
49+
return posts[i].setTags(tags);
50+
});
51+
}).then(function() {
52+
hexo.locals.invalidate();
53+
})
54+
.then(function() {
55+
listTags();
56+
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
57+
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
58+
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
59+
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
60+
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
61+
expect(console.log.calledWith(sinon.match('tags/baz'))).to.be.true;
62+
expect(console.log.calledWith(sinon.match('tags/foo'))).to.be.true;
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)