Updated chat tests with new async/await syntax

This commit is contained in:
Keith Holliday
2016-01-25 15:22:54 -06:00
parent 4149cbf381
commit bd87ada902
3 changed files with 144 additions and 202 deletions

View File

@@ -1,36 +1,32 @@
import {
generateUser,
createAndPopulateGroup,
translate as t,
} from '../../../../helpers/api-v3-integration.helper';
import { find } from 'lodash';
describe('POST /chat/:chatId/like', () => {
let user;
let group;
let groupWithChat;
let testMessage = 'Test Message';
let anotherUser;
before(() => {
let groupName = 'Test Guild';
let groupType = 'guild';
let groupPrivacy = 'public';
return generateUser({balance: 1}).then((generatedUser) => {
user = generatedUser;
})
.then(() => {
return user.post('/groups', {
name: groupName,
type: groupType,
privacy: groupPrivacy,
});
})
.then((generatedGroup) => {
group = generatedGroup;
before(async () => {
let { group, groupLeader, members } = await createAndPopulateGroup({
groupDetails: {
name: 'Test Guild',
type: 'guild',
privacy: 'public',
},
members: 1,
});
user = groupLeader;
groupWithChat = group;
anotherUser = members[0];
});
it('Returns an error when chat message is not found', () => {
return expect(user.post(`/groups/${group._id}/chat/incorrectMessage/like`))
it('Returns an error when chat message is not found', async () => {
await expect(user.post(`/groups/${groupWithChat._id}/chat/incorrectMessage/like`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
@@ -38,59 +34,42 @@ describe('POST /chat/:chatId/like', () => {
});
});
it('Returns an error when user tries to like their own message', () => {
return user.post(`/groups/${group._id}/chat`, { message: testMessage})
.then((result) => {
return expect(user.post(`/groups/${group._id}/chat/${result.message.id}/like`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('messageGroupChatLikeOwnMessage'),
});
});
it('Returns an error when user tries to like their own message', async () => {
let message = await user.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
await expect(user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('messageGroupChatLikeOwnMessage'),
});
});
it('Likes a chat', () => {
let message;
it('Likes a chat', async () => {
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
return generateUser().then((anotherUser) => {
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
})
.then((result) => {
message = result.message;
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
})
.then((result) => {
expect(result.likes[user._id]).to.equal(true);
return user.get(`/groups/${group._id}`);
})
.then((updatedGroup) => {
let messageToCheck = find(updatedGroup.chat, {id: message.id});
expect(messageToCheck.likes[user._id]).to.equal(true);
});
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
expect(likeResult.likes[user._id]).to.equal(true);
let groupWithChatLikes = await user.get(`/groups/${groupWithChat._id}`);
let messageToCheck = find(groupWithChatLikes.chat, {id: message.message.id});
expect(messageToCheck.likes[user._id]).to.equal(true);
});
it('Unlikes a chat', () => {
let message;
it('Unlikes a chat', async () => {
let message = await anotherUser.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
return generateUser().then((anotherUser) => {
return anotherUser.post(`/groups/${group._id}/chat`, { message: testMessage});
})
.then((result) => {
message = result.message;
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
})
.then((result) => {
expect(result.likes[user._id]).to.equal(true);
return user.post(`/groups/${group._id}/chat/${message.id}/like`);
})
.then((result) => {
expect(result.likes[user._id]).to.equal(false);
return user.get(`/groups/${group._id}`);
})
.then((updatedGroup) => {
let messageToCheck = find(updatedGroup.chat, {id: message.id});
expect(messageToCheck.likes[user._id]).to.equal(false);
});
let likeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
expect(likeResult.likes[user._id]).to.equal(true);
let unlikeResult = await user.post(`/groups/${groupWithChat._id}/chat/${message.message.id}/like`);
expect(unlikeResult.likes[user._id]).to.equal(false);
let groupWithoutChatLikes = await user.get(`/groups/${groupWithChat._id}`);
let messageToCheck = find(groupWithoutChatLikes.chat, {id: message.message.id});
expect(messageToCheck.likes[user._id]).to.equal(false);
});
});