-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test-models-post_asset): destructure and async/await (#4034)
* refactor(test-models-post_asset): async/await * refactor(test-models-post_asset): destructure & set defaults
- Loading branch information
Showing
1 changed file
with
69 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |