Skip to content

Commit

Permalink
refactor(test-helpers-list): async/await (#4026)
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh authored and SukkaW committed Jan 1, 2020
1 parent 8c0f87a commit 45b437f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
16 changes: 9 additions & 7 deletions test/scripts/helpers/list_archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ describe('list_archives', () => {
ctx.site = hexo.locals.toObject();
}

before(() => hexo.init().then(() => Post.insert([
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
])).then(() => {
before(async () => {
await hexo.init();
await Post.insert([
{source: 'foo', slug: 'foo', date: new Date(2014, 1, 2)},
{source: 'bar', slug: 'bar', date: new Date(2013, 5, 6)},
{source: 'baz', slug: 'baz', date: new Date(2013, 9, 10)},
{source: 'boo', slug: 'boo', date: new Date(2013, 5, 8)}
]);
resetLocals();
}));
});

it('default', () => {
const result = listArchives();
Expand Down
34 changes: 18 additions & 16 deletions test/scripts/helpers/list_categories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Promise = require('bluebird');

describe('list_categories', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname);
Expand All @@ -16,23 +14,27 @@ describe('list_categories', () => {

const listCategories = require('../../../lib/plugins/helper/list_categories').bind(ctx);

before(() => hexo.init().then(() => Post.insert([
{source: 'foo', slug: 'foo'},
{source: 'bar', slug: 'bar'},
{source: 'baz', slug: 'baz'},
{source: 'boo', slug: 'boo'},
{source: 'bat', slug: 'bat'}
])).then(posts => Promise.each([
['baz'],
['baz', 'bar'],
['foo'],
['baz'],
['bat', ['baz', 'bar']]
], (cats, i) => posts[i].setCategories(cats))).then(() => {
before(async () => {
await hexo.init();
const posts = await Post.insert([
{source: 'foo', slug: 'foo'},
{source: 'bar', slug: 'bar'},
{source: 'baz', slug: 'baz'},
{source: 'boo', slug: 'boo'},
{source: 'bat', slug: 'bat'}
]);
await Promise.all([
['baz'],
['baz', 'bar'],
['foo'],
['baz'],
['bat', ['baz', 'bar']]
].map((cats, i) => posts[i].setCategories(cats)));

hexo.locals.invalidate();
ctx.site = hexo.locals.toObject();
ctx.page = ctx.site.posts.data[1];
}));
});

it('default', () => {
const result = listCategories();
Expand Down
15 changes: 9 additions & 6 deletions test/scripts/helpers/list_posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ describe('list_posts', () => {

hexo.config.permalink = ':title/';

before(() => hexo.init().then(() => Post.insert([
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
])).then(() => {
before(async () => {
await hexo.init();
await Post.insert([
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
{source: 'bar', slug: 'bar', title: 'Chemistry', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'Bitch', date: 1e8 - 1}
]);

hexo.locals.invalidate();
ctx.site = hexo.locals.toObject();
}));
});

it('default', () => {
const result = listPosts();
Expand Down
24 changes: 13 additions & 11 deletions test/scripts/helpers/list_tags.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const Promise = require('bluebird');

describe('list_tags', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(__dirname);
Expand All @@ -16,21 +14,25 @@ describe('list_tags', () => {

const listTags = require('../../../lib/plugins/helper/list_tags').bind(ctx);

before(() => hexo.init().then(() => Post.insert([
{source: 'foo', slug: 'foo'},
{source: 'bar', slug: 'bar'},
{source: 'baz', slug: 'baz'},
{source: 'boo', slug: 'boo'}
])).then(posts => // TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
Promise.each([
before(async () => {
await hexo.init();
const posts = await Post.insert([
{source: 'foo', slug: 'foo'},
{source: 'bar', slug: 'bar'},
{source: 'baz', slug: 'baz'},
{source: 'boo', slug: 'boo'}
]);
// TODO: Warehouse needs to add a mutex lock when writing data to avoid data sync problem
await Promise.all([
['foo'],
['baz'],
['baz'],
['bar']
], (tags, i) => posts[i].setTags(tags))).then(() => {
].map((tags, i) => posts[i].setTags(tags)));

hexo.locals.invalidate();
ctx.site = hexo.locals.toObject();
}));
});

it('default', () => {
const result = listTags();
Expand Down

0 comments on commit 45b437f

Please sign in to comment.