UNPKG

dlms-protocol

Version:

DLMS Protocol parser for JavaScript

95 lines (72 loc) 3.73 kB
import DlmsDataType from "../src/enum/DlmsDataType"; import DlmsDatas from "../src/typesDLMS/DlmsDatas"; import DlmsCompactArray from "../src/typesDLMS/DlmsCompactArray"; import DlmsArrayStruct from "../src/typesDLMS/DlmsArrayStruct"; jest.mock("../src/typesDLMS/DlmsDatas"); // Mock da classe DlmsDatas jest.mock("../src/typesDLMS/DlmsArrayStruct"); // Mock da classe DlmsArrayStruct describe("DlmsCompactArray class", () => { beforeEach(() => { DlmsDatas.factory.mockClear(); DlmsArrayStruct.mockClear(); }); test("should initialize with default values when no data is provided", () => { const compactArray = new DlmsCompactArray(); expect(compactArray.tag).toBe(DlmsDataType.COMPACT_ARRAY); expect(compactArray.totalBytes).toBe(0); expect(compactArray.rawValue).toEqual([]); expect(compactArray.bodyBytes).toEqual([]); expect(compactArray.value).toEqual([]); }); test("should throw an error if tag does not match when inserting data", () => { const data = [0xFF, 1, 0x00]; // Tag inválida const compactArray = new DlmsCompactArray(); expect(() => compactArray.insertData(data)).toThrow("Tag mismatch."); }); // test("should correctly parse a compact array with non-struct descriptor", () => { // const mockDescriptor = { // getStandardLength: jest.fn().mockReturnValue(2), // }; // DlmsDatas.factory.mockReturnValueOnce(mockDescriptor); // const mockDlmsData = { // totalBytes: 3, // insertData: jest.fn(), // }; // DlmsDatas.factory.mockReturnValue(mockDlmsData); // const data = [DlmsDataType.COMPACT_ARRAY, 0x02, 2, 0x12, 0x34]; // const compactArray = new DlmsCompactArray(); // compactArray.insertData(data); // expect(compactArray.bodyBytes).toEqual([0x12, 0x34]); // expect(compactArray.totalBytes).toBe(data.length); // expect(mockDlmsData.insertData).toHaveBeenCalled(); // expect(compactArray.value.length).toBe(1); // expect(compactArray.rawValue).toEqual([DlmsDataType.COMPACT_ARRAY, 0x12, 0x34]); // }); // test("should correctly parse a compact array with struct descriptor", () => { // const mockStructDescriptor = new DlmsArrayStruct(); // const mockDlmsData = { // totalBytes: 4, // insertData: jest.fn(), // }; // mockStructDescriptor.totalBytes = 6; // DlmsDatas.factory.mockReturnValueOnce(mockStructDescriptor); // DlmsDatas.factory.mockReturnValue(mockDlmsData); // const data = [DlmsDataType.COMPACT_ARRAY, 0x02, 2, 0x12, 0x34, 0x56, 0x78]; // const compactArray = new DlmsCompactArray(); // compactArray.insertData(data); // expect(compactArray.bodyBytes).toEqual([0x12, 0x34, 0x56, 0x78]); // expect(compactArray.totalBytes).toBeGreaterThan(6); // expect(compactArray.value.length).toBe(1); // expect(mockDlmsData.insertData).toHaveBeenCalled(); // }); // test("should return the correct value with getValue()", () => { // const mockDlmsData = { totalBytes: 3 }; // DlmsDatas.factory.mockReturnValue(mockDlmsData); // const data = [DlmsDataType.COMPACT_ARRAY, 0x02, 2, 0x12, 0x34]; // const compactArray = new DlmsCompactArray(data); // expect(compactArray.getValue().length).toBe(1); // }); test("should throw an error if getStandardLength() is called", () => { const compactArray = new DlmsCompactArray(); expect(() => compactArray.getStandardLength()).toThrow("Method not implemented."); }); });