diff --git a/main.spec.js b/main.spec.js index a7f5b1f..530560d 100644 --- a/main.spec.js +++ b/main.spec.js @@ -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 = {}; diff --git a/message.js b/message.js index 1b59fb6..0610398 100644 --- a/message.js +++ b/message.js @@ -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;