Skip to content

Commit

Permalink
refactor(test-box-file): async/await (hexojs#3954)
Browse files Browse the repository at this point in the history
* refactor(test-box-file): async/await

* refactor(test-box-file): destructure

* refactor(test-box-file): arrow function

* chore(dev-deps): update eslint-config-hexo from 4.0 to 4.1

* style: asyncArrow
  • Loading branch information
curbengh authored and Thomas Parisot committed Jan 17, 2020
1 parent 1e5f2e9 commit 42c2467
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"chai-as-promised": "^7.1.1",
"cheerio": "0.22.0",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.0.0",
"eslint-config-hexo": "^4.1.0",
"hexo-renderer-marked": "^2.0.0",
"husky": "^3.0.0",
"lint-staged": "^9.1.0",
Expand Down
59 changes: 34 additions & 25 deletions test/scripts/box/file.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';

const pathFn = require('path');
const Promise = require('bluebird');
const fs = require('hexo-fs');
const yaml = require('js-yaml');
const { join } = require('path');
const { rmdir, stat, statSync, writeFile } = require('hexo-fs');
const { load } = require('js-yaml');

describe('File', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname);
const Box = require('../../../lib/box');
const box = new Box(hexo, pathFn.join(hexo.base_dir, 'file_test'));
const File = box.File;
const box = new Box(hexo, join(hexo.base_dir, 'file_test'));
const { File } = box;

const body = [
'name:',
Expand All @@ -24,31 +23,37 @@ describe('File', () => {
'- Banana'
].join('\n');

const obj = yaml.load(body);
const obj = load(body);
const path = 'test.yml';

function makeFile(path, props) {
const makeFile = (path, props) => {
return new File(Object.assign({
source: pathFn.join(box.base, path),
source: join(box.base, path),
path
}, props));
}
};

const file = makeFile(path, {
source: pathFn.join(box.base, path),
source: join(box.base, path),
path,
type: 'create',
params: {foo: 'bar'}
});

before(() => Promise.all([
fs.writeFile(file.source, body),
hexo.init()
]).then(() => fs.stat(file.source)));
before(async () => {
await Promise.all([
writeFile(file.source, body),
hexo.init()
]);
stat(file.source);
});

after(() => fs.rmdir(box.base));
after(() => rmdir(box.base));

it('read()', () => file.read().should.eventually.eql(body));
it('read()', async () => {
const result = await file.read();
result.should.eql(body);
});

it('read() - callback', callback => {
file.read((err, content) => {
Expand All @@ -62,27 +67,31 @@ describe('File', () => {
file.readSync().should.eql(body);
});

it('stat()', () => Promise.all([
fs.stat(file.source),
file.stat()
]).then(stats => {
it('stat()', async () => {
const stats = await Promise.all([
stat(file.source),
file.stat()
]);
stats[0].should.eql(stats[1]);
}));
});

it('stat() - callback', callback => {
file.stat((err, fileStats) => {
if (err) return callback(err);

fileStats.should.eql(fs.statSync(file.source));
fileStats.should.eql(statSync(file.source));
callback();
});
});

it('statSync()', () => {
file.statSync().should.eql(fs.statSync(file.source));
file.statSync().should.eql(statSync(file.source));
});

it('render()', () => file.render().should.eventually.eql(obj));
it('render()', async () => {
const result = await file.render();
result.should.eql(obj);
});

it('render() - callback', callback => {
file.render((err, data) => {
Expand Down

0 comments on commit 42c2467

Please sign in to comment.