From 3b2f7a092e4e84ec08dd5b22cc1b4fc9c0a68e42 Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Wed, 1 Jan 2020 04:22:07 +1030 Subject: [PATCH] refactor(test-processors-data): async/await & destructure (#4036) * refactor(test-processors-data): async/await * refactor(test-processors-data): destructure --- test/scripts/processors/data.js | 101 +++++++++++++++++++------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/test/scripts/processors/data.js b/test/scripts/processors/data.js index c980c5ac15..a3352b88c5 100644 --- a/test/scripts/processors/data.js +++ b/test/scripts/processors/data.js @@ -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; @@ -22,24 +24,37 @@ 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({ @@ -47,16 +62,17 @@ describe('data', () => { 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({ @@ -64,31 +80,33 @@ describe('data', () => { 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({ @@ -96,32 +114,33 @@ describe('data', () => { 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'); }); });