dlms-protocol
Version:
DLMS Protocol parser for JavaScript
103 lines (84 loc) • 4.65 kB
JavaScript
import Message from "../src/messages/Message";
import InformationReport from "../src/messages/InformationReport";
import WriteRequest from "../src/messages/WriteRequest";
import WriteResponse from "../src/messages/WriteResponse";
import SetRequest from "../src/messages/SetRequest";
import SetResponse from "../src/messages/SetResponse";
import ActionRequest from "../src/messages/ActionRequest";
import ActionResponse from "../src/messages/ActionResponse";
import GetRequest from "../src/messages/GetRequest";
import GetResponse from "../src/messages/GetResponse";
import DataNotification from "../src/messages/DataNotification";
import ReadRequest from "../src/messages/ReadRequest";
import ReadResponse from "../src/messages/ReadResponse";
import UnconfirmedWriteRequest from "../src/messages/UnconfirmedWriteRequest";
import GlobalBlockTransfer from "../src/messages/GlobalBlockTransfer";
import MessageType from "../src/enum/MessageType";
jest.mock("../src/messages/InformationReport");
jest.mock("../src/messages/WriteRequest");
jest.mock("../src/messages/WriteResponse");
jest.mock("../src/messages/SetRequest");
jest.mock("../src/messages/SetResponse");
jest.mock("../src/messages/ActionRequest");
jest.mock("../src/messages/ActionResponse");
jest.mock("../src/messages/GetRequest");
jest.mock("../src/messages/GetResponse");
jest.mock("../src/messages/DataNotification");
jest.mock("../src/messages/ReadRequest");
jest.mock("../src/messages/ReadResponse");
jest.mock("../src/messages/UnconfirmedWriteRequest");
jest.mock("../src/messages/GlobalBlockTransfer");
describe("Message.factory", () => {
beforeEach(() => {
jest.clearAllMocks();
});
const mockBytes = [0x01, 0x02, 0x03];
test("should create an InformationReport when MessageType is INFORMATION_REPORT", () => {
InformationReport.mockImplementation(() => ({ type: "InformationReport" }));
const message = Message.factory([MessageType.INFORMATION_REPORT, ...mockBytes]);
expect(InformationReport).toHaveBeenCalledWith([MessageType.INFORMATION_REPORT, ...mockBytes]);
expect(message.type).toBe("InformationReport");
});
test("should create a WriteRequest when MessageType is WRITE_REQUEST", () => {
WriteRequest.mockImplementation(() => ({ type: "WriteRequest" }));
const message = Message.factory([MessageType.WRITE_REQUEST]);
expect(WriteRequest).toHaveBeenCalledWith();
expect(message.type).toBe("WriteRequest");
});
test("should create a WriteResponse when MessageType is WRITE_RESPONSE", () => {
WriteResponse.mockImplementation(() => ({ type: "WriteResponse" }));
const message = Message.factory([MessageType.WRITE_RESPONSE, ...mockBytes]);
expect(WriteResponse).toHaveBeenCalledWith([MessageType.WRITE_RESPONSE, ...mockBytes]);
expect(message.type).toBe("WriteResponse");
});
test("should create a SetRequest when MessageType is SET_REQUEST", () => {
SetRequest.mockImplementation(() => ({ type: "SetRequest" }));
const message = Message.factory([MessageType.SET_REQUEST]);
expect(SetRequest).toHaveBeenCalledWith();
expect(message.type).toBe("SetRequest");
});
test("should create a SetResponse when MessageType is SET_RESPONSE", () => {
SetResponse.mockImplementation(() => ({ type: "SetResponse" }));
const message = Message.factory([MessageType.SET_RESPONSE, ...mockBytes]);
expect(SetResponse).toHaveBeenCalledWith([MessageType.SET_RESPONSE, ...mockBytes]);
expect(message.type).toBe("SetResponse");
});
test("should create an ActionRequest when MessageType is ACTION_REQUEST", () => {
ActionRequest.mockImplementation(() => ({ type: "ActionRequest" }));
const message = Message.factory([MessageType.ACTION_REQUEST]);
expect(ActionRequest).toHaveBeenCalledWith();
expect(message.type).toBe("ActionRequest");
});
test("should create an ActionResponse when MessageType is ACTION_RESPONSE", () => {
ActionResponse.mockImplementation(() => ({ type: "ActionResponse" }));
const message = Message.factory([MessageType.ACTION_RESPONSE, ...mockBytes]);
expect(ActionResponse).toHaveBeenCalledWith([MessageType.ACTION_RESPONSE, ...mockBytes]);
expect(message.type).toBe("ActionResponse");
});
test("should throw an error for an unsupported MessageType", () => {
const unsupportedType = 0xFF;
expect(() => {
Message.factory([unsupportedType, ...mockBytes]);
}).toThrow(`Unsupported MessageType: ${unsupportedType}`);
});
});