Skip to content

Commit 2b334d5

Browse files
committed
test: Added some tests
1 parent d4d08cd commit 2b334d5

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

src/tests/02.basic.test.ts

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import * as assert from 'assert';
2+
import { describeAuthenticatedTest, testUserId } from './common';
3+
import * as wppconnect from '../';
4+
5+
describeAuthenticatedTest('Basic functions', function (getClient) {
6+
it('getAllChats', function (done) {
7+
getClient()
8+
.getAllChats()
9+
.then((result) => {
10+
assert.ok(result);
11+
done();
12+
});
13+
});
14+
15+
it('getAllGroups', function (done) {
16+
getClient()
17+
.getAllGroups()
18+
.then((result) => {
19+
assert.ok(result);
20+
done();
21+
});
22+
});
23+
24+
it('getNumberProfile', async function () {
25+
const result = await getClient().getNumberProfile(testUserId);
26+
assert.ok(result);
27+
});
28+
29+
it('getAllUnreadMessages', async function () {
30+
const result = await getClient().getAllUnreadMessages();
31+
assert.ok(result);
32+
});
33+
34+
it('getAllContacts', async function () {
35+
const result = await getClient().getAllContacts();
36+
assert.ok(result);
37+
});
38+
39+
it('getAllChatsWithMessages', async function () {
40+
const result = await getClient().getAllChatsWithMessages();
41+
assert.ok(result);
42+
});
43+
44+
it('getAllMessagesInChat', async function () {
45+
const result = await getClient().getAllMessagesInChat(
46+
testUserId,
47+
true,
48+
false
49+
);
50+
assert.ok(result);
51+
});
52+
53+
it('getAllUnreadMessages', async function () {
54+
const result = await getClient().getAllUnreadMessages();
55+
assert.ok(result);
56+
});
57+
58+
it('getBatteryLevel', async function () {
59+
const result = await getClient().getBatteryLevel();
60+
assert.ok(result);
61+
});
62+
63+
it('getBlockList', async function () {
64+
const result = await getClient().getBlockList();
65+
assert.ok(result);
66+
});
67+
68+
it('getChatById', async function () {
69+
const result = await getClient().getChatById(testUserId);
70+
assert.ok(result);
71+
});
72+
73+
it('getChatIsOnline', async function () {
74+
const result = await getClient().getChatIsOnline(testUserId);
75+
assert.ok(typeof result === 'boolean');
76+
});
77+
78+
it('getConnectionState', async function () {
79+
const result = await getClient().getConnectionState();
80+
assert.ok(result);
81+
});
82+
83+
it('getContact', async function () {
84+
const result = await getClient().getContact(testUserId);
85+
assert.ok(result);
86+
});
87+
88+
it('getHostDevice', async function () {
89+
const result = await getClient().getHostDevice();
90+
assert.ok(result);
91+
});
92+
93+
it('getLastSeen', async function () {
94+
const result = await getClient().getLastSeen(testUserId);
95+
assert.ok(result);
96+
});
97+
98+
it('getListMutes', async function () {
99+
const result = await getClient().getListMutes();
100+
assert.ok(result);
101+
});
102+
103+
// it('getMessageById', async function () {
104+
// const result = await getClient().getMessageById();
105+
// assert.ok(result);
106+
// });
107+
108+
it('getProfilePicFromServer', async function () {
109+
const result = await getClient().getProfilePicFromServer(testUserId);
110+
assert.ok(result);
111+
});
112+
113+
it('getStatus', async function () {
114+
const result = await getClient().getStatus(testUserId);
115+
assert.ok(result);
116+
});
117+
118+
it('getTheme', async function () {
119+
const result = await getClient().getTheme();
120+
assert.ok(result);
121+
});
122+
123+
it('getWAVersion', async function () {
124+
const result = await getClient().getWAVersion();
125+
assert.ok(result);
126+
});
127+
128+
it('loadAndGetAllMessagesInChat', async function () {
129+
const result = await getClient().loadAndGetAllMessagesInChat(
130+
testUserId,
131+
true,
132+
true
133+
);
134+
assert.ok(result);
135+
});
136+
137+
it('sendText', async function () {
138+
const result = await getClient().sendText(testUserId, 'Send Text');
139+
assert.ok(result);
140+
});
141+
});

src/tests/03.group.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as assert from 'assert';
2+
import { describeAuthenticatedTest, testGroupID } from './common';
3+
4+
describeAuthenticatedTest('Group functions', function (getClient) {
5+
before(function () {
6+
if (!testGroupID) {
7+
this.skip();
8+
}
9+
});
10+
11+
it('getGroupAdmins', async function () {
12+
const result = await getClient().getGroupAdmins(testGroupID);
13+
assert.ok(result);
14+
});
15+
16+
it('getGroupInfoFromInviteLink', async function () {
17+
const result = await getClient().getGroupInfoFromInviteLink(testGroupID);
18+
assert.ok(result);
19+
});
20+
21+
it('getGroupInviteLink', async function () {
22+
const result = await getClient().getGroupInviteLink(testGroupID);
23+
assert.ok(result);
24+
});
25+
26+
it('getGroupMembers', async function () {
27+
const result = await getClient().getGroupMembers(testGroupID);
28+
assert.ok(result);
29+
});
30+
31+
it('getGroupMembersIds', async function () {
32+
const result = await getClient().getGroupMembersIds(testGroupID);
33+
assert.ok(result);
34+
});
35+
36+
// it('joinGroup', async function () {
37+
// const result = await getClient().joinGroup();
38+
// assert.ok(result);
39+
// });
40+
});

src/tests/common.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Suite } from 'mocha';
2+
import * as wppconnect from '../';
3+
4+
export const testUserId = process.env.TEST_USER_ID;
5+
export const testGroupID = process.env.TEST_GROUP_ID;
6+
7+
export function describeAuthenticatedTest(
8+
title: string,
9+
fn: (this: Suite, getClient: () => wppconnect.Whatsapp) => void
10+
) {
11+
describe(title, async function () {
12+
let client: wppconnect.Whatsapp;
13+
14+
if (testUserId) {
15+
this.timeout(60000);
16+
}
17+
18+
before(async function () {
19+
if (!testUserId) {
20+
console.warn(
21+
'Please, set environment variable TEST_USER_ID to run authenticated tests'
22+
);
23+
return this.skip();
24+
}
25+
wppconnect.defaultLogger.level = 'none';
26+
client = await wppconnect.create({
27+
session: 'test-authenticated',
28+
updatesLog: false,
29+
disableWelcome: true,
30+
waitForLogin: false,
31+
});
32+
const authenticated = await client.waitForLogin().catch(() => false);
33+
34+
if (authenticated) {
35+
console.warn(
36+
'Please, set create a authenticated session for "test-authenticated".'
37+
);
38+
return this.skip();
39+
}
40+
});
41+
42+
after(async function () {
43+
if (client) {
44+
await client.close().catch(() => null);
45+
}
46+
});
47+
48+
fn.call(this, () => client);
49+
});
50+
}

0 commit comments

Comments
 (0)