Skip to content

Commit

Permalink
refactor(test-processors-data): async/await & destructure (hexojs#4036)
Browse files Browse the repository at this point in the history
* refactor(test-processors-data): async/await
* refactor(test-processors-data): destructure
  • Loading branch information
curbengh authored and Thomas Parisot committed Jan 17, 2020
1 parent 26a9148 commit 3b2f7a0
Showing 1 changed file with 60 additions and 41 deletions.
101 changes: 60 additions & 41 deletions test/scripts/processors/data.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';

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

describe('data', () => {
const Hexo = require('../../../lib/hexo');
const baseDir = pathFn.join(__dirname, 'data_test');
const baseDir = join(__dirname, 'data_test');
const hexo = new Hexo(baseDir);
const processor = require('../../../lib/plugins/processor/data')(hexo);
const process = Promise.method(processor.process).bind(hexo);
const source = hexo.source;
const File = source.File;
const { source } = hexo;
const { File } = source;
const Data = hexo.model('Data');

const typeOf = str => typeof str;

function newFile(options) {
const path = options.path;

Expand All @@ -22,106 +24,123 @@ describe('data', () => {
};

options.path = '_data/' + path;
options.source = pathFn.join(source.base, options.path);
options.source = join(source.base, options.path);

return new File(options);
}

before(() => fs.mkdirs(baseDir).then(() => hexo.init()));
before(async () => {
await mkdirs(baseDir);
hexo.init();
});

after(() => fs.rmdir(baseDir));
after(() => rmdir(baseDir));

it('pattern', () => {
const pattern = processor.pattern;

pattern.match('_data/users.json').should.be.ok;
pattern.match('_data/users.yaml').should.be.ok;
should.not.exist(pattern.match('users.json'));
pattern.match('_data/users.json').should.eql({
0: '_data/users.json',
1: 'users.json',
path: 'users.json'
});

pattern.match('_data/users.yaml').should.eql({
0: '_data/users.yaml',
1: 'users.yaml',
path: 'users.yaml'
});

typeOf(pattern.match('users.json')).should.eql('undefined');
});

it('type: create - yaml', () => {
it('type: create - yaml', async () => {
const body = 'foo: bar';

const file = newFile({
path: 'users.yml',
type: 'create'
});

return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const data = Data.findById('users');
await writeFile(file.source, body);
await process(file);
const data = Data.findById('users');

data.data.should.eql({foo: 'bar'});
data.data.should.eql({foo: 'bar'});

return data.remove();
}).finally(() => fs.unlink(file.source));
data.remove();
unlink(file.source);
});

it('type: create - json', () => {
it('type: create - json', async () => {
const body = '{"foo": 1}';

const file = newFile({
path: 'users.json',
type: 'create'
});

return fs.writeFile(file.source, body).then(() => process(file)).then(() => {
const data = Data.findById('users');
await writeFile(file.source, body);
await process(file);
const data = Data.findById('users');

data.data.should.eql({foo: 1});
data.data.should.eql({foo: 1});

return data.remove();
}).finally(() => fs.unlink(file.source));
data.remove();
unlink(file.source);
});

it('type: create - others', () => {
it('type: create - others', async () => {
const file = newFile({
path: 'users.txt',
type: 'create'
});

return fs.writeFile(file.source, 'text').then(() => process(file)).then(() => {
const data = Data.findById('users');
await writeFile(file.source, 'text');
await process(file);
const data = Data.findById('users');

data.data.should.eql('text');
data.data.should.eql('text');

return data.remove();
}).finally(() => fs.unlink(file.source));
data.remove();
unlink(file.source);
});

it('type: update', () => {
it('type: update', async () => {
const body = 'foo: bar';

const file = newFile({
path: 'users.yml',
type: 'update'
});

return Promise.all([
fs.writeFile(file.source, body),
await Promise.all([
writeFile(file.source, body),
Data.insert({
_id: 'users',
data: {}
})
]).then(() => process(file)).then(() => {
const data = Data.findById('users');
]);
await process(file);
const data = Data.findById('users');

data.data.should.eql({foo: 'bar'});
data.data.should.eql({foo: 'bar'});

return data.remove();
}).finally(() => fs.unlink(file.source));
data.remove();
unlink(file.source);
});

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

return Data.insert({
await Data.insert({
_id: 'users',
data: {foo: 'bar'}
}).then(() => process(file)).then(() => {
should.not.exist(Data.findById('users'));
});
await process(file);
typeOf(Data.findById('users')).should.eql('undefined');
});
});

0 comments on commit 3b2f7a0

Please sign in to comment.