Skip to content

Commit

Permalink
refactor(test-theme_processors-config): async/await & destructure (he…
Browse files Browse the repository at this point in the history
…xojs#4040)

* refactor(test-theme_processors): async/await
* refactor(test-theme_processors): destructure
  • Loading branch information
curbengh authored and Thomas Parisot committed Jan 17, 2020
1 parent b920b74 commit 790d3ba
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions test/scripts/theme_processors/config.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
'use strict';

const sinon = require('sinon');
const pathFn = require('path');
const fs = require('hexo-fs');
const { spy } = require('sinon');
const { join } = require('path');
const { mkdirs, rmdir, unlink, writeFile} = require('hexo-fs');
const Promise = require('bluebird');

describe('config', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(pathFn.join(__dirname, 'config_test'), {silent: true});
const hexo = new Hexo(join(__dirname, 'config_test'), {silent: true});
const processor = require('../../../lib/theme/processors/config');
const process = Promise.method(processor.process.bind(hexo));
const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test');
const themeDir = join(hexo.base_dir, 'themes', 'test');

function newFile(options) {
options.source = pathFn.join(themeDir, options.path);
options.source = join(themeDir, options.path);
return new hexo.theme.File(options);
}

before(() => Promise.all([
fs.mkdirs(themeDir),
fs.writeFile(hexo.config_path, 'theme: test')
]).then(() => hexo.init()));
before(async () => {
await Promise.all([
mkdirs(themeDir),
writeFile(hexo.config_path, 'theme: test')
]);
hexo.init();
});

beforeEach(() => { hexo.theme.config = {}; });

after(() => fs.rmdir(hexo.base_dir));
after(() => rmdir(hexo.base_dir));

it('pattern', () => {
const pattern = processor.pattern;
Expand All @@ -33,7 +38,7 @@ describe('config', () => {
should.not.exist(pattern.match('foo.yml'));
});

it('type: create', () => {
it('type: create', async () => {
const body = [
'name:',
' first: John',
Expand All @@ -46,27 +51,25 @@ describe('config', () => {
content: body
});

return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
hexo.theme.config.should.eql({
name: {first: 'John', last: 'Doe'}
});
}).finally(() => {
hexo.theme.config = {};
return fs.unlink(file.source);
await writeFile(file.source, body);
await process(file);
hexo.theme.config.should.eql({
name: {first: 'John', last: 'Doe'}
});

unlink(file.source);
});

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

hexo.theme.config = {foo: 'bar'};

return process(file).then(() => {
hexo.theme.config.should.eql({});
});
await process(file);
hexo.theme.config.should.eql({});
});

it('load failed', () => {
Expand All @@ -75,7 +78,7 @@ describe('config', () => {
type: 'create'
});

const errorCallback = sinon.spy(err => {
const errorCallback = spy(err => {
err.should.have.property('message', 'Theme config load failed.');
});

Expand Down

0 comments on commit 790d3ba

Please sign in to comment.