Add test to verify websocket events come through for changing message visibility
This commit is contained in:
@@ -27,9 +27,9 @@ test('can fetch chat messages', async (done) => {
|
|||||||
.auth('admin', 'abc123')
|
.auth('admin', 'abc123')
|
||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
const expectedBody = `${testMessage.body}`
|
const expectedBody = `${testMessage.body}`;
|
||||||
const message = res.body.filter(function (msg) {
|
const message = res.body.filter(function (msg) {
|
||||||
return msg.body === expectedBody
|
return msg.body === expectedBody;
|
||||||
})[0];
|
})[0];
|
||||||
|
|
||||||
expect(message.body).toBe(expectedBody);
|
expect(message.body).toBe(expectedBody);
|
||||||
|
|||||||
@@ -1,42 +1,71 @@
|
|||||||
const { test } = require('@jest/globals');
|
const { test } = require('@jest/globals');
|
||||||
var request = require('supertest');
|
var request = require('supertest');
|
||||||
request = request('http://127.0.0.1:8080');
|
request = request('http://127.0.0.1:8080');
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
|
||||||
const registerChat = require('./lib/chat').registerChat;
|
const registerChat = require('./lib/chat').registerChat;
|
||||||
const sendChatMessage = require('./lib/chat').sendChatMessage;
|
const sendChatMessage = require('./lib/chat').sendChatMessage;
|
||||||
|
const listenForEvent = require('./lib/chat').listenForEvent;
|
||||||
|
|
||||||
const testVisibilityMessage = {
|
const testVisibilityMessage = {
|
||||||
body: "message " + Math.floor(Math.random() * 100),
|
body: 'message ' + Math.floor(Math.random() * 100),
|
||||||
type: 'CHAT',
|
type: 'CHAT',
|
||||||
};
|
};
|
||||||
|
|
||||||
test('can send a chat message', async (done) => {
|
test('can send a chat message', async (done) => {
|
||||||
const registration = await registerChat();
|
const registration = await registerChat();
|
||||||
const accessToken = registration.accessToken;
|
const accessToken = registration.accessToken;
|
||||||
|
|
||||||
sendChatMessage(testVisibilityMessage, accessToken, done);
|
sendChatMessage(testVisibilityMessage, accessToken, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('verify we can make API call to mark message as hidden', async (done) => {
|
test('verify we can make API call to mark message as hidden', async (done) => {
|
||||||
const res = await request.get('/api/admin/chat/messages').auth('admin', 'abc123').expect(200)
|
const registration = await registerChat();
|
||||||
|
const accessToken = registration.accessToken;
|
||||||
|
const ws = new WebSocket(
|
||||||
|
`ws://localhost:8080/ws?accessToken=${accessToken}`,
|
||||||
|
{
|
||||||
|
origin: 'http://localhost:8080',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const message = res.body[0];
|
// Verify the visibility change comes through the websocket
|
||||||
const messageId = message.id;
|
ws.on('message', function incoming(message) {
|
||||||
await request.post('/api/admin/chat/updatemessagevisibility')
|
const messages = message.split('\n');
|
||||||
.auth('admin', 'abc123')
|
messages.forEach(function (message) {
|
||||||
.send({ "idArray": [messageId], "visible": false }).expect(200);
|
const event = JSON.parse(message);
|
||||||
done();
|
|
||||||
|
if (event.type === 'VISIBILITY-UPDATE') {
|
||||||
|
done();
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await request
|
||||||
|
.get('/api/admin/chat/messages')
|
||||||
|
.auth('admin', 'abc123')
|
||||||
|
.expect(200);
|
||||||
|
|
||||||
|
const message = res.body[0];
|
||||||
|
const messageId = message.id;
|
||||||
|
await request
|
||||||
|
.post('/api/admin/chat/updatemessagevisibility')
|
||||||
|
.auth('admin', 'abc123')
|
||||||
|
.send({ idArray: [messageId], visible: false })
|
||||||
|
.expect(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('verify message has become hidden', async (done) => {
|
test('verify message has become hidden', async (done) => {
|
||||||
const res = await request.get('/api/admin/chat/messages')
|
const res = await request
|
||||||
.expect(200)
|
.get('/api/admin/chat/messages')
|
||||||
.auth('admin', 'abc123')
|
.expect(200)
|
||||||
|
.auth('admin', 'abc123');
|
||||||
|
|
||||||
const message = res.body.filter(obj => {
|
const message = res.body.filter((obj) => {
|
||||||
return obj.body === `${testVisibilityMessage.body}`;
|
return obj.body === `${testVisibilityMessage.body}`;
|
||||||
});
|
});
|
||||||
expect(message.length).toBe(1);
|
expect(message.length).toBe(1);
|
||||||
expect(message[0].hiddenAt).toBeTruthy();
|
expect(message[0].hiddenAt).toBeTruthy();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,5 +29,27 @@ function sendChatMessage(message, accessToken, done) {
|
|||||||
ws.on('open', onOpen);
|
ws.on('open', onOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function listenForEvent(name, accessToken, done) {
|
||||||
|
const ws = new WebSocket(
|
||||||
|
`ws://localhost:8080/ws?accessToken=${accessToken}`,
|
||||||
|
{
|
||||||
|
origin: 'http://localhost:8080',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
ws.on('message', function incoming(message) {
|
||||||
|
const messages = message.split('\n');
|
||||||
|
messages.forEach(function (message) {
|
||||||
|
const event = JSON.parse(message);
|
||||||
|
|
||||||
|
if (event.type === name) {
|
||||||
|
done();
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.sendChatMessage = sendChatMessage;
|
module.exports.sendChatMessage = sendChatMessage;
|
||||||
module.exports.registerChat = registerChat;
|
module.exports.registerChat = registerChat;
|
||||||
|
module.exports.listenForEvent = listenForEvent;
|
||||||
|
|||||||
Reference in New Issue
Block a user