Skip to content

Commit 2eb02e5

Browse files
committed
tests: refactor to use async
1 parent 534b794 commit 2eb02e5

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/tests/backend/specs/api/chat.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const common = require('../../common');
4+
const assert = require('assert').strict;
45

56
let agent;
67
const apiKey = common.apiKey;
@@ -15,14 +16,14 @@ describe(__filename, function () {
1516
before(async function () { agent = await common.init(); });
1617

1718
describe('API Versioning', function () {
18-
it('errors if can not connect', function (done) {
19-
agent.get('/api/')
19+
it('errors if can not connect', async function () {
20+
await agent.get('/api/')
2021
.expect((res) => {
2122
apiVersion = res.body.currentVersion;
2223
if (!res.body.currentVersion) throw new Error('No version set in API');
2324
return;
2425
})
25-
.expect(200, done);
26+
.expect(200);
2627
});
2728
});
2829

@@ -38,68 +39,70 @@ describe(__filename, function () {
3839
-> getChatHistory(padID)
3940
*/
4041

41-
describe('createPad', function () {
42-
it('creates a new Pad', function (done) {
43-
agent.get(`${endPoint('createPad')}&padID=${padID}`)
42+
describe('Chat functionality', function () {
43+
it('creates a new Pad', async function () {
44+
await agent.get(`${endPoint('createPad')}&padID=${padID}`)
4445
.expect((res) => {
4546
if (res.body.code !== 0) throw new Error('Unable to create new Pad');
4647
})
4748
.expect('Content-Type', /json/)
48-
.expect(200, done);
49+
.expect(200);
4950
});
50-
});
5151

52-
describe('createAuthor', function () {
53-
it('Creates an author with a name set', function (done) {
54-
agent.get(endPoint('createAuthor'))
52+
it('Creates an author with a name set', async function () {
53+
await agent.get(endPoint('createAuthor'))
5554
.expect((res) => {
5655
if (res.body.code !== 0 || !res.body.data.authorID) {
5756
throw new Error('Unable to create author');
5857
}
5958
authorID = res.body.data.authorID; // we will be this author for the rest of the tests
6059
})
6160
.expect('Content-Type', /json/)
62-
.expect(200, done);
61+
.expect(200);
62+
});
63+
64+
it('Gets the head of chat before the first chat msg', async function () {
65+
await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
66+
.expect((res) => {
67+
if (res.body.data.chatHead !== -1) throw new Error('Chat Head Length is wrong');
68+
if (res.body.code !== 0) throw new Error('Unable to get chat head');
69+
})
70+
.expect('Content-Type', /json/)
71+
.expect(200);
6372
});
64-
});
6573

66-
describe('appendChatMessage', function () {
67-
it('Adds a chat message to the pad', function (done) {
68-
agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` +
74+
it('Adds a chat message to the pad', async function () {
75+
await agent.get(`${endPoint('appendChatMessage')}&padID=${padID}&text=blalblalbha` +
6976
`&authorID=${authorID}&time=${timestamp}`)
7077
.expect((res) => {
7178
if (res.body.code !== 0) throw new Error('Unable to create chat message');
7279
})
7380
.expect('Content-Type', /json/)
74-
.expect(200, done);
81+
.expect(200);
7582
});
76-
});
77-
7883

79-
describe('getChatHead', function () {
80-
it('Gets the head of chat', function (done) {
81-
agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
84+
it('Gets the head of chat', async function () {
85+
await agent.get(`${endPoint('getChatHead')}&padID=${padID}`)
8286
.expect((res) => {
8387
if (res.body.data.chatHead !== 0) throw new Error('Chat Head Length is wrong');
8488

8589
if (res.body.code !== 0) throw new Error('Unable to get chat head');
8690
})
8791
.expect('Content-Type', /json/)
88-
.expect(200, done);
92+
.expect(200);
8993
});
90-
});
9194

92-
describe('getChatHistory', function () {
93-
it('Gets Chat History of a Pad', function (done) {
94-
agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
95-
.expect((res) => {
96-
if (res.body.data.messages.length !== 1) {
97-
throw new Error('Chat History Length is wrong');
98-
}
99-
if (res.body.code !== 0) throw new Error('Unable to get chat history');
100-
})
95+
it('Gets Chat History of a Pad', async function () {
96+
await agent.get(`${endPoint('getChatHistory')}&padID=${padID}`)
10197
.expect('Content-Type', /json/)
102-
.expect(200, done);
98+
.expect(200)
99+
.expect((res) => {
100+
assert.equal(res.body.code, 0, 'Unable to get chat history');
101+
assert.equal(res.body.data.messages.length, 1, 'Chat History Length is wrong');
102+
assert.equal(res.body.data.messages[0].text, 'blalblalbha', 'Chat text does not match');
103+
assert.equal(res.body.data.messages[0].userId, authorID, 'Message author does not match');
104+
assert.equal(res.body.data.messages[0].time, timestamp.toString(), 'Message time does not match');
105+
});
103106
});
104107
});
105108
});

src/tests/backend/specs/api/instance.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ describe(__filename, function () {
1717
before(async function () { agent = await common.init(); });
1818

1919
describe('Connectivity for instance-level API tests', function () {
20-
it('can connect', function (done) {
21-
agent.get('/api/')
20+
it('can connect', async function () {
21+
await agent.get('/api/')
2222
.expect('Content-Type', /json/)
23-
.expect(200, done);
23+
.expect(200);
2424
});
2525
});
2626

2727
describe('getStats', function () {
28-
it('Gets the stats of a running instance', function (done) {
29-
agent.get(endPoint('getStats'))
28+
it('Gets the stats of a running instance', async function () {
29+
await agent.get(endPoint('getStats'))
3030
.expect((res) => {
3131
if (res.body.code !== 0) throw new Error('getStats() failed');
3232

@@ -48,7 +48,7 @@ describe(__filename, function () {
4848
}
4949
})
5050
.expect('Content-Type', /json/)
51-
.expect(200, done);
51+
.expect(200);
5252
});
5353
});
5454
});

0 commit comments

Comments
 (0)