Skip to content

Commit

Permalink
refactor(test-list-categories-page-post-tags): async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Dec 17, 2019
1 parent c82082c commit c94b1ce
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 66 deletions.
31 changes: 15 additions & 16 deletions test/scripts/console/list_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,25 @@ describe('Console list', () => {
expect(console.log.calledWith(sinon.match('No categories.'))).to.be.true;
});

it('categories', () => {
it('categories', async() => {
const posts = [
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
];
return hexo.init()
.then(() => Post.insert(posts)).then(posts => Promise.each([
['foo'],
['baz'],
['baz']
], (tags, i) => posts[i].setCategories(tags))).then(() => {
hexo.locals.invalidate();
})
.then(() => {
listCategories();
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
});

await hexo.init();
const output = await Post.insert(posts);
await Promise.each([
['foo'],
['baz'],
['baz']
], (tags, i) => output[i].setCategories(tags));
await hexo.locals.invalidate();
listCategories();
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
});
});
26 changes: 13 additions & 13 deletions test/scripts/console/list_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ describe('Console list', () => {
expect(console.log.calledWith(sinon.match('No pages.'))).to.be.true;
});

it('page', () => Page.insert({
source: 'foo',
title: 'Hello World',
path: 'bar'
})
.then(() => {
listPages();
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('Hello World'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
}));
it('page', async() => {
await Page.insert({
source: 'foo',
title: 'Hello World',
path: 'bar'
});
listPages();
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('Hello World'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
});
});
35 changes: 17 additions & 18 deletions test/scripts/console/list_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,27 @@ describe('Console list', () => {
expect(console.log.calledWith(sinon.match('No posts.'))).to.be.true;
});

it('post', () => {
it('post', async() => {
const posts = [
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
];
return hexo.init()
.then(() => Post.insert(posts)).then(() => {
hexo.locals.invalidate();
})
.then(() => {
listPosts();
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('Category'))).to.be.true;
expect(console.log.calledWith(sinon.match('Tags'))).to.be.true;
for (let i = 0; i < posts.length; i++) {
expect(console.log.calledWith(sinon.match(posts[i].source))).to.be.true;
expect(console.log.calledWith(sinon.match(posts[i].slug))).to.be.true;
expect(console.log.calledWith(sinon.match(posts[i].title))).to.be.true;
}
});

await hexo.init();
await Post.insert(posts);
await hexo.locals.invalidate();

listPosts();
expect(console.log.calledWith(sinon.match('Date'))).to.be.true;
expect(console.log.calledWith(sinon.match('Title'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('Category'))).to.be.true;
expect(console.log.calledWith(sinon.match('Tags'))).to.be.true;
for (let i = 0; i < posts.length; i++) {
expect(console.log.calledWith(sinon.match(posts[i].source))).to.be.true;
expect(console.log.calledWith(sinon.match(posts[i].slug))).to.be.true;
expect(console.log.calledWith(sinon.match(posts[i].title))).to.be.true;
}
});
});
38 changes: 19 additions & 19 deletions test/scripts/console/list_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,29 @@ describe('Console list', () => {
expect(console.log.calledWith(sinon.match('No tags.'))).to.be.true;
});

it('tags', () => {
it('tags', async() => {
const posts = [
{source: 'foo', slug: 'foo', title: 'Its', date: 1e8},
{source: 'bar', slug: 'bar', title: 'Math', date: 1e8 + 1},
{source: 'baz', slug: 'baz', title: 'Dude', date: 1e8 - 1}
];
return hexo.init()
.then(() => Post.insert(posts)).then(posts => Promise.each([
['foo'],
['baz'],
['baz']
], (tags, i) => posts[i].setTags(tags))).then(() => {
hexo.locals.invalidate();
})
.then(() => {
listTags();
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
expect(console.log.calledWith(sinon.match('tags/baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('tags/foo'))).to.be.true;
});

await hexo.init();
const output = await Post.insert(posts);
await Promise.each([
['foo'],
['baz'],
['baz']
], (tags, i) => output[i].setTags(tags));
await hexo.locals.invalidate();

listTags();
expect(console.log.calledWith(sinon.match('Name'))).to.be.true;
expect(console.log.calledWith(sinon.match('Posts'))).to.be.true;
expect(console.log.calledWith(sinon.match('Path'))).to.be.true;
expect(console.log.calledWith(sinon.match('baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('foo'))).to.be.true;
expect(console.log.calledWith(sinon.match('tags/baz'))).to.be.true;
expect(console.log.calledWith(sinon.match('tags/foo'))).to.be.true;
});
});

0 comments on commit c94b1ce

Please sign in to comment.