Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve coverage #4422

Merged
merged 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/plugins/console/migrate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { underline } = require('chalk');
const { underline, magenta } = require('chalk');

function migrateConsole(args) {
// Display help message if user didn't input any arguments
Expand All @@ -14,7 +14,7 @@ function migrateConsole(args) {
if (!migrators[type]) {
let help = '';

help += `${type.magenta} migrator plugin is not installed.\n\n`;
help += `${magenta(type)} migrator plugin is not installed.\n\n`;
help += 'Installed migrator plugins:\n';
help += ` ${Object.keys(migrators).join(', ')}\n\n`;
help += `For more help, you can check the online docs: ${underline('https://hexo.io/')}`;
Expand Down
29 changes: 26 additions & 3 deletions test/scripts/console/migrate.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
'use strict';

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

describe('migrate', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname, {silent: true});
const hexo = new Hexo(__dirname, { silent: true });
const migrate = require('../../../lib/plugins/console/migrate').bind(hexo);

it('default', async () => {
const migrator = spy();

hexo.extend.migrator.register('test', migrator);

await migrate({_: ['test'], foo: 1, bar: 2});
await migrate({ _: ['test'], foo: 1, bar: 2 });

sinonAssert.calledWithMatch(migrator, { foo: 1, bar: 2 });
migrator.calledOnce.should.be.true;
});

it('no args', async () => {
const hexo = new Hexo(__dirname, { silent: true });
hexo.call = spy();
const migrate = require('../../../lib/plugins/console/migrate').bind(hexo);

await migrate({ _: [] });
hexo.call.calledOnce.should.be.true;
hexo.call.args[0][0].should.eql('help');
hexo.call.args[0][1]._[0].should.eql('migrate');
});

it('migrator not found', async () => {
const logStub = stub(console, 'log');

await migrate({ _: ['foo'] });

logStub.calledOnce.should.be.true;
logStub.args[0][0].should.contains('migrator plugin is not installed.');
logStub.args[0][0].should.contains('Installed migrator plugins:');

logStub.restore();
});
});
14 changes: 13 additions & 1 deletion test/scripts/console/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { exists, mkdirs, readFile, rmdir, unlink } = require('hexo-fs');
const moment = require('moment');
const { join } = require('path');
const Promise = require('bluebird');
const { useFakeTimers } = require('sinon');
const { useFakeTimers, spy } = require('sinon');

describe('publish', () => {
const Hexo = require('../../../lib/hexo');
Expand Down Expand Up @@ -70,6 +70,18 @@ describe('publish', () => {
await unlink(path);
});

it('no args', async () => {
const hexo = new Hexo(join(__dirname, 'publish_test'), {silent: true});
hexo.call = spy();
const publish = require('../../../lib/plugins/console/publish').bind(hexo);

await publish({_: []});

hexo.call.calledOnce.should.be.true;
hexo.call.args[0][0].should.eql('help');
hexo.call.args[0][1]._[0].should.eql('publish');
});

it('layout', async () => {
const path = join(hexo.source_dir, '_posts', 'Hello-World.md');
const date = moment(now);
Expand Down
13 changes: 13 additions & 0 deletions test/scripts/console/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { mkdirs, readFile, rmdir, unlink, writeFile } = require('hexo-fs');
const { join } = require('path');
const Promise = require('bluebird');
const { spy } = require('sinon');

describe('render', () => {
const Hexo = require('../../../lib/hexo');
Expand All @@ -22,6 +23,18 @@ describe('render', () => {
' boo: 2'
].join('\n');

it('no args', async () => {
const hexo = new Hexo(join(__dirname, 'render_test'), {silent: true});
hexo.call = spy();
const render = require('../../../lib/plugins/console/render').bind(hexo);

await render({_: []});

hexo.call.calledOnce.should.be.true;
hexo.call.args[0][0].should.eql('help');
hexo.call.args[0][1]._.should.eql('render');
});

it('relative path', async () => {
const src = join(hexo.base_dir, 'test.yml');
const dest = join(hexo.base_dir, 'result.json');
Expand Down
14 changes: 11 additions & 3 deletions test/scripts/helpers/feed_tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ describe('feed_tag', () => {

const feed = require('../../../lib/plugins/helper/feed_tag').bind(ctx);

it('path', () => {
it('path - atom', () => {
feed('atom.xml').should.eql('<link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">');
});

it('path - rss', () => {
feed('rss2.xml').should.eql('<link rel="alternate" href="/rss2.xml" title="Hexo" type="application/rss+xml">');
});

it('title', () => {
feed('atom.xml', {title: 'RSS Feed'}).should.eql('<link rel="alternate" href="/atom.xml" title="RSS Feed" type="application/atom+xml">');
});
Expand All @@ -37,9 +41,13 @@ describe('feed_tag', () => {
});

it('invalid input - undefined', () => {
delete ctx.config.feed;
feed().should.eql('');
});

it('invalid input - empty', () => {
ctx.config.feed = {};
const result = feed();
result.should.eql('');
feed().should.eql('');
});

it('feed - parse argument if available', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/scripts/helpers/mail_to.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ describe('mail_to', () => {
mailto('abc@example.com').should.eql('<a href="mailto:abc@example.com" title="abc@example.com">abc@example.com</a>');
});

it('path - array', () => {
const emails = ['abc@example.com', 'foo@example.com'];
const emailsStr = 'abc@example.com,foo@example.com';
mailto(emails).should.eql(`<a href="mailto:${emailsStr}" title="${emailsStr}">${emailsStr}</a>`);
});

it('text', () => {
mailto('abc@example.com', 'Email').should.eql('<a href="mailto:abc@example.com" title="Email">Email</a>');
});
Expand Down
4 changes: 4 additions & 0 deletions test/scripts/helpers/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ describe('number_format', () => {
numberFormat(1234.567).should.eql('1,234.567');
});

it('int', () => {
numberFormat(1234567).should.eql('1,234,567');
});

it('precision', () => {
numberFormat(1234.567, {precision: false}).should.eql('1,234.567');
numberFormat(1234.567, {precision: 0}).should.eql('1,234');
Expand Down
10 changes: 10 additions & 0 deletions test/scripts/helpers/open_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,16 @@ describe('open_graph', () => {
result.should.have.string(meta({property: 'og:locale', content: 'es_CR'}));
});

it('og:locale - options.language (incorrect format)', () => {
const result = openGraph.call({
page: {},
config: hexo.config,
is_post: isPost
}, {language: 'foo-bar'});

result.should.have.string(meta({property: 'og:locale', content: undefined}));
});

it('og:locale - page.lang', () => {
const result = openGraph.call({
page: { lang: 'es-mx' },
Expand Down
3 changes: 3 additions & 0 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ describe('partial', () => {

partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar');
partial('test', {}, {cache: true}).should.eql('bar');

partial('test', {foo: 'baz'}, {cache: 'ash'}).should.eql('baz');
partial('test', {}, {cache: 'ash'}).should.eql('baz');
});

it('only', () => {
Expand Down
19 changes: 17 additions & 2 deletions test/scripts/helpers/search_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,34 @@ describe('search_form', () => {
+ '</form>');
});

it('button enabled', () => {
it('text - null', () => {
searchForm({text: null}).should.eql('<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form">'
+ '<input type="search" name="q" class="search-form-input">'
+ '<input type="hidden" name="sitesearch" value="https://hexo.io">'
+ '</form>');
});

it('button - true', () => {
searchForm({button: true, text: 'Find'}).should.eql('<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form">'
+ '<input type="search" name="q" class="search-form-input" placeholder="Find">'
+ '<button type="submit" class="search-form-submit">Find</button>'
+ '<input type="hidden" name="sitesearch" value="https://hexo.io">'
+ '</form>');
});

it('button text', () => {
it('button - string', () => {
searchForm({button: 'Go', text: 'Find'}).should.eql('<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form">'
+ '<input type="search" name="q" class="search-form-input" placeholder="Find">'
+ '<button type="submit" class="search-form-submit">Go</button>'
+ '<input type="hidden" name="sitesearch" value="https://hexo.io">'
+ '</form>');
});

it('button - ignore incorrect type', () => {
searchForm({button: {}, text: 'Find'}).should.eql('<form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form">'
+ '<input type="search" name="q" class="search-form-input" placeholder="Find">'
+ '<button type="submit" class="search-form-submit">Find</button>'
+ '<input type="hidden" name="sitesearch" value="https://hexo.io">'
+ '</form>');
});
});
12 changes: 12 additions & 0 deletions test/scripts/helpers/tagcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe('tagcloud', () => {
].join(' '));
});

it('no tags', async () => {
const hexo = new Hexo(__dirname);
await hexo.init();
hexo.locals.invalidate();
hexo.site = hexo.locals.toObject();
const tagcloud = require('../../../lib/plugins/helper/tagcloud').bind(hexo);

const result = tagcloud();

result.should.eql('');
});

it('specified collection', () => {
const result = tagcloud(Tag.find({
name: /bc/
Expand Down