Skip to content

Commit

Permalink
refactor(test-models-post_asset): destructure and async/await (#4034)
Browse files Browse the repository at this point in the history
* refactor(test-models-post_asset): async/await
* refactor(test-models-post_asset): destructure & set defaults
  • Loading branch information
curbengh committed Jan 31, 2020
1 parent 667d9a0 commit 25a87b5
Showing 1 changed file with 69 additions and 69 deletions.
138 changes: 69 additions & 69 deletions test/scripts/models/post_asset.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
'use strict';

const sinon = require('sinon');
const pathFn = require('path');
const { join } = require('path');

describe('PostAsset', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const PostAsset = hexo.model('PostAsset');
const Post = hexo.model('Post');
let post;
const defaults = require('../../../lib/hexo/default_config');

before(() => hexo.init().then(() => Post.insert({
source: 'foo.md',
slug: 'bar'
})).then(post_ => {
post = post_;
}));

it('default values', () => PostAsset.insert({
_id: 'foo',
slug: 'foo',
post: post._id
}).then(data => {
data.modified.should.be.true;
return PostAsset.removeById(data._id);
}));

it('_id - required', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', 'ID is not defined');
before(async () => {
await hexo.init();
post = await Post.insert({
source: 'foo.md',
slug: 'bar'
});
});

return PostAsset.insert({}).catch(errorCallback).finally(() => {
errorCallback.calledOnce.should.be.true;
});
beforeEach(() => {
hexo.config = Object.assign({}, defaults);
});

it('slug - required', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', '`slug` is required!');
it('default values', async () => {
const data = await PostAsset.insert({
_id: 'foo',
slug: 'foo',
post: post._id
});
data.modified.should.eql(true);
PostAsset.removeById(data._id);
});

return PostAsset.insert({
_id: 'foo'
}).catch(errorCallback).finally(() => {
errorCallback.calledOnce.should.be.true;
});
it('_id - required', async () => {
try {
await PostAsset.insert({});
} catch (err) {
err.message.should.eql('ID is not defined');
}
});

it('path - virtual', () => PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}));
it('slug - required', async () => {
try {
await PostAsset.insert({
_id: 'foo'
});
} catch (err) {
err.message.should.eql('`slug` is required!');
}
});

it('path - virtual', async () => {
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink is .html', () => {
it('path - virtual - when permalink is .html', async () => {
hexo.config.permalink = ':year/:month/:day/:title.html';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink is .htm', () => {
it('path - virtual - when permalink is .htm', async () => {
hexo.config.permalink = ':year/:month/:day/:title.htm';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.htm',
slug: 'foo.htm',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path, data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path, data.slug));

PostAsset.removeById(data._id);
});

it('path - virtual - when permalink contains .htm not in the end', () => {
it('path - virtual - when permalink contains .htm not in the end', async () => {
hexo.config.permalink = ':year/:month/:day/:title/.htm-foo/';
return PostAsset.insert({
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.html',
slug: 'foo.html',
post: post._id
}).then(data => {
data.path.should.eql(pathFn.join(post.path + '.htm-foo/', data.slug));
return PostAsset.removeById(data._id);
}).finally(() => {
hexo.config.permalink = ':year/:month/:day/:title';
});
data.path.should.eql(join(post.path + '.htm-foo/', data.slug));

PostAsset.removeById(data._id);
});

it('source - virtual', () => PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
}).then(data => {
data.source.should.eql(pathFn.join(hexo.base_dir, data._id));
return PostAsset.removeById(data._id);
}));
it('source - virtual', async () => {
const data = await PostAsset.insert({
_id: 'source/_posts/test/foo.jpg',
slug: 'foo.jpg',
post: post._id
});
data.source.should.eql(join(hexo.base_dir, data._id));

PostAsset.removeById(data._id);
});
});

0 comments on commit 25a87b5

Please sign in to comment.