dlms-protocol
Version:
DLMS Protocol parser for JavaScript
62 lines (49 loc) • 2.04 kB
JavaScript
import SetResponse from "../src/messages/SetResponse";
import MessageType from "../src/enum/MessageType";
describe("SetResponse class", () => {
test("should initialize with default values when no bytes are provided", () => {
const setResponse = new SetResponse();
expect(setResponse.setType).toBe(0);
expect(setResponse.invokeIdAndPriority).toBe(0);
expect(setResponse.dataAccessResult).toBeNull();
expect(setResponse.value).toEqual([]);
});
test("should correctly parse bytes with insertData()", () => {
const bytes = [
MessageType.SET_RESPONSE, // Message type
0x01, // setType
0xC3, // invokeIdAndPriority
0x05, // dataAccessResult
];
const setResponse = new SetResponse(bytes);
expect(setResponse.setType).toBe(0x01);
expect(setResponse.invokeIdAndPriority).toBe(0xC3);
expect(setResponse.dataAccessResult).toBe(0x05);
expect(setResponse.value).toEqual(bytes);
});
test("should return the correct byte array with getBytes()", () => {
const bytes = [
MessageType.SET_RESPONSE, // Message type
0x01, // setType
0xC3, // invokeIdAndPriority
0x05, // dataAccessResult
];
const setResponse = new SetResponse(bytes);
const result = setResponse.getBytes();
expect(result).toEqual(bytes);
});
test("should return the correct hexadecimal string with getHexString()", () => {
const bytes = [
MessageType.SET_RESPONSE, // Message type
0x01, // setType
0xC3, // invokeIdAndPriority
0x05, // dataAccessResult
];
const setResponse = new SetResponse(bytes);
const hexString = setResponse.getHexString();
const expectedHex = bytes
.map(byte => byte.toString(16).toUpperCase().padStart(2, '0'))
.join(' ');
expect(hexString).toBe(expectedHex);
});
});