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-tag): async/await #4035

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
276 changes: 144 additions & 132 deletions test/scripts/models/tag.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,141 @@
'use strict';

const sinon = require('sinon');
const Promise = require('bluebird');
const { full_url_for } = require('hexo-util');
const { deepMerge, full_url_for } = require('hexo-util');

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

before(() => hexo.init());

it('name - required', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', '`name` is required!');
});
beforeEach(() => { hexo.config = deepMerge({}, defaults); });

return Tag.insert({}).catch(errorCallback).finally(() => {
errorCallback.calledOnce.should.be.true;
});
it('name - required', async () => {
try {
await Tag.insert({});
} catch (err) {
err.message.should.be.eql('`name` is required!');
}
});

it('slug - virtual', () => Tag.insert({
name: 'foo'
}).then(data => {
it('slug - virtual', async () => {
const data = await Tag.insert({
name: 'foo'
});
data.slug.should.eql('foo');
return Tag.removeById(data._id);
}));

it('slug - tag_map', () => {
Tag.removeById(data._id);
});

it('slug - tag_map', async () => {
hexo.config.tag_map = {
test: 'wat'
};

return Tag.insert({
const data = await Tag.insert({
name: 'test'
}).then(data => {
data.slug.should.eql('wat');
hexo.config.tag_map = {};

return Tag.removeById(data._id);
});
data.slug.should.eql('wat');
Tag.removeById(data._id);
});

it('slug - filename_case: 0', () => Tag.insert({
name: 'WahAHa'
}).then(data => {
it('slug - filename_case: 0', async () => {
const data = await Tag.insert({
name: 'WahAHa'
});
data.slug.should.eql('WahAHa');
return Tag.removeById(data._id);
}));

it('slug - filename_case: 1', () => {
Tag.removeById(data._id);
});

it('slug - filename_case: 1', async () => {
hexo.config.filename_case = 1;

return Tag.insert({
const data = await Tag.insert({
name: 'WahAHa'
}).then(data => {
data.slug.should.eql('wahaha');
hexo.config.filename_case = 0;
return Tag.removeById(data._id);
});
data.slug.should.eql('wahaha');

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

it('slug - filename_case: 2', () => {
it('slug - filename_case: 2', async () => {
hexo.config.filename_case = 2;

return Tag.insert({
const data = await Tag.insert({
name: 'WahAHa'
}).then(data => {
data.slug.should.eql('WAHAHA');
hexo.config.filename_case = 0;
return Tag.removeById(data._id);
});

data.slug.should.eql('WAHAHA');

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

it('path - virtual', () => Tag.insert({
name: 'foo'
}).then(data => {
it('path - virtual', async () => {
const data = await Tag.insert({
name: 'foo'
});

data.path.should.eql(hexo.config.tag_dir + '/' + data.slug + '/');
return Tag.removeById(data._id);
}));

it('permalink - virtual', () => Tag.insert({
name: 'foo'
}).then(data => {
Tag.removeById(data._id);
});

it('permalink - virtual', async () => {
const data = await Tag.insert({
name: 'foo'
});

data.permalink.should.eql(hexo.config.url + '/' + data.path);
return Tag.removeById(data._id);
}));

it('permalink - trailing_index', () => {
Tag.removeById(data._id);
});

it('permalink - trailing_index', async () => {
hexo.config.pretty_urls.trailing_index = false;
return Tag.insert({
const data = await Tag.insert({
name: 'foo'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, ''));
hexo.config.pretty_urls.trailing_index = true;
return Tag.removeById(data._id);
});

data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/index\.html$/, ''));

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

it('permalink - trailing_html', () => {
it('permalink - trailing_html', async () => {
hexo.config.pretty_urls.trailing_html = false;
return Tag.insert({
const data = await Tag.insert({
name: 'foo'
}).then(data => {
data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));
hexo.config.pretty_urls.trailing_html = true;
return Tag.removeById(data._id);
});

data.permalink.should.eql(hexo.config.url + '/' + data.path.replace(/\.html$/, ''));

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

it('permalink - should be encoded', () => {
it('permalink - should be encoded', async () => {
hexo.config.url = 'http://fôo.com';
return Tag.insert({
const data = await Tag.insert({
name: '字'
}).then(data => {
data.permalink.should.eql(full_url_for.call(hexo, data.path));
hexo.config.url = 'http://yoursite.com';
return Tag.removeById(data._id);
});

data.permalink.should.eql(full_url_for.call(hexo, data.path));

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

it('posts - virtual', () => Post.insert([
{source: 'foo.md', slug: 'foo'},
{source: 'bar.md', slug: 'bar'},
{source: 'baz.md', slug: 'baz'}
]).each(post => post.setTags(['foo'])).then(posts => {
it('posts - virtual', async () => {
const posts = await Post.insert([
{source: 'foo.md', slug: 'foo'},
{source: 'bar.md', slug: 'bar'},
{source: 'baz.md', slug: 'baz'}
]);
await Promise.all(posts.map(post => post.setTags(['foo'])));

const tag = Tag.findOne({name: 'foo'});

function mapper(post) {
Expand All @@ -138,14 +146,18 @@ describe('Tag', () => {
tag.posts.map(mapper).should.eql(posts.map(mapper));
tag.length.should.eql(posts.length);

return tag.remove().thenReturn(posts);
}).map(post => post.remove()));
await tag.remove();
await Promise.all(posts.map(post => post.remove()));
});

it('posts - draft', async () => {
const posts = await Post.insert([
{source: 'foo.md', slug: 'foo', published: true},
{source: 'bar.md', slug: 'bar', published: false},
{source: 'baz.md', slug: 'baz', published: true}
]);
await Promise.all(posts.map(post => post.setTags(['foo'])));

it('posts - draft', () => Post.insert([
{source: 'foo.md', slug: 'foo', published: true},
{source: 'bar.md', slug: 'bar', published: false},
{source: 'baz.md', slug: 'baz', published: true}
]).each(post => post.setTags(['foo'])).then(posts => {
let tag = Tag.findOne({name: 'foo'});

function mapper(post) {
Expand All @@ -166,73 +178,73 @@ describe('Tag', () => {
tag.length.should.eql(posts.length);
hexo.config.render_drafts = false;

return tag.remove().thenReturn(posts);
}).map(post => post.remove()));
await tag.remove();
await Promise.all(posts.map(post => post.remove()));
});

it('posts - future', () => {
it('posts - future', async () => {
const now = Date.now();

return Post.insert([
const posts = await Post.insert([
{source: 'foo.md', slug: 'foo', date: now - 3600},
{source: 'bar.md', slug: 'bar', date: now + 3600},
{source: 'baz.md', slug: 'baz', date: now}
]).each(post => post.setTags(['foo'])).then(posts => {
let tag = Tag.findOne({name: 'foo'});

function mapper(post) {
return post._id;
}

// future on
hexo.config.future = true;
hexo.locals.invalidate();
tag.posts.map(mapper).should.eql(posts.map(mapper));
tag.length.should.eql(posts.length);

// future off
hexo.config.future = false;
hexo.locals.invalidate();
tag = Tag.findOne({name: 'foo'});
tag.posts.eq(0)._id.should.eql(posts[0]._id);
tag.posts.eq(1)._id.should.eql(posts[2]._id);
tag.length.should.eql(2);

return tag.remove().thenReturn(posts);
}).map(post => post.remove());
});

it('check whether a tag exists', () => {
const errorCallback = sinon.spy(err => {
err.should.have.property('message', 'Tag `foo` has already existed!');
});
]);
await Promise.all(posts.map(post => post.setTags(['foo'])));

let tag = Tag.findOne({name: 'foo'});

return Tag.insert({
function mapper(post) {
return post._id;
}

// future on
hexo.config.future = true;
hexo.locals.invalidate();
tag.posts.map(mapper).should.eql(posts.map(mapper));
tag.length.should.eql(posts.length);

// future off
hexo.config.future = false;
hexo.locals.invalidate();
tag = Tag.findOne({name: 'foo'});
tag.posts.eq(0)._id.should.eql(posts[0]._id);
tag.posts.eq(1)._id.should.eql(posts[2]._id);
tag.length.should.eql(2);

await tag.remove();
await Promise.all(posts.map(post => post.remove()));
});

it('check whether a tag exists', async () => {
let data = await Tag.insert({
name: 'foo'
}).then(data => {
Tag.insert({
});

try {
data = await Tag.insert({
name: 'foo'
}).catch(errorCallback);
});
} catch (err) {
err.message.should.eql('Tag `foo` has already existed!');
}

return Tag.removeById(data._id);
}).finally(() => {
errorCallback.calledOnce.should.be.true;
});
Tag.removeById(data._id);
});

it('remove PostTag references when a tag is removed', () => {
let tag;

return Post.insert([
it('remove PostTag references when a tag is removed', async () => {
const posts = await Post.insert([
{source: 'foo.md', slug: 'foo'},
{source: 'bar.md', slug: 'bar'},
{source: 'baz.md', slug: 'baz'}
]).then(posts => // One item a time
Promise.map(posts, post => post.setTags(['foo']).thenReturn(post), {concurrency: 1})).then(posts => {
tag = Tag.findOne({name: 'foo'});
return Tag.removeById(tag._id).thenReturn(posts);
}).then(posts => {
PostTag.find({tag_id: tag._id}).length.should.eql(0);
return posts;
}).map(post => Post.removeById(post._id));
]);
await Promise.all(posts.map(post => post.setTags(['foo'])));

const tag = Tag.findOne({name: 'foo'});
await Tag.removeById(tag._id);

PostTag.find({tag_id: tag._id}).length.should.eql(0);

await Promise.all(posts.map(post => Post.removeById(post._id)));
});
});