dlms-protocol
Version:
DLMS Protocol parser for JavaScript
57 lines (44 loc) • 2.22 kB
JavaScript
import DlmsDataType from "../src/enum/DlmsDataType";
import Boolean from "../src/typesDLMS/Boolean";
describe("Boolean class", () => {
test("should initialize with default values when no data is provided", () => {
const booleanInstance = new Boolean();
expect(booleanInstance.tag).toBe(DlmsDataType.BOOLEAN);
expect(booleanInstance.totalBytes).toBe(0);
expect(booleanInstance.rawValue).toEqual([]);
expect(booleanInstance.bodyBytes).toEqual([]);
expect(booleanInstance.value).toBeNull();
});
test("should initialize and parse data correctly when valid data is provided", () => {
const data = [DlmsDataType.BOOLEAN, 1];
const booleanInstance = new Boolean(data);
expect(booleanInstance.tag).toBe(DlmsDataType.BOOLEAN);
expect(booleanInstance.totalBytes).toBe(2);
expect(booleanInstance.rawValue).toEqual([DlmsDataType.BOOLEAN, 1]);
expect(booleanInstance.bodyBytes).toEqual([1]);
expect(booleanInstance.value).toBe(1);
});
test("should throw an error if tag does not match when inserting data", () => {
const data = [0xFF, 1]; // Invalid tag
const booleanInstance = new Boolean();
expect(() => booleanInstance.insertData(data)).toThrow("Tag mismatch.");
});
test("should correctly insert data when the tag matches", () => {
const data = [DlmsDataType.BOOLEAN, 0];
const booleanInstance = new Boolean();
booleanInstance.insertData(data);
expect(booleanInstance.totalBytes).toBe(2);
expect(booleanInstance.rawValue).toEqual([DlmsDataType.BOOLEAN, 0]);
expect(booleanInstance.bodyBytes).toEqual([0]);
expect(booleanInstance.value).toBe(0);
});
test("should return the correct value with getValue()", () => {
const data = [DlmsDataType.BOOLEAN, 1];
const booleanInstance = new Boolean(data);
expect(booleanInstance.getValue()).toBe(1);
});
test("should throw an error if getStandardLength() is called", () => {
const booleanInstance = new Boolean();
expect(() => booleanInstance.getStandardLength()).toThrow("Method not implemented.");
});
});