Add tests and fix code

main
lambda 2 years ago
parent f2ee1d8c51
commit 4053fbb0f5
No known key found for this signature in database
GPG Key ID: 662ADE783CBB4026

@ -1,6 +1,42 @@
import { messageProcessor } from "./main.js";
import { jest } from '@jest/globals'
test('should fire error message with empty message', () => {
console.error = jest.fn();
var errorMessage = "Got mallformed message:";
var errorParams = { action: undefined, data: undefined };
messageProcessor.processMessage();
expect(console.error).toHaveBeenCalledWith(errorMessage, errorParams);
});
test('should fire error message with empty string', () => {
console.error = jest.fn();
var errorMessage = "Got mallformed message:";
var errorParams = { action: undefined, data: undefined };
messageProcessor.processMessage("");
expect(console.error).toHaveBeenCalledWith(errorMessage, errorParams);
});
test('should fire error message with null', () => {
console.error = jest.fn();
var errorMessage = "Got mallformed message:";
var errorParams = { action: undefined, data: undefined };
messageProcessor.processMessage(null);
expect(console.error).toHaveBeenCalledWith(errorMessage, errorParams);
});
test('should fire error message with undefined', () => {
console.error = jest.fn();
var errorMessage = "Got mallformed message:";
var errorParams = { action: undefined, data: undefined };
messageProcessor.processMessage(undefined);
expect(console.error).toHaveBeenCalledWith(errorMessage, errorParams);
});
test('should fire error message about mallformed message', () => {
console.error = jest.fn();
var message = {};

@ -18,7 +18,7 @@ const parseMessageFromString = (message) => {
function Message(data = {}) {
switch(Object.prototype.toString.call(data)) {
case "[object String]":
const parsedMessage = parseMessageFromString(x);
const parsedMessage = parseMessageFromString(data);
this.action = parsedMessage?.action;
this.data = parsedMessage?.data;