Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test-models-post_asset): async/await #4034

Merged
merged 2 commits into from
Jan 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});