UNPKG

malwoden

Version:

![alt text](./coverage/badge-lines.svg) ![alt text](./coverage/badge-statements.svg) ![alt text](./coverage/badge-functions.svg) ![alt text](./coverage/badge-branches.svg)

286 lines 12 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var input_1 = require("../input"); var memory_terminal_1 = require("../terminal/memory-terminal"); var widget_1 = require("./widget"); var terminal_1 = require("../terminal"); var test_utils_spec_1 = require("../input/test-utils.spec"); var TestWidget = /** @class */ (function (_super) { __extends(TestWidget, _super); function TestWidget() { return _super !== null && _super.apply(this, arguments) || this; } TestWidget.prototype.onDraw = function () { }; return TestWidget; }(widget_1.Widget)); var ntw = function () { return new TestWidget({ initialState: {} }); }; describe("widget", function () { beforeEach(test_utils_spec_1.setupTestDom); it("Can add/remove a child", function () { var parent = ntw(); var childA = ntw(); var childB = ntw(); parent.addChild(childA); expect(parent["children"]).toEqual([childA]); parent.addChild(childB); expect(parent["children"]).toEqual([childA, childB]); parent.removeChild(childA); expect(parent["children"]).toEqual([childB]); parent.removeChild(childB); expect(parent["children"]).toEqual([]); }); it("Can set state", function () { var w = new TestWidget({ initialState: { number: 0 } }); expect(w.getState()).toEqual({ number: 0 }); w.setState({ number: 1 }); expect(w.getState()).toEqual({ number: 1 }); }); it("Can set/get the origin", function () { var w = ntw(); expect(w.getAbsoluteOrigin()).toEqual({ x: 0, y: 0 }); expect(w.getOrigin()).toEqual({ x: 0, y: 0 }); w.setOrigin({ x: 2, y: 1 }); expect(w.getAbsoluteOrigin()).toEqual({ x: 2, y: 1 }); expect(w.getOrigin()).toEqual({ x: 2, y: 1 }); }); it("Can check for disabled/not disabled", function () { var w = ntw(); expect(w.isDisabled()).toBeFalsy(); w.setDisabled(true); expect(w.isDisabled()).toBeTruthy(); w.setDisabled(false); expect(w.isDisabled()).toBeFalsy(); }); it("Can set an update func", function () { var w = new TestWidget({ initialState: { number: 0 } }); expect(w.getState()).toEqual({ number: 0 }); w.setUpdateFunc(function () { return ({ number: 1 }); }); w.update(); expect(w.getState()).toEqual({ number: 1 }); }); it("Can clear an update func", function () { var w = new TestWidget({ initialState: { number: 0 } }); expect(w.getState()).toEqual({ number: 0 }); w.setUpdateFunc(function () { return ({ number: 1 }); }); w.clearUpdateFunc(); w.update(); expect(w.getState()).toEqual({ number: 0 }); }); it("can calculate local/absolute positions", function () { var p = ntw(); var c = ntw(); var c2 = ntw(); c2.setOrigin({ x: 1, y: 1 }); c2.setParent(c); c.setOrigin({ x: 1, y: 2 }); c.setParent(p); expect(p.getAbsoluteOrigin()).toEqual({ x: 0, y: 0 }); expect(p.absoluteToLocal({ x: 0, y: 0 })).toEqual({ x: 0, y: 0 }); expect(p.absoluteToLocal({ x: 1, y: 2 })).toEqual({ x: 1, y: 2 }); expect(p.localToAbsolute({ x: 0, y: 0 })).toEqual({ x: 0, y: 0 }); expect(p.localToAbsolute({ x: 1, y: 2 })).toEqual({ x: 1, y: 2 }); expect(c.getAbsoluteOrigin()).toEqual({ x: 1, y: 2 }); expect(c.absoluteToLocal({ x: 0, y: 0 })).toEqual({ x: -1, y: -2 }); expect(c.absoluteToLocal({ x: 1, y: 2 })).toEqual({ x: 0, y: 0 }); expect(c.localToAbsolute({ x: 0, y: 0 })).toEqual({ x: 1, y: 2 }); expect(c.localToAbsolute({ x: -1, y: -2 })).toEqual({ x: 0, y: 0 }); expect(c2.getAbsoluteOrigin()).toEqual({ x: 2, y: 3 }); }); it("can set a parent", function () { var p1 = ntw(); var p2 = ntw(); var c = ntw(); expect(c["parent"]).toEqual(undefined); c.setParent(p1); expect(c["parent"]).toEqual(p1); expect(p1["children"]).toEqual([c]); expect(p2["children"]).toEqual([]); c.setParent(p2); expect(p1["children"]).toEqual([]); expect(p2["children"]).toEqual([c]); }); it("can cascade update", function () { var p = new TestWidget({ initialState: { n: 0 } }); var c = new TestWidget({ initialState: { n: 0 } }); c.setParent(p); c.setUpdateFunc(function () { return ({ n: c.getState().n + 1 }); }); p.setUpdateFunc(function () { return ({ n: p.getState().n + 1 }); }); expect(c.getState()).toEqual({ n: 0 }); expect(p.getState()).toEqual({ n: 0 }); p.cascadeUpdate(); expect(c.getState()).toEqual({ n: 1 }); expect(p.getState()).toEqual({ n: 1 }); // Test after clear p.clearUpdateFunc(); p.cascadeUpdate(); expect(c.getState()).toEqual({ n: 2 }); expect(p.getState()).toEqual({ n: 1 }); }); it("won't update if disabled", function () { var w = new TestWidget({ initialState: { n: 0 } }) .setDisabled() .setUpdateFunc(function () { return ({ n: 1 }); }); w.update(); expect(w.getState().n).toEqual(0); }); it("won't cascade update if disabled", function () { var p = new TestWidget({ initialState: { n: 0 } }).setDisabled(); var c = new TestWidget({ initialState: { n: 0 } }) .setUpdateFunc(function () { return ({ n: 1, }); }) .setParent(p); p.cascadeUpdate(); expect(c.getState().n).toEqual(0); p.setDisabled(false); p.cascadeUpdate(); expect(c.getState().n).toEqual(1); }); it("can cascade draw", function () { var p = new TestWidget({ initialState: { n: 0 } }); var c = new TestWidget({ initialState: { n: 0 } }); var pSpy = jest.spyOn(p, "onDraw").mockImplementation(function () { }); var cSpy = jest.spyOn(c, "onDraw").mockImplementation(function () { }); c.setParent(p); p.cascadeDraw(); expect(pSpy).toHaveBeenCalledTimes(1); expect(cSpy).toHaveBeenCalledTimes(1); }); it("can cascade click", function () { var p = ntw(); var c = ntw(); var pSpy = jest.spyOn(p, "onMouseClick").mockImplementation(function () { return false; }); var cSpy = jest.spyOn(c, "onMouseClick").mockImplementation(function () { return false; }); c.setParent(p); p.cascadeMouseClick({ button: 0, type: "mousedown", x: 1, y: 1 }); expect(pSpy).toHaveBeenCalledTimes(1); expect(cSpy).toHaveBeenCalledTimes(1); }); it("will return false on baseline onClick", function () { var p = ntw(); expect(p.onMouseClick({ x: 0, y: 0, button: 0, type: "mousedown" })).toEqual(false); }); it("can set the origin in the constructor", function () { var p = new TestWidget({ initialState: {}, origin: { x: 1, y: 2 } }); expect(p.getOrigin()).toEqual({ x: 1, y: 2 }); }); it("will exit on cascades if disabled", function () { var terminal = new memory_terminal_1.MemoryTerminal({ width: 10, height: 10 }); var p = ntw().setDisabled(); var c = ntw().setDisabled(); c.setParent(p); var pDrawSpy = jest.spyOn(p, "onDraw").mockImplementation(function () { }); var cDrawSpy = jest.spyOn(c, "onDraw").mockImplementation(function () { }); p.cascadeDraw(); p.draw(); c.draw(); expect(pDrawSpy).toHaveBeenCalledTimes(0); expect(cDrawSpy).toHaveBeenCalledTimes(0); var pClickSpy = jest .spyOn(p, "onMouseClick") .mockImplementation(function () { return false; }); var cClickSpy = jest .spyOn(c, "onMouseClick") .mockImplementation(function () { return false; }); var mouse = { x: 0, y: 1, button: 1, type: "mousedown", }; p.cascadeMouseClick(mouse); p.mouseClick(mouse); c.mouseClick(mouse); expect(pClickSpy).toHaveBeenCalledTimes(0); expect(cClickSpy).toHaveBeenCalledTimes(0); }); it("will stop a click cascade on capture", function () { var p = ntw(); var c = ntw(); c.setParent(p); var pClickSpy = jest .spyOn(p, "onMouseClick") .mockImplementation(function () { return false; }); var cClickSpy = jest .spyOn(c, "onMouseClick") .mockImplementation(function () { return true; }); var mouse = { x: 0, y: 1, button: 1, type: "mousedown", }; p.cascadeMouseClick(mouse); expect(pClickSpy).toHaveBeenCalledTimes(0); expect(cClickSpy).toHaveBeenCalledTimes(1); }); it("Can register a mouse context", function () { var _a, _b, _c; var c = new input_1.MouseContext(); var w = ntw(); w.registerMouseContext(c); expect(w["mouseRegistration"]).toBeTruthy(); expect((_a = w["mouseRegistration"]) === null || _a === void 0 ? void 0 : _a.mouseContext).toBeTruthy(); expect((_b = w["mouseRegistration"]) === null || _b === void 0 ? void 0 : _b.mouseOnDown).toBeTruthy(); expect((_c = w["mouseRegistration"]) === null || _c === void 0 ? void 0 : _c.mouseOnUp).toBeTruthy(); w.clearMouseContext(); expect(w["mouseRegistration"]).toBeFalsy(); }); it("Can act on a mouseClick", function () { var w = ntw(); var pClickSpy = jest .spyOn(w, "onMouseClick") .mockImplementation(function () { return false; }); var event = { x: 0, y: 0, type: "mousedown", button: 0, }; w.mouseClick(event); expect(pClickSpy).toHaveBeenCalledTimes(1); w.setDisabled(); w.mouseClick(event); expect(pClickSpy).toHaveBeenCalledTimes(1); }); it("Can draw a glyph relative to it's position", function () { var t = new memory_terminal_1.MemoryTerminal({ width: 10, height: 10 }); var w = ntw().setOrigin({ x: 1, y: 1 }).setTerminal(t); var g = new terminal_1.Glyph("f"); w.drawGlyph({ x: 0, y: 0 }, g); expect(t.glyphs.get({ x: 1, y: 1 })).toEqual(g); // test without terminal w.setTerminal(); w.drawGlyph({ x: 1, y: 1 }, g); expect(t.glyphs.get({ x: 2, y: 2 })).toBeUndefined(); }); it("can set/unset terminal/mouseHandler from children", function () { var t = new memory_terminal_1.MemoryTerminal({ width: 10, height: 10 }); var h = new input_1.MouseHandler(); var p = ntw().setTerminal(t).setMouseHandler(h); var c = ntw().setParent(p); expect(p["mouseHandler"]).toBeTruthy(); expect(p["terminal"]).toBeTruthy(); expect(c["mouseHandler"]).toBeTruthy(); expect(c["terminal"]).toBeTruthy(); p.removeChild(c); expect(c["mouseHandler"]).toBeFalsy(); expect(c["terminal"]).toBeFalsy(); // won't error out on second call expect(p.removeChild(c)).toBeUndefined(); }); }); //# sourceMappingURL=widget.spec.js.map